packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

I am a newbie to perl and I am trying to write a small socket client that will conenct to a remote server on a certain tcp port and then send and receive data until termianted. The problem is I can only send and receive one piece of data. That is, I can't seem to keep sending the receivign. The connection stays open but I can't anymore than the first line. Here is my code:
use IO::Socket; $socket = new IO::Socket::INET ( PeerAddr => '192.168.1.20', PeerPort => 1969, Proto => 'tcp', ) or die "Couldn't connect to Server\n"; while (1) { $socket->recv($recv_data,1024); if ($recv_data eq 'q' or $recv_data eq 'Q') { close $socket; last; } else { print "RECIEVED: $recv_data"; print "\nSEND( TYPE q or Q to Quit):"; $send_data = <STDIN>; chop($send_data); if ($send_data ne 'q' and $send_data ne 'Q') { $socket->send($send_data); } else { $socket->send($send_data); close $socket; last; } } }

Replies are listed 'Best First'.
Re: Socket Questions
by kcott (Archbishop) on Nov 05, 2010 at 17:52 UTC

    Adding use warnings; to the top of the script, I immediately get:

    Name "main::send_date" used only once: possible typo at ...

    So, that presumably needs to be $send_data.

    You should also add use strict; to the top of the script and fix all the issues it reports.

    There also seems to be problems with functionality. You have, for instance:

    print "\nSEND( TYPE q or Q to Quit):";

    but I can't see where you're capturing the user input.

    As you're new to Perl, I'd also recommend you add use diagnostics; to the top of your script - this will provide more verbose messages.

    You can also test the script without actually running it from the commandline by typing:

    perl -c script_name

    Finally, perlipc has lots of information about using sockets.

    -- Ken

      I think I might have pasted the wrong worksheet the last time, this one is the one I am using (edited original post with code tags too). I will try the strict and diag settings too. What happens is the socket connects and the response from the server is dumped to screen. I then input my response and although I can see the <STIND> appear on the remote servers log the response from the server never appears on the perl app, if you know what I mean.

        When you post sentences like this:

        That is, I can't seem to keep sending the receivign.

        ...you have more important things to worry about than learning perl sockets.

        However, when dealing with sockets you have to realize that the data you send over a socket can be broken up into many chunks, and you can't know ahead of time how many chunks there will be. As a result, the other side(e.g. the server) has to know when to stop waiting for chunks of data to arrive and that your transmission is complete. Should the server stop trying to receive data after receiving one chunk of data? What if your data got broken up into three chunks? Should the server stop trying to receive data after reading three chunks? What if your data got sent in only one chunk? Then the server will wait indefinitely for two other chunks to arrive

        Because you can't know how many chunks your message will get broken into, you have to send some kind of signal to the server that it has read the end of your message. Is the server waiting until it reads "GOOGLEPLEX" before considering the transmission complete? Or, maybe its "THE END"? Did you add either of those to the end of the data you sent to the server?

        I suggest that before you try to interact with a remote server that you write your own server program to interact with your client program. Then you will hopefully see the issues involved in trying to determine when the other side has finished transmitting data.

        In the event you consider that too much bother, you have to realize that in order to communicate with a remote server, you have to know what 'protocol' the server expects you to use. That is, among other things, you must know what character(s) the server expects you to use to signal the end of your message.

Re: Socket Questions
by Illuminatus (Curate) on Nov 05, 2010 at 17:34 UTC
    Please edit your post and put code tags around the code.

    It seems like you are expecting data from stdin, but you never actually read anything. You reference a variable ($send_data) that is never set (the only set is commented out).

    The connection stays open but I can't anymore than the first line.
    What exactly does it do? Hang? If so, where?

    fnord

Re: Socket Questions
by jethro (Monsignor) on Nov 05, 2010 at 17:36 UTC

    Please use <code> tags around your code listing, otherwise lots of special characters and the indentation are invisible

    What I can see is that you seem to recv twice inside the loop, once at the start and once at the end of the loop. So your script will listen->send->listen->listen->send