#!/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:@payloadwoh\n\n"; sub rdt_listen{ #takes necessary parameters creates a socket and listens on that port. my $port1 = $_[0]; $protocol = 'udp'; $socket1 = IO::Socket::INET->new(LocalPort=>$port1,Proto=>$protocol)or die "Cannot Listen On The Socket $@"; return $socket1,$port1; } sub select{ #it selects the socket(socket is given as a parameter) and receives 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 function 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]"; } }