When i try to implement a server program giving UDP as protocol , it works fine , but if the same c

Discussion in 'Perl Misc' started by Tompyna, Feb 12, 2004.

  1. Tompyna

    Tompyna Guest

    Hi All,

    I am a newbie to Socket Programming . I had written a socket
    program in perl , implementing client and server.

    Now coming to the problem i am stuck:

    1.When i implement the program with "UDP" as protocol option, the code
    works
    fine,but When i try to implement the same program , giving TCP as
    the
    protocol option, it gives me an error:
    "Can't call method "recv" on an undefined value at servermodulenew.pl
    line 18"

    I believe the method "recv" should be called irrespective of the
    protocol option i give.

    Hope someone could possibly give me an answer to this problem.

    (Below is the code for server implementation)
    NOTE: The program is executing on solaris and run as a background
    process.

    The server Program i implemented is:

    /************************************************/
    # Server Program

    use IO::Socket;

    my $port = 1024;

    #use ngd_simulator::thomasp::perl::Socket;

    my $receive_status;

    print ">> Server Program <<\n";

    # Create a new socket
    $MySocket=new IO::Socket::INET->new(LocalPort=>($port),Proto=>(getprotobyname('udp')));

    while(1)
    {
    #Receiving a message from the client
    $receive_status = $MySocket->recv($text,128);

    if($text ne '')
    {
    print "\nReceived message ===>", $text,"\n";

    }
    # If client message is empty exit
    else
    {
    print "Cilent has exited!";
    exit 1;

    }
    }
    #Close the Socket after sending the message
    close($MySocket);

    /******************************************************/



    Thanx.... in advance,
    Thomas Poly.
    Tompyna, Feb 12, 2004
    #1
    1. Advertising

  2. Tompyna

    gnari Guest

    "Tompyna" <> wrote in message
    news:...

    > 1.When i implement the program with "UDP" as protocol option, the code
    > works
    > fine,but When i try to implement the same program , giving TCP as
    > the
    > protocol option, it gives me an error:
    > "Can't call method "recv" on an undefined value at servermodulenew.pl
    > line 18"
    >
    > I believe the method "recv" should be called irrespective of the
    > protocol option i give.


    UDP is connectionless, TCP is not.

    with TCP you have to
    make socket
    listen
    accept connections when they arrive
    and then read data

    gnari
    gnari, Feb 12, 2004
    #2
    1. Advertising

  3. Tompyna

    Guest

    In comp.lang.perl.misc Tompyna <> wrote:

    > 1.When i implement the program with "UDP" as protocol option, the code
    > works
    > fine,but When i try to implement the same program , giving TCP as
    > the
    > protocol option, it gives me an error:
    > "Can't call method "recv" on an undefined value at servermodulenew.pl
    > line 18"


    Thats because TCP doesn't have a recv. :)

    UDP is connection-less, you're responsible for any network errors and
    dropped packets, it has a lot in common with the US postal service, in that
    it will loose your mail and not care.

    TCP is more like a phone connection, so the methods are different.

    (TCP and UNIX sockets are a lot like working with file handles)

    Jamie
    , Feb 12, 2004
    #3
  4. Tompyna

    Anno Siegel Guest

    Tompyna <> wrote in comp.lang.perl.misc:

    [...]

    > 1.When i implement the program with "UDP" as protocol option, the code
    > works
    > fine,but When i try to implement the same program , giving TCP as
    > the
    > protocol option, it gives me an error:
    > "Can't call method "recv" on an undefined value at servermodulenew.pl
    > line 18"


    [...]

    > The server Program i implemented is:
    >
    > /************************************************/
    > # Server Program


    No strict, no warnings. Switch them on.

    > use IO::Socket;
    >
    > my $port = 1024;
    >
    > #use ngd_simulator::thomasp::perl::Socket;
    >
    > my $receive_status;
    >
    > print ">> Server Program <<\n";
    >
    > # Create a new socket
    > $MySocket=new


    You have declared other variables lexicals. Why is $MySocket a package
    variable?

    > IO::Socket::INET->new(LocalPort=>($port),Proto=>(getprotobyname('udp')));


    This is the statement that returns an undefined value for $MySocket. Why
    don't you ask why?

    my $MySocket = new IO::Socket::INET->new(
    LocalPort=>($port),Proto=>(getprotobyname('udp'))) or
    die "Socket error: $!";

    I notice that you call the new() method twice in that statement, once as
    as class method and once as an object method. I don't think you want to
    do that.

    [rest snipped]

    Anno
    Anno Siegel, Feb 12, 2004
    #4
  5. Re: When i try to implement a server program giving UDP as protocol, it works fine , but if the same code is executed with TCP as protocoloption, it gives an error.

    Tompyna wrote:

    > Hi All,
    >
    > I am a newbie to Socket Programming . I had written a socket
    > program in perl , implementing client and server.
    >
    > Now coming to the problem i am stuck:
    >
    > 1.When i implement the program with "UDP" as protocol option, the code
    > works
    > fine,but When i try to implement the same program , giving TCP as
    > the
    > protocol option, it gives me an error:
    > "Can't call method "recv" on an undefined value at servermodulenew.pl
    > line 18"

    [cut]
    > # Create a new socket
    > $MySocket=new IO::Socket::INET->new(LocalPort=>($port),Proto=>(getprotobyname('udp')));
    >
    > while(1)
    > {
    > #Receiving a message from the client
    > $receive_status = $MySocket->recv($text,128);
    >
    > if($text ne '')


    As far as I know to use a connection oriented socket in general (with recv as
    well), as tcp is, you must connect first.
    --
    Dr.Ugo Gagliardelli,Modena,ItalyCertifiedUindoscrasherAñejoAlcoolInside
    Spaccamaroni andate a cagare/Spammers not welcome/Spammers vão à merda
    Spamers iros a la mierda/Spamers allez vous faire foutre
    Spammers loop schijten/Spammers macht Euch vom Acker
    Dr. Ugo Gagliardelli, Feb 17, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam P
    Replies:
    0
    Views:
    469
    Adam P
    Sep 27, 2005
  2. Eric
    Replies:
    5
    Views:
    556
  3. catweezle2010
    Replies:
    0
    Views:
    385
    catweezle2010
    Jan 26, 2006
  4. entrepreneur
    Replies:
    11
    Views:
    495
    Luc The Perverse
    Jan 1, 2007
  5. Mufasa
    Replies:
    0
    Views:
    805
    Mufasa
    Jun 10, 2007
Loading...

Share This Page