deewanagan has asked for the wisdom of the Perl Monks concerning the following question:
and the server code#!/usr/bin/perl use IO::Socket; my $sock = &rdt_connect($ARGV[0],$ARGV[1]); $msg = "get $ARGV[2]"; $sendmsg = &rdt_send($msg); print length($sendmsg); &sendall($sock,$sendmsg); sub rdt_connect { #takes necessary parameters, creates socket and connects to that c +lient. my ($serverconnect,$portconnect) = @_; $proto = 'udp'; $socket = IO::Socket::INET->new (PeerAddr=>$_[0],PeerPort=>$_[1],P +roto=>'udp') or die "Can't Connect $!"; return $socket; } sub rdt_send{ #makes packet and sends it to the socket. my $datatosend = @_; $lengthpayload = length($_[0]); $r = 'R';$d='D';$t='T';$one=1;$point = '.';$zero=0;$null1=00000000 +;$null2=00000000;$null3=00000000;$null4=00000000;$null5=00000000;$nul +l6=00000000;$null7=00000000; $protocol = pack("a"x6,$r,$d,$t,$one,$point,$zero); $packetheader = pack('N3 n N3 n',$null1,$null2,$null3,$lengthpaylo +ad,$null4,$null5,$null6,$null7); #$packetpayload = $packetheader.$datatosend; $packetpayload = pack('a*',$_[0]); $tobuffer = ($protocol.$packetheader.$packetpayload); return $tobuffer; } sub sendall{ my ($socket,$packet) = @_; if ($packet ne ''){$_[0]->send($_[1]) or die "Send error: $!\n";} }
#!/usr/bin/perl -w use IO::Socket; ($server,$port1) = &rdt_listen($ARGV[0]); print"LISTENING ON PORT $port1\n"; $serverhasdata = &select($server); ($thehost,$request) = &rcv_from($serverhasdata); $thedata = &statmachine_send($request); @payloadwoh = &statemachine_rcv($thedata); @payld = &rdt_recv(@payloadwoh); @payloadwoh = split(/\t/,$line); print"u have got a request from:$thehost and the request is:@paylo +adwoh\n\n"; sub rdt_listen{ #takes necessary parameters creates a socket and listens on that p +ort. my $port1 = $_[0]; $protocol = 'udp'; $socket1 = IO::Socket::INET->new(LocalPort=>$port1,Proto=>$protoc +ol)or die "Cannot Listen On The Socket $@"; return $socket1,$port1; } sub select{ #it selects the socket(socket is given as a parameter) and receive +s data from that socket my ($rcvsocket) = @_; $_[0]->recv($buf,534); if ( length($buf) != 0 ){ return $_[0]; } } sub rcv_from{ #takes data from the socket which is selected by the select functi +on my $sock = @_; while($_[0]->recv($msg, 534)){ my($port, $ipaddr) = sockaddr_in($_[0]->peername); $hishost = gethostbyaddr($ipaddr,AF_INET); return ($hishost,$msg); } } sub statmachine_send { #takes data from rcv_from and returns it my $comingdata = @_; return $_[0]; } sub statemachine_rcv { #it takes the packet from the statmachine_send function; my $packet = @_; #unpack the packet my($r,$d,$t,$one,$point,$zero,$null1,$null2,$null3,$lengthpayload, +$null4,$null5,$null6,$null7) = unpack('a3 C3 N3 n N3 n',$_[0]); $lengthpayload1 = $lengthpayload; @payload = unpack('a*',substr($_[0],34,$lengthpayload1+34)); #check if payload contains data or its empty if ( length(@payload) != 0){ return @payload; }else { return ;} } sub rdt_recv{ #it reads data from recive buffer. my @read = $_[0]; while (@read){ return "$_[0]"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sending problem in UDP
by gone2015 (Deacon) on Dec 05, 2008 at 10:15 UTC | |
|
Re: Sending problem in UDP
by quester (Vicar) on Dec 05, 2008 at 08:06 UTC | |
|
Re: Sending problem in UDP
by zwon (Abbot) on Dec 05, 2008 at 18:58 UTC |