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

I've been messing around with some sockets and am having great difficulty with bidirectional setups. For simplicity, I want program A to send program B a packet containing "1" and B to return a packet containing "2". Thats all. Just a message and a response back.

I can't figure this out. I've read docs for IO::Socket. The cougar book really doesn't help me much here, either.

Also, while I'm here, is it safe to say the TCP won't drop packets similar to UDP? (Or atleast, if it can make a connection it'll resend the packets automatically.)

Thanks

Replies are listed 'Best First'.
Re: Simple Msg/Response Socket Setup
by ton (Friar) on Aug 31, 2001 at 07:48 UTC
    I generally use Socket instead of the higher-level IO::Socket, but that's just my personaly preference. Here's some code to get you started. First the server:
    use strict; use Socket; my $socket; # the socket waiting for incoming connections my $incoming; # an incoming connection my $port = 4000; my $ipaddr = "localhost"; my $numConnections = 1; socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "Could not create socket: $!\n"; bind($socket, sockaddr_in($port, inet_aton($ipaddr))) || die "Could not bind socket: $!\n"; listen($socket, $numConnections) || die "Could not listen on socket: $!\n"; accept($incoming, $socket) || die "Could not accept connection on socket: $!\n"; my $message; my $messageLen = 999999; # the maximum number of bytes to read off th +e socket defined(recv($incoming, $message, $messageLen, 0)) || die "Receive err +or: $!"; print "I got the following message: $message.\n"; defined(send($incoming, "2", 0)) || die "Send error: $!"; shutdown($incoming, 2); shutdown($socket, 2);
    and now the client.
    use strict; use Socket; my $socket; my $port = 4000; my $ipaddr = "localhost"; socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "Could not create socket: $!\n"; connect($socket, sockaddr_in($port, inet_aton($ipaddr))) || die "Could not connect: $!\n"; my $message; my $messageLen = 999999; # the maximum number of bytes to read off the + socket defined(send($socket, "1", 0)) || die "Send error: $!"; defined(recv($socket, $message, $messageLen, 0)) or die "Receive error +: $!"; print "I got the following message: $message.\n"; shutdown($socket, 2);
    Good luck!

    -Ton
    -----
    Be bloody, bold, and resolute; laugh to scorn
    The power of man...

Re: Simple Msg/Response Socket Setup
by Beatnik (Parson) on Aug 31, 2001 at 10:06 UTC
    This is a basic example of IO::Socket. Check the POD again coz this is where I learnt it from.
    #!/usr/bin/perl -w #Client use strict; use IO::Socket::INET; my $remote_host = "127.0.0.1"; #define remote host... local host now p +robably my $remote_port = 5050; #define port here my $socket = IO::Socket::INET->new( PeerAddr => $remote_host, PeerPort => $remote_port, Proto => "tcp", Type => SOCK_STREAM) || die "Can't connect to $remote_host:$remote_port -> $!\n"; print $socket "Quoth the Raven\n"; my $answer = <$socket>; print $answer; close($socket);




    #!/usr/bin/perl -w #Server use strict; use IO::Socket::INET; my $local_port = 5050; #Define a port my $server = IO::Socket::INET->new( LocalPort => $local_port, Reuse => 1, Listen => SOMAXCONN, Type => SOCK_STREAM) || die "Can't start server on $local_port -> @!\n"; my $client = undef; my $user = undef; my $file = undef; print "accepting connections...\n"; while($client = $server->accept()) { $client->autoflush(1); my $line = undef; if(defined($line = <$client>)) { print "received :", $line; print $client "nevermore"; } close($client); } close($server);
    Enjoy!

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Simple Msg/Response Socket Setup
by princepawn (Parson) on Aug 31, 2001 at 02:56 UTC
    you might use stem for this.

    or take a look in Linconln Stein's "Network Programming with Perl"