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

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:)

Replies are listed 'Best First'.
Re^5: sockets and such in Perl
by polettix (Vicar) on Jul 04, 2005 at 10:06 UTC
    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.