Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Reply to TCP messages Net::Server::PreFork

by diazocon (Initiate)
on Oct 06, 2010 at 22:01 UTC ( #863874=perlquestion: print w/ replies, xml ) Need Help??
diazocon has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks, I will like to know if someone can help me, I have been reading for 2 days now and I am stuck; so I decide to come for help... I need a multi threaded TCP server listening on port 9000 for a GPS project. I have 2 different devices and each one sends the message in different format, one sends lines like:
(013632782450BP05000013632782450101005A2038.3383N10315.1368W000.004122 +0116.4900000000L00000000)(013632782450BP05000013632782450101005A2038. +3383N10315.1368W000.0041225116.4900000000L00000000)(013632782450BP050 +00013632782450101005A2038.3383N10315.1368W000.0041255116.4900000000L0 +0000000)(013632782450BP00000013632782450HSO)(013632782450BP0500001363 +2782450101005A2038.3383N10315.1368W000.0041325116.4900000000L00000000 +)
and the other sends lines like: ##,imei:354779033965634,A; and sits there, waiting for the server to reply LOAD and then it replies back with the actual GPS data. I have been reading and using multiple approaches, but the hard thing its that the GPS dont send newline chars, so many solutions (such as NetServer::Generic or IO:Socket*...) don't work. Long story short, I was able to parse the first format with read but I still need to be able to get the other message, reply with LOAD and get the text. Once I get that text, I could process it with a regex... but I am still away from there, Any advice? Here is my code (which I also borrowed and modified from here):
#!/usr/bin/perl -w -T package MyPackage; use strict; use base qw(Net::Server::PreFork); MyPackage->run({port => 9000, no_client_stdout => 1}); sub process_request { my $self = shift; eval { local $SIG{'ALRM'} = sub { die "Timed Out!\n" }; my $timeout = 5; my $sock = $self->{server}->{client}; $sock->autoflush(1); $|=1; binmode $sock; my $buf; while (read ($sock, $buf, 94) != 0 ){ #sleep(5); print $sock "client said '$buf'\r\n"; print "client said '$buf'\r\n"; alarm($timeout); } print $buf; alarm(0); }; if ($@ =~ /timed out/i) { print STDOUT "Timed Out: $@.\r\n"; return; } }
Thank you All! Carlos

Comment on Reply to TCP messages Net::Server::PreFork
Select or Download Code
Replies are listed 'Best First'.
Re: Reply to TCP messages Net::Server::PreFork
by sflitman (Hermit) on Oct 07, 2010 at 05:46 UTC
    I'd try setting $/ to the ; at the end, see if that terminates a call to readline:
    ... local $/=';'; while (defined($buf=$sock->getline)) { # parse $buf here } ...
    HTH,
    SSF
      Hi SSF, It didn't work, it still waits for newline to parse get the imput. Is there anyway to get the contents of the buffer after a specific timeout? that way I could just process the contents of $buf wheater is a 96 byte string or the other format. Thanks! Carlos
        hi.. i am in the same stuff, but different issue. to solve your problem i use "IO::Handle->input_record_separator(')');" it helps me to separate each gps records. it works fine. i cant get "Net::Server::PreFork;" work yeat.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863874]
Approved by ww
Front-paged by sflitman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (18)
As of 2015-07-29 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (265 votes), past polls