http://www.perlmonks.org?node_id=968837


in reply to Re^6: UDP connection
in thread UDP connection

Can you not see, from your own posting, that there are four pairs of ports, and each pair has to communicate in both directions?
No.

I've listed the 8 data streams I've seen described by the OP. They use ports 8020, 8019, 8008, 8003 to send from, and ports 53036, 53037, 53038, 53039 to listen to. I haven't seen the OP describe traffic originating from ports 53036, 53037, 53038, 53039.

But whether or not there's bidirectional UPD traffic, that's irrelevant. I still don't see 2 processes trying to use the same port. Can you point out a port, and the two processes that need to access it?

It is implied by the OPs description.
Why?
The pre-existing server might be,
Most services restrict UDP packets to be at most 512 bytes. But even if they don't, UDP packets are restricted to be at most 65535 bytes (as the length field of the header is 2 bytes large), leaving slightly less for the payload. I just haven't assumed the OP runs hardware that needs more than 10 seconds to copy 65535 bytes from the IP stack to the process' memory.

Replies are listed 'Best First'.
Re^8: UDP connection
by BrowserUk (Patriarch) on May 04, 2012 at 01:41 UTC
    I haven't seen the OP describe traffic originating from ports 53036, 53037, 53038, 53039.

    Geez Louise. Re-read the OPs post but concentrate on a single pair of ports:

    1. The server listens on 4 ports 8020 ....
    2. The client sends the first heartbeat from ports: 53036, ... to ports 8020, ..., This repeats in every 10 seconds.
    3. When server ... [it] replies from ports 8020, ... to ports 53036, ....
    4. If ... the server ... sends them to ports 53036, ....

    Graphically:

    step | Action | Server | direction | client -----+--------+--------+-----------+------- 1 | Listen | 8020 | | -----+--------+--------+-----------+------- 2 | h'beat | 8020 | <-------- | 53036 Client 53036 originates; S +erver 8020 receives. -----+--------+--------+-----------+------- 3 | resp | 8020 | --------> | 53036 Server 8020 originates; cl +ient 53036 receives. -----+--------+--------+-----------+------- 4 | data | 8020 | --------> | 53036 Server 8020 originates; cl +ient 53036 receives. -----+--------+--------+-----------+-------

    4 ports at the server, 4 ports at the client; 4 bi-directional conversations. There is no ambiguity.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      You are correct. I must have misread the numbers.

      However, there is no problem. Here are a sample server and client that do bidirection communication over 4 socket pairs. The server waits for an initial heartbeat from the client, then starts sending data, and it will stop sending data if it hasn't seen a heartbeat for 10 seconds. There's of course lots of room for improvement, but it does show bidirectional communication is not a problem.

      First, the server:

      #!/usr/bin/perl use 5.010; use strict; use warnings; use IO::Socket; my $INTERVAL = 10; my $MAX_BUFSIZE = 512; my $NO_CONTACT = 0; my $SENDING = 1; my $LOST_CONTACT = 2; my $server = '192.168.0.1'; my @server_ports = (8020, 8019, 8008, 8003); my @sockets = map { my $socket = IO::Socket::INET::->new( LocalPort => $_, LocalAddr => $server, Proto => 'udp', ) or die $!; $socket; } @server_ports; my @states = ($NO_CONTACT) x @sockets; my @heartbeats = (0) x @sockets; my @counts = (0) x @sockets; my $Rbits = ""; my $Ebits = ""; vec($Rbits, fileno($_), 1) = 1 for @sockets; vec($Ebits, fileno($_), 1) = 1 for @sockets; while (1) { select(my $rbits = $Rbits, undef, my $ebits = $Ebits, 1); for (my $i = 0; $i < @sockets; $i ++) { my $socket = $sockets[$i]; my $fileno = fileno($socket); my $state = $states[$i]; my $heartbeat = $heartbeats[$i]; if (vec($ebits, $fileno, 1)) { say "Got an error on channel $i. I'm out of here"; exit 1; } if (vec($rbits, $fileno, 1)) { my $sender = $socket->recv(my $buffer, $MAX_BUFSIZE); if (length $buffer) { my ($port, $remote) = sockaddr_in($sender); $remote = inet_ntoa($remote); say "Got HB from $remote:$port"; $heartbeat = $heartbeats[$i] = time; if ($state == $NO_CONTACT) { # # Upgrade the socket now we know the remote # vec($Rbits, fileno($socket), 1) = 0; vec($Ebits, fileno($socket), 1) = 0; undef $sockets[$i]; undef $socket; $socket = $sockets[$i] = IO::Socket::INET::->new( LocalPort => $server_ports[$i], LocalAddr => $server, PeerAddr => $remote, PeerPort => $port, Proto => 'udp', ) or die $!; vec($Rbits, fileno($socket), 1) = 1; vec($Ebits, fileno($socket), 1) = 1; $state = $states[$i] = $SENDING; } } } # # Out of time? # if ($state == $SENDING && $heartbeat + $INTERVAL < time) { say "Channel $i is dead. Bye!"; exit 1; } # # Send data? # if ($state == $SENDING) { foreach (1 .. int rand 10) { my $count = ++$counts[$i]; say "Send packet $count on channel $i"; $socket->send($count); } } } } __END__
      And the client:
      #!/usr/bin/perl use 5.010; use strict; use warnings; use autodie; use IO::Socket; my $server = '192.168.0.1'; my $client = '192.168.0.12'; my @server_ports = ( 8020, 8019, 8008, 8003); my @client_ports = (53036, 53037, 53038, 53039); my $INTERVAL = 10; my $MAX_BUFSIZE = 512; my @sockets; for (my $i = 0; $i < @server_ports; $i ++) { $sockets[$i] = IO::Socket::INET::->new( LocalPort => $client_ports[$i], LocalAddr => $client, PeerPort => $server_ports[$i], PeerAddr => $server, Proto => 'udp', ) or die $!; } my $Rbits = ""; my $Ebits = ""; vec($Rbits, fileno($_), 1) = 1 for @sockets; vec($Ebits, fileno($_), 1) = 1 for @sockets; # # Now loop. If there's data to read, read it. If it's time to send a # heartbeat, do so. # my @heartbeats = (0) x @sockets; while (1) { for (my $i = 0; $i < @sockets; $i++) { if (time >= $heartbeats[$i] + $INTERVAL - 1) { # # Initialize contact, or keep alive # say "Send HB on channel $i"; $sockets[$i]->send("HB"); $heartbeats[$i] = time; } } select(my $rbits = $Rbits, undef, my $ebits = $Ebits, 1); for (my $i = 0; $i < @sockets; $i++) { my $socket = $sockets[$i]; my $fileno = fileno($socket); if (vec($ebits, $fileno, 1)) { say "Got an error on channel $i. I'm out of here"; exit 1; } if (vec($rbits, $fileno, 1)) { # # Read messages, if any # $socket->recv(my $buffer, $MAX_BUFSIZE); next unless length $buffer; say "Received packet $buffer on channel $i"; } } } __END__
        You are correct. I must have misread the numbers.

        It happens. Thank you for acknowledging it.

        However, there is no problem. Here are a sample server and client that do bidirection communication over 4 socket pairs.

        Hm. There are still problems you are not dealing with. I'm not talk ing about error handling or niceties here.

        1. Your server is not acknowledging (responding to) the clients heartbeats per the OPs description:
          And once it receives a "heartbeat" (a byte containing "01") it sends a response to the IP and port the hb came from (byte containing "25").

          It may sound a minor omission, but (I believe) it does complicate the design of the client (and server) somewhat.

        2. Your server is using the receipt of the heartbeat as a trigger for the start of server to client data flow.

          In the OPs description:

          • the hexdumps are only sent after the initial heartbeat.

            (Obviously, how else would the server know there was a client to send to:)

          • But they are not triggered by the heartbeat!

            He goes on to say:

            If there are test results ready on the server it sends them to ....

            Which suggests, though it doesn't actually state -- and he never came back to answer the question -- that the sending of the hexdumps is initiated by the server, when they become available; provided that is within 10 seconds of a heartbeat.

        The significance of those (perhaps apparently small) differences, is that your client treats all inbound communications as data. The OP cannot do this as he has also to distinguish between the heartbeat response and the actual data.

        The single byte packet size of the response should make that easy... except when you take the uncertain timing of the availability of the hexdump data into consideration.

        Here is the black hole in the timing scenario I was trying resolve with the OP, which your client does not -- and (I believe) as written, cannot -- resolve:

        time ascending relative ... 0.000 At this point, a client has (just) sent a HB, and the server has ac +knowledged it. The server had no data to send ... 9.99999... seconds expire The server discovers that it has hexdump to send and starts transmitt +ing ... 10.000 The client sends it next heartbeat and awaits the response.

        The hexdump doesn't have to be large, a single packet coming available (at exactly the wrong time) will trigger the problem. The client is expecting a single byte response. The server hasn't seen the heartbeat, so it isn't going to send it until (at least) after it finishes sending the current packet. If, after sending the heartbeat, the client went into a read state for the single byte response, it will get the first byte of the data packet, and the rest will be discarded. This is the exact scenario that the OP described in his first post.

        Using blocking reads -- as would normally be used in conjunction with threads as indicated by the OP -- the client cannot be in a read state in anticipation of the arrival of a data packet that could come at any time; and also send a regular heartbeat, because you cannot send() via socket, if that socket is currently in a recv() state. (At the same end!)

        Using select obviates that, by avoiding entering the read state until it knows (by polling) that data is ready to be read. But that alone does not completely close the window for failure.

        If the 10 second timeout at the server occurs exactly whilst the client is in the process of receiving the latest packet -- and is therefore unable to transmit its next heartbeat -- the server will (may) conclude that the client has 'gone away' and discard (fail to send) any subsequent data until the client re-heartbeats. And that leads to data loss, which the OP explicitly denounced.

        Whilst the window for such a failure is small; such are the nature of communications protocol failures.

        The usual solution(s) to this problem include:

        1. Have the client send the heartbeat at twice the frequency of the servers timeout.
        2. Have the server declare a timeout of half what it will actually accept.
        3. Have the server reset the timeout whenever it sends -- be it heartbeat ack; or data packet -- rather than when it receives.

        And it was these details I was trying to establish with the OP before he got scared away by our ...um... discussion.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?