?
Solved

Perl Sockets - Raw Data

Posted on 2010-04-07
2
Medium Priority
?
460 Views
Last Modified: 2012-06-21
I am using a modified sample script to capture udp packets on a single port and then send the data back to the sender. This seems to work fine. My issue is that the payload being sent seems to be converted to ascii. Anything that I have tried converts the data to ascii then back to hex, something gets lost in translation. What I need is the original message as it is sent.  Thanks

Sample packet data

8305123100138201010102034c4b28ead64b28ead618a74dd4d422467a00003c4d0000002101170620001affab0f280f0005030200000000000000c594




#!/usr/bin/perl -w
# udpqotd - UDP message server
use strict;
use IO::Socket;

my($sock, $newmsg, $hisaddr, $MAXLEN, $PORTNO);
$MAXLEN = 1024;
$PORTNO = 21210;
$sock = IO::Socket::INET->new(LocalPort => $PORTNO, Proto => 'udp' )
    or die "socket: $@";
print "Awaiting UDP messages on port $PORTNO\n";

while ($sock->recv($newmsg, $MAXLEN)) {
    my($port, $ipaddr) = sockaddr_in($sock->peername);

    print "This: $newmsg \n" ;
    $sock->send($newmsg);
}
die "recv: $!";

Open in new window

0
Comment
Question by:altobravo
1 Comment
 
LVL 10

Accepted Solution

by:
Tobias earned 2000 total points
ID: 31094309
Dear,

I have tried your code with a client, and I didn't see any problem. Maybe it's the receiver that corrupt the data. I send you the client used (the server was the perl code that you provided)  then maybe you could find something that will help you.

Best Regards
#!/usr/bin/perl -w
# udpmsg - send a message to the udpquotd server

use IO::Socket;
use strict;

my($sock, $server_host, $msg, $port, $ipaddr, $hishost, 
   $MAXLEN, $PORTNO, $TIMEOUT);

$MAXLEN  = 1024;
$PORTNO  = 21210;
$TIMEOUT = 5;

$server_host = shift;
$msg         = "8305123100138201010102034c4b28ead64b28ead618a74dd4d422467a00003c4d0000002101170620001affab0f280f0005030200000000000000c594";
$sock = IO::Socket::INET->new(Proto     => 'udp',
                              PeerPort  => $PORTNO,
                              PeerAddr  => 'localhost')
    or die "Creating socket: $!\n";
$sock->send($msg) or die "send: $!";

eval {
    local $SIG{ALRM} = sub { die "alarm time out" };
    alarm $TIMEOUT;
    $sock->recv($msg, $MAXLEN)      or die "recv: $!";
    alarm 0;
    1;  # return value from eval on normalcy
} or die "recv from $server_host timed out after $TIMEOUT seconds.\n";

($port, $ipaddr) = sockaddr_in($sock->peername);
$hishost = gethostbyaddr($ipaddr, AF_INET);
print "Server $hishost responded ``$msg''\n";

Open in new window

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question