in reply to Re: Re: UDP bidirectional client
in thread UDP bidirectional client

pg is on the right track when he says to use two sockets. You don't want to use the same socket to both send and receive over UDP. The code from the cookbook is for TCP sockets and won't work with UDP. If you want to use UDP, here is your code changed to use two sockets:
#!/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 my $in = IO::Socket::INET->new(Proto => "udp", LocalPort => $lport ) or die "can't connect to port $lport on localhost: $!"; my $out = IO::Socket::INET->new(Proto => "udp", PeerAddr => $host, PeerPort => $port) or die "can't connect to port $port on $host: $!"; print STDOUT "[Listening on $lport, Connected to ($host:$port)]\n"; # Thelonius notes: Actually, with UDP, we # are NOT CONNECTED to ($host:$port). # However, our messages will be sent there # 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 ($in->recv($msg, $MAXLEN)) { print "\nReceived from ($host:$port) \"$msg\" \n"; # Thelonius notes: You do not know where # the message came from unless you use # recvfrom. } # This loop will never exit. kill("TERM" => $kidpid); # send SIGTERM to child } else { print STDOUT "Child $PID started\n"; print $out "Child $PID started"; # child copies standard input to the socket while ($line = <STDIN>) { chomp($line); #print STDOUT "You entered: $line\n"; print $out $line; } # You might want to kill parent if you exit. } exit;

Replies are listed 'Best First'.
Re: Re: Re: Re: UDP bidirectional client
by sbrandt (Initiate) on Mar 06, 2003 at 21:36 UTC
    Thanks Thelonius - but isn't there a fundamental flaw in that approach? You are sending on based on some kernel chosen port, and then listening for replies on a specified port.

    How does the peer know which port the sender is listening on?

    That's why I was trying to use one socket object. I hope that will work, or I don't know how to listen for what I sent?

      You probably want to use TCP.
Re: Re: Re: UDP bidirectional client
by sbrandt (Initiate) on Mar 07, 2003 at 13:41 UTC
    Thanks Thelonius - but isn't there a fundamental flaw in that approach? You are sending on based on some kernel chosen port, and then listening for replies on a specified port.

    How does the peer know which port the sender is listening on?

    That's why I was trying to use one socket object. I hope that will work, or I don't know how to listen for what I sent?