hello monks,i have writen a code for a udp client,it just sends a packet to the server. the problem is that i have to press Enter/return two times to get the packet on the other side.and one more question, if we have data more than MSS of UDP packet which is 534 bytes, how do we send it? e.g. if the client requests a file, how do we send it back? any help is appreciated. the code of client:
#!/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";} }
and the server code

#!/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]"; } }

In reply to Sending problem in UDP by deewanagan

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.