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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.