shekarkcb has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

Sorry to reposting the similar question.
I was going through socket programing, got a doubt, what is the difference between udp client and server? Suppose if i need to listen in my machine for a perticular port (40150 - which will have x amounts of udp packets coming in from a server - direct push) how can i code this on Perl?
i have written this code

# $IP will have the actual ip from where packets are coming in system(" tcpdump -v -i eth1 dst $IP > file.txt & "); my $GREP=`ps aux | grep 'tcpdump ' | grep -v grep | awk '{pri +nt \$2}'`; chomp($GREP); print "PID for |$IP| IS -->|$GREP|\n"; print "SLEEEPING FOR 5 seconds....\n"; sleep(5); chomp($GREP); print "KILLING PROCESS ID ------->|$GREP|\n"; system("kill -9 $GREP"); my $LINES=`wc -l $path\/file.txt | awk '{ print \$1}'`; chomp($LINES); if($LINES > 5) { # I have ip coming in } else { # Looks like ip with udp packets is not coming in } }

This subroutine tests given ip is listed on tcpdump, It works great, but i don't want to use tcpdump or any unix related tools (awk, grepm, kill etc). Looks like this is using huge amount of memory. Is there any way i can write this using socket programing?
Any hint ???

Thanks.

Replies are listed 'Best First'.
Re: What is the difference between udp client and server?
by locked_user sundialsvc4 (Abbot) on Nov 10, 2010 at 13:35 UTC

    “The difference between a client and a server” has nothing to do with “UDP.”   Rather, it has to do with the role that each program plays, in relation to one another.

    A “server” is any computer program that is primarily designed to talk to other computer programs, rather than to people.

    “Servers” often work by listening on network connections ... TCP/IP or UDP ... but some internal-only servers use other techniques such as named pipes.   Many commercial servers support multiple modes of communication.

    Within CPAN, you will find substantial building-blocks for constructing server programs.   If you look hard enough before “just diving in,” you’ll find that a great deal of your work has been done for you.   The code is, by and large, very high-performance, reliable, and well tested.

Re: What is the difference between udp client and server?
by chrestomanci (Priest) on Nov 10, 2010 at 09:17 UTC

    With UDP sockets, the server receves packets, and the client sends them

    If you can get hold of a copy, then you should take a look at the examples in Perl Cookbook (O'Reilly). Otherwise, take a look at IO::Socket::INET in CPAN.

    Here is a quick example:

    Client:

    my $sendSock = IO::Socket::INET->new( 'Proto' => 'udp', 'PeerPort' => $udp_port, 'PeerAddr' => $hostname, ) or die("Error creating socket $!"); $sendSock->send($data) or die("Socket send error $!");

    Server:

    my $listenSock = IO::Socket::INET->new('LocalPort'=>$udp_port, 'Proto' +=>'udp'); my $readBuff; $listenSock->recv( $readBuff, $MAX_BYTES );

    Note that the server above will block until some data is received.

      With UDP sockets, the server receves packets, and the client sends them

      ow! That's not true. For starters, UDP doesn't know anything of client and server. sundialsvc4 has a great answer you should read.

      Both the server and the client can both send and receive. Both the client and server almost always both send and receive. (Conversations are generally variations of the following: The client sends a request, the server receives a request, the server sends a response, the client receives a response.)

Re: What is the difference between udp client and server?
by JavaFan (Canon) on Nov 10, 2010 at 15:33 UTC
    I wouldn't go and mimic tcpdump in Perl. It's not going to be a trivial task.

    But I would just fork (using a pipe-open), exec tcpdump in the child, read in the parent. The parent can count the number of lines seen - if it has seen 6, you have an IP coming else, otherwise, not. A standard alarm/die/eval trick will get you the 5 second time out. Still need the tcpdump that way, but no external ps, grep, awk, kill, or wc. And won't wait 5 seconds even if you saw the ip address after 2.

Re: What is the difference between udp client and server?
by andal (Hermit) on Nov 10, 2010 at 09:21 UTC

    Well, it is not clear what is your question. Do you want to know the difference between the UDP client and server, or just want to know how to find the address of the UDP datagram sender? :) If the latter, then use "recv" to read the datagrams. This function returns the address of the datagram sender.

    Check "perldoc -f recv" and "perldoc perlipc" for more information.