Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Socket recv?

by packetstormer (Monk)
on Mar 11, 2011 at 20:39 UTC ( #892740=perlquestion: print w/replies, xml ) Need Help??
packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use Perl to create a simple socket client. It connects to a remote server and then sends some text then gets the server response then sends some more text etc. My problem is I can only get it to connect, send some text and show the servers reply. It won't continue to send and receive, the "Enter String to send" appears each time as per the loop but it doesn't send or recv anything to the server. Code below:
use IO::Socket::INET; $socket = new IO::Socket::INET ( PeerAddr => '', PeerPort => 6001, Proto => 'tcp', ) or die "Couldn't connect to Server\n"; while ($socket) { print "\nEnter a string to send to SIP: "; $string=<STDIN>; chomp($string); $socket->send($string."\n"); $socket->recv($data,1024); print $data; }
As I am still a beginner I am sure this is a silly error. Can anyone spot it? Thanks

Replies are listed 'Best First'.
Re: Socket recv?
by Illuminatus (Curate) on Mar 11, 2011 at 21:56 UTC
    1. You do know you should be using 'use strict;', right?
    2. You are not checking the return values of either the send or recv. This might shed some light on what is going on
    3. Closing of the socket will not change $socket to null, or unset it. An exception state on the socket will not terminate the loop


Re: Socket recv?
by Corion (Pope) on Mar 11, 2011 at 20:52 UTC

    I think send and recv are only for UDP, not for TCP.

    For TCP, you can just treat the socket as a filehandle and read and write using print and <> (or readline).

      Please don't encourage people to use <> / readline on sockets. It is a great way to have things appear to lock up mysteriously (just because something arrived that didn't end in a newline character). It is a much better idea to read from a socket using read or sysread.

      - tye        

        read will block until the requested number of characters has arrived (if possible). (Not sure how that's different than readline.) select cannot be used in conjunction with read.

        sysread will block until data is present and return whatever data is present (up to the specified amount). sysread and select can work in concert.

      Like this?
      ....... print "\nEnter a string to send to SIP: "; $string=<STDIN>; chomp($string); print $socket $string; #$socket->send($string."\n"); #$socket->recv($data,1024); #print $data; print <$socket>;
      Its the same result!

        I don't know - I would imagine that you will need to look at what gets send over the wire to know what your server responds. Also look at Net::SIP if you're actually doing SIP.

      They can be used with TCP, but I don't think there are any benefits to doing so.
      send and recv are calls that can only be used in a connected-state socket (ie TCP).

        You have to use send for unconnected sockets. Saying it can't be used for unconnected sockets is nonsense. The system would have no idea where to send the data to.

        Furthermore, UDP sockets can be connected. Connecting a UDP socket specifies the default host+port for sending, and it specifies the only host+port from which it will receive.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://892740]
Approved by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2018-05-21 10:03 GMT
Find Nodes?
    Voting Booth?