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

In reply to Reply to TCP messages Net::Server::PreFork by diazocon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.