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

I have a simple TCP server; and I am wishing creating a client to interact with it. The server send outs commands in individual packets, and expects responces in the same manner. I am new to sockets etc, how would i go about creating a client in perl script to communicate with this server?
  • Comment on Creating a TCP client, that receives and responds in individual packets

Replies are listed 'Best First'.
Re: Creating a TCP client, that receives and responds in individual packets
by valdez (Monsignor) on Nov 11, 2002 at 10:47 UTC
Re: Creating a TCP client, that receives and responds in individual packets
by Bukowski (Deacon) on Nov 11, 2002 at 10:29 UTC
    I haven't done this myself, or rather I haven't taken more than a cursory look at sockets myself, but would

    IO::Socket be appropriate for this? I seem to remember it being used in a client in Learning Perl.

    Bukowski - aka Dan (dcs@black.hole-in-the.net)
    "Coffee for the mind, Pizza for the body, Sushi for the soul" -Userfriendly

Re: Creating a TCP client, that receives and responds in individual packets
by dingus (Friar) on Nov 11, 2002 at 10:42 UTC
    Take a look at Expect.pm and also some of the POE sample code for some ideas.

    Dingus


    Enter any 47-digit prime number to continue.
Re: Creating a TCP client, that receives and responds in individual packets
by pg (Canon) on Nov 11, 2002 at 16:19 UTC
    Hope this helps:
    server.pl: use strict; use IO::Socket; my $server = IO::Socket::INET->new(Proto => "tcp", Listen => 5, LocalPort => "3000", Timeout => 2000) || die "failed to start server\n"; print "server wait for client to connect ...\n"; my $connection = $server->accept; print "a client connected\n"; while (1) { my $req; $connection->recv($req, 7000); print "server received $req\n"; my $res = $req + 1; print $connection $res; print "server responsed $res\n"; } client.pl use strict; use IO::Socket; my $client = IO::Socket::INET->new(Proto => "tcp", PeerAddr => "172.135.2.54", PeerPort => "3000", Timeout => 2) || die "failed to connect to a server\n"; my $req = 1; while (1) { print $client $req; print "client sent $req\n"; my $res; $client->recv($res, 7000); print "client received $res\n"; $req = $res; }
      pg, you've solved my problem, thanks a lot. I see how its done now, before I was using a loop like: while (sysread($handle, $byte, 1) == 1) { print; } to receive every byte, as the server sent no line feeds or other control characters to seperate its messages. It simply sends them as individual tcp packet's - i found this out using Ethereal (packet sniffer). Thanks to everyone to replied.
Re: Creating a TCP client, that receives and responds in individual packets
by iburrell (Chaplain) on Nov 11, 2002 at 17:19 UTC
    It is difficult for an application to control sending individual packets over TCP. The server could be using UDP, which does use single packets, or it could be using a protocol with messages over a TCP stream. With the TCP server, use the low-level sysread/syswrite to make sure that you don't block waiting for data.
      Here involves two different concepts:
      1. TCP reassembles all packets, and that is transparent to the end programmer.
      2. When your application reads from buffer, it shall take care of the boundary between packets.