in reply to sockets and such in Perl

Sockets is somewhat vague, anyway all sockets methods I know of are bidirectional. So, first of all, no need to have a couple of them - they're not pipes or fifos! This should half the complexity :)

Second, you probably have to decide who's the server and who's the client. Even if it may seem strange, I'd put the man-in-the-middle as a client, simply because... I forecast it will be simpler to code. A matter of taste and of forecasting powers, anyway.

This issue, tough physically completely independent on the following algorithm, is logically connected. How would you implement it inside a single process, reading from three files? Here you have much the same scenario, only with a bit of control logic to avoid reading more than you need. Again, the man-in-the-middle process will be the master driver, while the other two will act as slaves (i.e. servers).

The first and third process (i.e. the "endpoints"):

Open file Open socket in server mode Accept incoming request Loop: read a request from the socket if request is "stop" exit else read name from file and send back to the socket
The second follows the same logic as a "single-process" computation: each time the process wants to read from one of the "side" file, it sends a fake command (a simple newline will do) to the corresponding far process, then reads the answer as if it were reading from a file. The core algorithm does not need to be changed, as you can see.

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: sockets and such in Perl
by scotchfx (Sexton) on Jul 03, 2005 at 17:26 UTC
    CODE UPDATED!!!! Once again - thanks for all of your replies - I am grateful for all of your feedback and advice.

    I tried using each socket in the following bidirectional manner:

    my $s_socket = IO::Socket::INET-> new('LocalPort' => $S_PORT, 'Proto' => 'tcp', 'Listen' => 2) or die "Third: Can't create socket to Second($!)\n"; #then later on... print $s_socket "$match\n"; print scalar <$s_socket>;
    But could not get it to work... am I forgetting something here?

    I think I've tried to implement a similar algorithm - I will comment on this later in response to one of the other suggestions made to my original post. Basically, the issue is that there may be false positives (i.e. two files have the same name, but not the third). This forces me to have to return to the process that originated the initial "false match" and resume input from it until I find a match that occurs in all three files... more to come...

    Thanks again people... you all are lifesavers...

      You have to use the same socket for both send and receive. I suspect you're not issuing use strict and use warnings here. Why should a variable named $s_receive turn into $s_send?!? Moreover, the print scalar <$s_send> is really obscure - what do you want to obtain?

      Here's a skeleton for your application, but again you have to fill in the actual implementation of the algorithm. You can also find some example of bidirectional communication.

      #!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; + # Main socket, it's only for accepting connections my $sock = IO::Socket::INET->new(LocalPort => 10000, Proto => 'tcp', List +en => 2) or die "unable to initiate socket: $!"; print STDERR "ready to accept connections\n"; + # Working sockets, they're bidirectional connections towards each # single client, so they're to be used for actual communications my $conn1 = $sock->accept() or die "accept(): $!"; print STDERR "client 1 connected\n"; + my $conn3 = $sock->accept() or die "accept(): $!"; print STDERR "other client connected\n"; + # Core of algorithm should go here, just some examples of IO given # Read from client #1 my $rec1 = <$conn1>; # Send something to client #1 print $conn1 "Hey #1, I heard you saying [$rec1]\n"; # Read from the other client my $rec3 = <$conn3>; # Send something to it as well print $conn3 "You're there, #3... did you say [$rec3]?\n"; $_->close foreach ($conn3, $conn1, $sock);

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
        Frodo - my bad, I cut&paste'd the wrong contiguous sections of my original code. Earlier on I was trying to receive and send from the same socket, but when this started giving me problems I created a pair of individual receive and send ($s_receive and $s_send) respectively. I did try to implement this in a single socket initially, I just posted the updated code by accident.

        Sorry for the confusion (see my UPDATED example in the original reply).

        The following section was taken from an example I found online:

        print $s_socket "$match\n"; print scalar <$s_socket>;
        If it's unnecessary I can take it out - it worked so I didn't mess with it. What was the original purpose behind this "scalar" anyways?

        Regarding the example you provided me, I have one primary question:

        If I use a single socket for two different clients, can I still distinguish between which client I am connected to? What if my Third process connects to $conn1, and my First to $conn3 just by the order in which they happen to reach the Second process?

        Is it better to setup individual ports/connections/sockets for two different clients, and then use those individual ports in a bidirectional manner?

        One other thing I'd like to ask: Given that these processes will likely all be on the same machine - is there a better means of IPC to use other than sockets?

        Sorry again about mangled code - I've been up for awhile:)