Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

recv in socket programming

by madhu.hanigadde (Novice)
on Feb 12, 2009 at 10:20 UTC ( #743264=perlquestion: print w/replies, xml ) Need Help??
madhu.hanigadde has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, am trying to interacting with a server.I should get a response from the server when i will send a command. But there is a chance where i wont get response sometimes. In my code the recv function will get the response wen the server sends, but if there is no response then recv will hangout and will wait till the response come...i want it to wait for sometime and should comeout... Here is my code....
#!/usr/bin/perl use IO::Socket; $|=1; $socket = new IO::Socket::INET ( PeerAddr => '$IP', PeerPort => 3575, Proto => 'tcp', ) or die "Couldn't connect to Server\n"; while (1) { $send_data = "CHK $ARGV[0]"; print STDERR "\nSEND( TYPE q or Q to Quit):$send_data\n"; if ($send_data ne 'q' and $send_data ne 'Q') { $socket->send($send_data); sleep(1); $socket->recv($recv_data1,1024); sleep(1); $socket->recv($recv_data2,10240); print "RECIEVED: $recv_data2\n"; close $socket; last; } else { $socket->send($send_data); close $socket; last; } }

20090213 Janitored by Corion: Closed code tag, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: recv in socket programming
by irah (Pilgrim) on Feb 12, 2009 at 12:39 UTC
    You can use Non-blocking I/O concept. Please use IO::Socket::Inet.
Re: recv in socket programming
by gone2015 (Deacon) on Feb 12, 2009 at 13:20 UTC

    If the far end simply stops responding, but leaves the connection open, then you are indeed in for a long wait.

    BTW send and recv are really packet level operations... and may not be what you really want with TCP. In particular, $socket->recv($recv_data2,10240); is unlikely to return anything like 10K of data.

    So you need to implement a time-out. You can use an ALRM signal to do this -- but it's a bit messy and won't work on all systems (notably not on Windows -- go figure). So I'd use IO::Select, as in the example below.

Re: recv in socket programming
by weismat (Friar) on Feb 12, 2009 at 12:33 UTC
    You may use IO::Select before the recv to determine if yoy can read from the socket.
    my $select=new IO::Select(); $select->add($socket); if ($select->can_read($timeout)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://743264]
Approved by marto
[marto]: good music, is good music, ignore the genre :P
[Corion]: marto: Thank you, reminds me of good times ;))
[Corion]: holli: If you are in Frankfurt, I can recommend the Ubahnkontrollöre - they have two gigs, one on the 28th Dec., the next in May 2018
[Corion]: They don't do new material anymore, but the old stuff is good enough IMO :)
[holli]: hessenrap...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-11-18 19:24 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.