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

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>?

Replies are listed 'Best First'.
Re^5: Bidirectional use of a Socket
by scotchfx (Sexton) on Jul 04, 2005 at 03:04 UTC
    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.