Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am definately a newbie here. My IO::Socket program listens for incoming messages, processes them and then waits for the next. My cliet program gets executed lots of times (it's a message processor driven by incoming events - so each new event is a new copy of the sender program) My question is what exactly happens when the client kicks twice in a very very short period of time. If I understand IO:Socket correctly, and I am using TCP, the second copy of the program will wait (or the data will be buffered) until the first copy of hte problem and thus the first buffer is processed. I have set Listen parameter to 5 and I perform Autoflush(1) on each receipt of a buffer. Code example available but I'm just asking for explanation on how it works. What I am seeing is the packets are on the wire for two events but the receviing program only sees one buffer. This is a very simple environment. The server runs "like" a daemon and the client executes each time a new messge is received. I only see squirrely results when the messages arrive very close together.

Replies are listed 'Best First'.
Re: Multiple clients to one server
by matija (Priest) on Apr 05, 2004 at 20:50 UTC
    I'm not quite sure why you say your program gets executed many times: the standard method of using IO::Socket is
    my $sock=IO::Socket::new(.....); while (my $conn=$sock->accept) { # do something with $conn }
    That loop is where every now connection is accepted, and it's up to you to manage fresh connections. If the second connection arrives while you are still processing your loop, the sync packet will be kept waiting. Once you call accept, the SYN-ACK packet will be sent to a client. If you wait too long, the TCP timout will take place, and the connection won't be established. Note that by default, accept blocks until there is a connection it can accept.

    On the other hand, the cases where your program gets executed many times are when your program is running from inetd - but in that case, your program would read and write STDIN and STDOUT and would not have to deal with IO::Socket.

      Thanks for the reply. Let me flush out the design. We use NetView network management system. It receives SNMP traps and automates actions. In this case, when a mainframe connection to one of our clients goes down, it can result in numerous of these traps be generated (between 1 and 5 usually, but sometimes more).

      NetView recieves and processes the trap - firing my client program once for each incoming trap. The client program opens a socket, sends the data (logs it) and terminates. The reciever program is always running, catches the incoming event and makes some business decisions on what to do with it (like logging and paging).

      The snoop I ran (solaris) indicates that the client program kicked the appropriate number of times, and sent the appropriate number of messages but the receiving program only reflects some of the messages. Here is an example:

      (client says he sent these events - might be ugly here)

      $MESSAGE = Mon Apr 5 22:34:56 2004|2|WOK|DOWN|
      $MESSAGE = Mon Apr 5 22:34:57 2004|2|Q20|DOWN|
      $MESSAGE = Mon Apr 5 22:34:57 2004|2|BYS|DOWN|
      $MESSAGE = Mon Apr 5 22:34:57 2004|2|BYU|DOWN|
      $MESSAGE = Mon Apr 5 22:34:58 2004|2|W7C|DOWN|
      $MESSAGE = Mon Apr 5 22:34:58 2004|2|W0I|DOWN|
      $MESSAGE = Mon Apr 5 22:34:58 2004|2|BCU|DOWN|
      $MESSAGE = Mon Apr 5 22:35:12 2004|2|F4A|DOWN|
      $MESSAGE = Mon Apr 5 22:34:58 2004|2|YEG|DOWN|
      $MESSAGE = Mon Apr 5 22:34:58 2004|2|BCM|DOWN|
      $MESSAGE = Mon Apr 5 22:34:58 2004|2|Y3A|DOWN|
      $MESSAGE = Mon Apr 5 22:35:56 2004|3|F4A|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:29 2004|3|Q20|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:31 2004|3|WOK|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:40 2004|3|BYU|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:40 2004|3|YEG|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:40 2004|3|BCM|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:49 2004|3|W0I|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:50 2004|3|W7C|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:50 2004|3|Y3A|ACTIVE|
      $MESSAGE = Mon Apr 5 22:36:50 2004|3|BYS|ACTIVE|
      $MESSAGE = Mon Apr 5 22:37:09 2004|3|BCU|ACTIVE|


      (receiver says he got these messages - but packet trace shows the senders all sent their packets)

      MON APR 5 22:34:56 2004 | <node name omitted> | WOK | DOWN | <customer name omitted> NORTH YORK
      MON APR 5 22:35:12 2004 | <node name omitted> | F4A | DOWN | <customer name omitted> DORVAL
      MON APR 5 22:35:56 2004 | <node name omitted> | F4A | ACTIVE | <customer name omitted> DORVAL
      MON APR 5 22:36:29 2004 | <node name omitted> | Q20 | ACTIVE | <customer name omitted> NORTH YORK
      MON APR 5 22:36:31 2004 | <node name omitted> | WOK | ACTIVE | <customer name omitted> NORTH YORK
      MON APR 5 22:36:40 2004 | <node name omitted> | BYU | ACTIVE | <customer name omitted> NORTH YORK
      MON APR 5 22:36:49 2004 | <node name omitted> | W0I | ACTIVE | <customer name omitted> NORTH YORK
      MON APR 5 22:37:09 2004 | <node name omitted> | BCU | ACTIVE | <customer name omitted> NORTH YORK

      Where my break down (mentally) is: Does the receiver actually have the capability to receive queued requests? Here is my IO:Socket code bit:
      do { use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '7070', Proto => 'tcp', Listen => 20, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; # $sock->autoflush(1); my $new_sock = $sock->accept(); $sock->autoflush(1); # -------------------------------------------------------------------- +----- # --- Now process the buffer + --- # -------------------------------------------------------------------- +----- while ($BUFFER = <$new_sock>) { do stuff here } else { #print "$ORIGIN Un-Processed Buffer: $BUFFER\n"; print ERR_DATA "$ORIGIN Un-Processed Buffer: $BUFFER\n"; } } close($sock);

      I've left a lot of the code out here for brevity.
Re: Multiple clients to one server
by Bolan (Initiate) on Apr 06, 2004 at 17:38 UTC
    Just a note, I am no longer an anonymous monk. My name here is Bolan :) Thanks in advance.
      I think this is my problem: http://www.perlfect.com/articles/select.shtml I'm guessing that I have a problem because of multiple incoming socket requests but really only one buffer to handle it based on what this article says. I'm really in a pickle here, anyone care to help out?
        It's fixed. Thanks for all your support.