in reply to Re^2: sockets and such in Perl
in thread sockets and such in Perl

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.

Replies are listed 'Best First'.
Re^4: sockets and such in Perl
by scotchfx (Sexton) on Jul 03, 2005 at 18:30 UTC
    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:)

      No problem, anyone can make a mistake.
      What was the original purpose behind this "scalar" anyways?
      The print function forces list contexts on its arguments, but I think that example you were using wanted to take a single line from the socket and print it out. If you did not put scalar, the list context would have sucked all the input, and probably hang waiting for the peer to close communication (which triggers an EOF). You should substitute the print with some kind of grab in real world usage, unless you only want to print the data from the peer on the screen. To this extent, use my examples.

      As for differencing the clients, you can use the accept method in list context to get the peer address, as of IO::Socket documentation; something like:

      my ($conn1, $peer1) = $sock->accept(); die "accept: $!" unless $conn1; # Now $peer1 tells you who is connected to $conn1
      .

      Update: sorry, I forgot about the closing question. Generally speaking, if all processes are on the same machine you could use multithreading to ease (and speed up) communications. There exist methods to share memory between threads, which makes communication as easy as reading and writing a variable... if you pay attention to synchronise the accesses to it :). But now I have a question: I thought you have to do this exercise only to test sockets, if you need it in the real world why don't you open the three files in a single process?!?

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

      Don't fool yourself.