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

I am trying to get the following segment of code to work - the code sends a message between processes then waits (at least in my mind) for a response from the process to which it just sent:
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"; } }
however for some reason I can never enter the while(<$t_socket>){...} loop...

I was told not to use $t_socket->accept() since I have already opened the socket, is this correct?

why can I not listen on this $t_socket after I have sent to it?

I am including my entire code below:

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 advice would be much appreciated!

Thanks!

Replies are listed 'Best First'.
Re: Read/Write Socket question (Bidirectional my ass...)
by castaway (Parson) on Jul 04, 2005 at 10:26 UTC
    What sort of data do you get back from the $T_PORT process? Reading with <$t_socket> will only work if the data you are expecting contains newlines. (the angle brackets are a call to readline), if you have no newlines, then try using recv instead.

    Have you tried testing with telnet? ie connect with telnet to localhost $T_PORT, type in the data you are trying to send with your script, and see what it answers?

    C.

      Do I need to setup a UDP connection specifically to use recv? To do this do I only need to specify 'Proto'=>'udp' when opening the socket?

      I am attempt to read a string back through the <$t_socket> -it is a name that I need to find a eq match for - the link below has a better description of my project:

      http://www.perlmonks.org/?node_id=472004

      Essentially I am trying to match input given to me by a First process, if I match this input in the Second process I send it to the Third process to see if I have yet another match. However if the First/Second match is not confirmed by the Third process, then I need to respond to the Second to find another match.

      Sending the data from First, to Second, to Third works fine.

      My problem comes in being able to establish a connection "downstream" (Third, to Second, to First) using the same set of sockets that I originally used to send data "upstream". In short, I cannot seem to pick up a response from Third to Second, and then from Second to First.

      The code for my Third process is appended below - as you can see I am adding a newline at the end of the string before I send it back to the Second process - but for some reason the while(<$t_socket>){...} loop never executes - either it is indefinitely waiting on the <$t_socket> or it is skipping the loop altogether.

      use IO::Socket; use strict; use warnings; my $PORT = shift or die "Please specify First port number\n"; #my $file = shift or die "Please specify input file\n"; my $socket = IO::Socket::INET->new('LocalPort' => $PORT, 'Proto' => 'tcp', 'Listen' => 2) or die "Third: Can't create socket to Second($!)\n"; my $match = "brenna"; print "Third listening\n"; while (my $second = $socket->accept) { #setup acknowledgement message to Second my $host = gethostbyaddr($second->peeraddr, AF_INET); my $port = $second->peerport; while (<$second>) { my $name = $_; chomp($name); print "[$host $port] $name\n"; #print $socket "$.: $name\n"; if($name eq $match) { print "Third sending \"$name\" to Second\n"; print $socket "$name\n"; #close $second or die "Can't close ($!)\n"; } } } die "Third: Can't accept receive socket from Second($!)\n";
      Any help/advice you can offer to help me figure this out would be much appreciated!
        No you don't, or I wouldnt have suggested using it. recv works with a socket, no matter how it is connected.

        Does $name in fact equal $match? ie is the third one actually answering at all? Please try testing it using telnet, or give the data that you are using.

        C.