UPDATE:

I am having issues being able to accept() a connection from a process on a socket that I established (and written to) at a previous point in my script... in an nutshell, 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.

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 get my $socket->accept to pick up the response from Third to Second, and then from Second to First.

Below is a chunk of code that I've managed to isolate as the hangup in my project:

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"; #Sending info upstream to Third process #this part works fine if($name eq $match) { print "Second found match: $name\n"; print "Second sending \"$name\" to Third\n"; print $t_socket "$match\n"; } #after I send I am expecting a response #from Third process #I never get into this loop #it skips or accept() blocks indefinitely while(my $third = $t_socket->accept) { print "Second has accepted socket to Third...\n"; my $t_host = gethostbyaddr($third->peeraddr, AF_INET); my $t_port = $third->peerport; while (<$third>) { my $name = $_; chomp($name); print "Received from [$f_host $f_port]: $name\n"; if($name eq $match) { print "Received \"$name\" from Third\n"; last; } else { last; } } } }
Here is the sending part of the Third process:
if($name eq $match) { print "Third sending \"$name\" to Second\n"; print $socket "$name\n"; #close $second or die "Can't close ($!)\n"; last; }
This code seems to execute okay.

I'd be happy to include all three scripts if anyone is interested to look. I'm doing this for a class project (see http://www.perlmonks.org/?node_id=472004 for a better description).

#####PREVIOUS QUESTION#####

I have a quick IO::Socket question for those of you out there familiar w/ Perl IPC.

What is the difference between opening a socket with "LocalPort" as opposed to "PeerPort" (see code example below):

my $socket = IO::Socket::INET->new('LocalPort' => $PORT, 'Proto' => 'tcp', 'Listen' => 2) or die "Second: Can't create socket to First($!)\n"; #VERSUS my $socket = IO::Socket::INET-> new( 'PeerAddr' => "localhost", 'PeerPort' => $PORT, 'Proto' => 'tcp') or die "Second: Can't create socket to Third($!)\n";
In this instance, can each of these $socket be used to write or read from? Is the former only for listening, and the latter for sending or can I use both sockets equally?

Can I only do a $socket->accept on the former example, but not the latter?

This leads me to my next question... what is the reason behind doing the following:

while(my $handle = $socket->accept) { while(<$handle>) { #do something with $_ } }
Is this _only_ for receiving input from a socket via a handle, or can I use it to write to as well - some examples on the web use code that writes directly to the $handle ex) "print $first "hello"; ... is this correct form?

If I need to send a message across the socket, should I instead use print $socket "my message"; ? Should $handle = $socket->accept; only be used when listening on a socket?


In reply to Bidirectional use of a Socket 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.