Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Listening UDP socket (http://www.velocityreviews.com/forums/t897417-listening-udp-socket.html)

jussi 04-11-2006 10:57 AM

Listening UDP socket
 
Hi gurus

My program listen a socket and wait for right kind of string to do a
job. Problem is that some times the string cut out and my program
thinks that all the data is arrived and we can continue.

Here is part of my program:

------------------ SNIP ----------------------------------------------


$MySocket=new IO::Socket::INET->new(LocalPort=>3500,Proto=>'udp');

while(1)
{
$MySocket->recv($ID,128);
print LOG "data to socket: $ID\n";
# print to LOG every data what get to socket
if ($ID =~ /1820/)
# does the string contain numbers 1820, if yes go on
{
print LOG "We are in with string: $ID\n";
$ID =~ s/.*10([1-9])00//;
#cut garpage out
$ID =~ s/\W+\D+//;
# only numbers and letters
$ID =~ s/[a-z]//;
# delete these too
$ID =~ s/${1}//;
# cut last mark out because it's usually garpage
$ID =~ /([A-F0-9]{1,16})/;
#I should have 1-16 numbers or letters
print LOG "to HEX2ASCII: $ID\n";
#print to LOG before translate
$h_str = pack 'H*', $ID;
# HEX2ASCII
Print LOG "from HEX2ASCII: $h_str\n";
# print to log after translate




------------------------- SNIP
------------------------------------------


OK. So my script listening a UDP socket and waits for a string which
contains a string 1820. This usually work. But some times the string
some how cuts out and rest of my script get faulty string to proces.

Here is part from my LOG to show what I mean. First part is how it
ususally works (and should work)

------------- SNIP --------------------------------

data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket
:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
#all this is gargabe
so do nothing
data to socket: 0184000100000182000106004D3737363034
# here is right kind of string so go on
We are in with string: 0184000100000182000106004D3737363034
#So we are in
to HEX2ASCII 4D3737363034
# take out constant header
and send only the part we are intrested
from HEX2ASCII: M77604
# yes, hex2ascii is working
right
data to socket:
# lets wait next right
header
data to socket:
data to socket: 018300010000
# thsi string is not what we want
so do nothing

data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket:
data to socket:


------------------------------ SNIP ---------------------------------


And next SNIP from LOG is my problem:

------------------------------- SNIP
-----------------------------------


data to so socket:
data to so socket:

data to so socket:

data to so socket:
data to so socket: # so
far its empty or garbage
data to so socket: 01840001000001820 # yes, here is
string with 1820 which we are intrested, but this is header part were
is the REAL part we want to ??
We are in with string:: 01840001000001820 # so we take it to
for processing
to HEX2ASCII: 01840001000001820 # this is not
good
from HEX2ASCII: #
gives me empty

data to so socket: 0010600503737333331 # here is REAL part
(0010600 is belongs to header, but after that data). But because there
is no header any more we doun't reqocnice this any more and we think
its garbage
data to so socket:
data to so socket: 018300010000

data to so socket:
data to so socket:
data to so socket:
data to so socket:


--------------------- SNIP ------------------------------------------

So how can I take the real part for processing if it's cutted out from
the string ?

I know that data part which I'm interested starts wit 0x02 end with
code 0x03. So I think that I should do it like this:

listen socket and take one mark at time to process -- do it
until we get 0x02
descard everything else before 0x02
if 0x02 then
append every mark to $MyDatastring until 0x03
do something wirh the data and start from beginning


Any help would be nice

Thansk advance
-Jussi



All times are GMT. The time now is 10:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.