Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

UDP Client and Server in one

by mhirschb (Initiate)
on Mar 05, 2017 at 21:58 UTC ( [id://1183721]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I should write a thin and easy emulation of a networked device which is controlled by simple UDP commands. The device listens to UDP port 7070 and if a request arrives, it immediately responses with some status data.

I solved this easily with
use IO::Socket; my $PORTNO = 7090; $sock = IO::Socket::INET->new(LocalPort => $PORTNO, Proto => 'udp') while ($sock->recv($request, $MAXLEN)) { $response=parseRequest($request); $sock->send($response); }

My problem is, that this box should also send time-triggered status notifications every five seconds. Additionaly, these "five seconds" and the content of the status notifications are modified by the incoming request.

Currently, I have no clear idea, how the solution might look like. Any hint?

Thanks for your time!

Replies are listed 'Best First'.
Re: UDP Client and Server in one
by NetWallah (Canon) on Mar 06, 2017 at 07:08 UTC
    Here is one way to do it:

    * Make your "recv" non-blocking, with a 5-second timeout
    * Write to your socket with content based on the most recent successful recv

    You will likely run into trouble implementing this - once you have some code, post back here, and we can help.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Re: UDP Client and Server in one
by tybalt89 (Monsignor) on Mar 06, 2017 at 22:00 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1183721 use strict; use warnings; use IO::Socket; use IO::Select; use Time::HiRes 'time'; @ARGV and $ARGV[0] eq 'tester' and do_test_routine(); my $from; my $PORTNO = 7090; my $timerinterval = 5; my $timedresponse = "default timed response\n"; my $MAXLEN = 512; my $sock = IO::Socket::INET->new(LocalPort => $PORTNO, Proto => 'udp') or die $@; my $sel = IO::Select->new($sock); my $nexttimer = time + $timerinterval; for(;;) { my $timeout = $nexttimer - time; if( $timeout <= 0 ) { warn "timed out @{[int time - $^T]}\n"; if( defined $from ) #send time-triggered status { $sock->send($timedresponse, 0, $from); } $nexttimer = $timerinterval + time; } else { for my $fh ( $sel->can_read($timeout) ) { $from = $fh->recv(my $request, $MAXLEN); my $response = parseRequest($request); $fh->send($response, 0, $from); } } } sub parseRequest { warn "got @_"; return "canned response\n"; } sub do_test_routine # for testing purposes only { my $sock = IO::Socket::INET->new(LocalPort => 7091, Proto => 'udp', PeerPort => 7090, PeerHost => 'localhost', ) or die $@; $sock->send("a test message\n"); for(;;) { $sock->recv(my $buf, 512); warn "tester got: $buf"; } }

    semi-tested :)

Re: UDP Client and Server in one
by thanos1983 (Parson) on Mar 06, 2017 at 09:06 UTC

    Hello mhirschb,

    I just wanted to add something here regarding your title of your question.

    You are writting UDP Client and Server in one. From my point of view if I understand correctly you think that Server is receiving your messages and client is replying back.

    If this is the case it would be nice to read a similar question What is the difference between udp client and server?.

    Answer given by sundialsvc4 from the question:

    “The difference between a client and a server” has nothing to do with “UDP.” Rather, it has to do with the role that each program plays, in relation to one another. A “server” is any computer program that is primarily designed to talk to other computer programs, rather than to people. “Servers” often work by listening on network connections ... TCP/IP or UDP ... but some internal-only servers use other techniques such as named pipes. Many commercial servers support multiple modes of communication. Within CPAN, you will find substantial building-blocks for constructing server programs. If you look hard enough before “just diving in,” you’ll find that a great deal of your work has been done for you. The code is, by and large, very high-performance, reliable, and well tested.

    I hope this clears things a bit, as NetWallah said start implementing your solution and repost and we will be more than happy to assist you with your solution.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1183721]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-19 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found