Wise Monks,

I have a simple problem (I think). I would like to be actively listening on a UDP port (server) and be able to send to specified UDP ports (client) based on <STDIN>.

Code below works on unix and linux, but on Win32 (ActiveState 5.8.0 805, Win2k) the following code seems to block no matter what I try on the $sock->recv. This prevents any $sock->send from the child (why is that?).

Advice, code, gladly accepted (I've tried to implement different iterations on forking, low-level socket defines and IO::Select for 20+ hours, but no luck)!!

#!/usr/bin/perl # biclient - bidirectional forking client perl_book\cookbook\ch17_11 +.htm use IO::Socket; use English; use strict; my ($lport, $host, $port, $kidpid, $sock, $line, $msg, $MAXLEN); $MAXLEN = 1024; unless (@ARGV == 3) { die "usage: $0 listen_port dest_host dest_port\ +n" } ($lport, $host, $port) = @ARGV; # create a udp connection to the specified host and port $sock = IO::Socket::INET->new(Proto => "udp", LocalPort => $lport, PeerAddr => $host, PeerPort => $port) or die "can't connect to port $port on $host: $!"; print STDOUT "[Listening on $lport, Connected to ($host:$port)]\n"; # split the program into two processes, identical twins die "can't fork: $!" unless defined($kidpid = fork()); if ($kidpid) { print STDOUT "Parent $PID started\n"; # parent copies the socket to standard output while ($sock->recv($msg, $MAXLEN)) { print "\nReceived from ($host:$port) \"$msg\" \n"; } kill("TERM" => $kidpid); # send SIGTERM to child } else { print STDOUT "Child $PID started\n"; print $sock "Child $PID started"; # child copies standard input to the socket while ($line = <STDIN>) { chomp($line); #print STDOUT "You entered: $line\n"; print $sock $line; } } exit;

Also, it seems that once a connection is made from perl program to another, I cannot connect another perl program to the same socket (ie. listen in on the datagrams). Is this part of the design, or can this be worked-around?

Thanks in Advance!!
Steve


In reply to UDP bidirectional client by sbrandt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.