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!


In reply to Read/Write Socket question (Bidirectional my ass...) by scotchfx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.