Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: UDP connection

by JavaFan (Canon)
on May 03, 2012 at 09:30 UTC ( #968688=note: print w/ replies, xml ) Need Help??


in reply to UDP connection

Unlike some others in this thread, I don't think it's impossible to use UDP to send data in two directions: a heartbeat from the client to the server, and data from the server to the client. In fact, I don't see any particular difficulties -- with IP over 30 years old, we have learned to distinguish between source and destination addresses in IP packets (not that this ever was a problem).

You want to do something like the following (untested pseudo code):

... create sockets ... my @fileno = (fileno(SOCKET1), fileno(SOCKET2), fileno(SOCKET3), fileno(SOCKET4)); my ($rbits, $ebits); vec($rbits, $_, 1) = 1 for @fileno; vec($ebits, $_, 1) = 1 for @fileno; while (1) { my $time_to_next_heartbeat = ...; # How long till we have to send + a hb if ($time_to_next_heartbeat <= 0) { ... send heartbeats ... redo; } if (select (my $rbits = $rbits, undef, my $ebits = $ebits, $time_t +o_next_heartbeat)) { if ($ebits) { ... deal with errors ... } if ($rbits) { foreach my $fileno (@fileno) { if (vec($rbits, $filen0, 1)) { ... read data ... } } } } }
There's a much simpler solution though, and that may work for you:
  • Start 4 processes. Each process sends a heart beat every 10 seconds (one process for each of the ports).
  • Start another 4 processes. Each of them reads (and logs) whatever it gets from a particular socket.


Comment on Re: UDP connection
Download Code
Re^2: UDP connection
by BrowserUk (Pope) on May 03, 2012 at 14:02 UTC
    untested pseudo code

    If a read is in progress when the 10 seconds times out, then the heartbeat will be delayed until the read completes.

    much simpler solution though

    Two processes will be able to send to the same remote port 'simultaneously' -- thought the tcpip stack will serialise those communications.

    But can two concurrent processes talk from the same local port concurrently?

    Neither scenario seems to fit the OPs exact description does it!


    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?

      If a read is in progress when the 10 seconds times out, then the heartbeat will be delayed until the read completes.
      Sure, and if that's a problem, send it after 9.8 seconds, or some other interval. Read in small chuncks if that's required. Or just use a separate process.
      But can two concurrent processes talk from the same local port concurrently?
      Whether or not that's possible is irrelevant (however, this is UDP we're talking about, it's isn't about armies of ogres having to exit a tower through a narrow gate), as that is not what the OP seems to be doing. To quote him:
      The client sends the first heartbeat from ports: 53036, 53037, 53038, 53039 to ports 8020, 8019, 8008, 8003. This repeats in every 10 seconds.
      Oh, and do note that in my sketch, there's just one program anyway.

        Oh dear, here we go again.

        1. send it after 9.8 seconds,

          1) That's not the "every 10 seconds" called for.

        2. Read in small chuncks ...

          To quote you "this is UDP we're talking about,".

          And to quote from the man page that you should probably review before giving your next knee-jerk response:

          If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received

          Use short reads with UDP and data gets thrown way. Something the OP took pains to explain he cannot allow to happen.

        3. Or just use a separate process.

          And if these are two separate processes, how will the heartbeat process know when the read process is between reads?

        Three ideas, that it takes just a few seconds to dismiss as unworkable.

        Whether or not that's possible is irrelevant ... that is not what the OP seems to be doing. To quote him: ...

        You quote only section 2 of the OPs response to my questions.

        Selective amnesia; or did ADHD kick in before you got to section 3?

        Let's quote him a little more:

        2) The client sends the first heartbeat from ports: 53036, 53037, 53038, 53039 to ports 8020, 8019, 8008, 8003. This repeats in every 10 seconds.

        3) When server receives them replies from ports 8020, 8019, 8008, 8003 to ports 53036, 53037, 53038, 53039.

        See it? The source/destination pairs are the same in both directions (Ie. for the client to server heartbeats and the server to client transfers) which makes the question of whether two local processes can use the same local port concurrently very relevant!

        Oh, and do note that in my sketch, there's just one program anyway.

        You made two suggestions in that post, the second of which used multiple processes, with separate processes for the heartbeats and data transfers.

        And both of which cannot possibly work for the OPs requirements as described.


        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968688]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-08-22 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls