in reply to Re^2: Bidirectional use of a Socket
in thread Bidirectional use of a Socket

One obvious problem is that accept. You don't need to accept, as the connection (between client and server) has been created already, at the time when you call ->new().

On the client side, the porgram above, you can use $t_socket for both write and read, to "listen for response", all what you need to do is <$t_socket>. Of course , for better control, you can always use select, which is something you want to learn next step.

Replies are listed 'Best First'.
Re^4: Bidirectional use of a Socket
by scotchfx (Sexton) on Jul 04, 2005 at 02:43 UTC
    Is there a way to tell if there is an incoming message without having to go into a while(<$t_socket>){...} loop?

    (something like an if(<$t_socket>) or perhaps a function I can call on the $t_socket itself?

    Thanks!

      IO::Select is what you want.

      The following code can show you some basic ideas of IO::Select. Follow those steps:

      1. Run perl -w server.pl 3000 1
      2. Run perl -w server.pl 3001 10
      3. Run perl -w client.pl

      Observe the client, see how it receives from server 1 (the faster guy), without being locked by the slower guy (server 2).

      server

      use strict; use warnings; use IO::Socket::INET; my $server = IO::Socket::INET->new(Proto => "tcp", LocalAddr => "local +host", LocalPort => $ARGV[0], Listen => 10); my $connection = $server->accept(); print "Connected\n"; while (1) { print $connection "$ARGV[0]\r\n"; print "sent $ARGV[0]\n"; sleep($ARGV[1]); }

      client

      use strict; use warnings; use IO::Socket::INET; use IO::Select; my $connection1 = IO::Socket::INET->new(PeerAddr => "localhost", PeerP +ort => 3000, Proto => "tcp"); my $connection2 = IO::Socket::INET->new(PeerAddr => "localhost", PeerP +ort => 3001, Proto => "tcp"); my $selector = IO::Select->new(); $selector->add($connection1); $selector->add($connection2); while (1) { my @readers = $selector->can_read(0); for my $reader (@readers) { my $line = <$reader>; print $line; } }
Re^4: Bidirectional use of a Socket
by scotchfx (Sexton) on Jul 04, 2005 at 02:59 UTC
    I still can't seem to get the following section of code to work - it never enters the while<$t_socket>{...} loop to receive input from the Third process (the one it sends $name)

    if($name eq $match) { print "Second found match: $name\n"; print "Second sending \"$name\" to Third\n"; print $t_socket "$name\n"; while(<$t_socket>) { #print "Second has accepted socket to Third...\n"; my $name = $_; chomp($name); my $t_host = gethostbyaddr($t_socket->peeraddr, AF_INE +T); my $t_port = $t_socket->peerport; print "Received from [$f_host $f_port]: $name\n"; } }

    Do I need to do anything in particular to "listen" on the <$t_socket>?

      For your reference, here is the entire code:
      use IO::Socket; use strict; use warnings; our $F_PORT = shift or die "Please specify First port number\n"; our $T_PORT = shift or die "Please specify Third port number\n"; #our $file = shift or die "Please specify input file\n";; my $f_socket = IO::Socket::INET->new('LocalPort' => $F_PORT, 'Proto' => 'tcp', 'Listen' => 2) or die "Second: Can't create socket to First($!)\n"; my $t_socket = IO::Socket::INET->new( 'PeerAddr' => "localhost", 'PeerPort' => $T_PORT, 'Proto' => 'tcp') or die "Second: Can't create socket to Third($!)\n"; my $match = "brenna"; print "Second listening\n"; while(my $first = $f_socket->accept()) { #setup acknowledgement print "Second: Accepted connection from First..\n"; my $f_host = gethostbyaddr($first->peeraddr, AF_INET); my $f_port = $first->peerport; while (<$first>) { my $name = $_; chomp($name); #send acknowledgement message to First print "Received from [$f_host $f_port]: $name\n"; #print $f_socket "$.: $name\n"; if($name eq $match) { print "Second found match: $name\n"; print "Second sending \"$name\" to Third\n"; print $t_socket "$match\n"; while(<$t_socket>) { #print "Second has accepted socket to Third...\n"; my $name = $_; chomp($name); my $t_host = gethostbyaddr($t_socket->peeraddr, AF_INE +T); my $t_port = $t_socket->peerport; print "Received from [$f_host $f_port]: $name\n"; } } } } die "Second: Can't accept socket from First($!)\n";

      Any help would be much much appreciated... I've been beating my head against this for days now.