?
Solved

Perl and listening TCP socket

Posted on 2007-03-22
7
Medium Priority
?
641 Views
Last Modified: 2012-05-05
Hi gurus


I need to listen a TCP socket for a specifig string, take that string and make something with it.

So far I have no luck and here is my code:

----------------------  START ----------------------------
#!/usr/bin/perl -w
use IO::Socket::INET;
use LWP::UserAgent;
use IO::Socket;


# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
$MySocket=new IO::Socket::INET->new(Listen  => 5,
                             LocalPort => 3319,
                               Proto => 'tcp') or die "$!" unless $sock;


# Next we need to wait so long, that we receive a string "PROG_ID xxxxxxx" ,where xxxx is a some ASCII string. Time to wait is from few seconds to 6 hours
while(1)
{
        $MySocket->recv($ID,512);
      print LOG "From socket $ID\n";       # Are we getting any

              if ($ID =~ /^PROG_ID/)
              {
                      print LOG "IF-LAUSEESSA: $ID\n";
                      $ID =~ s/PROGRAM_START //;      #RIP off "PROG_ID and leave only ASCII string
                  print LOG "ASCII ID is: $ID\n";

            }
}
 
0
Comment
Question by:salmjuh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 18775763
try it using regular sockets, no module.  I never got those modules working.


#!/usr/bin/perl -w
use Socket;
# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
my $port=3319;
my $proto=getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l",1)) or die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(Server,SOMAXCONN) or die "listen: $!";

my $paddr;
my $ID;

# Next we need to wait so long, that we receive a string "PROG_ID xxxxxxx" ,where xxxx is a some ASCII string. Time to wait is from few seconds to 6 hours

for( ; $paddr = accept(Client, Server); close Client)
{
my($port, $iaddr) = sockaddr_in($paddr);
my $buff;
my $string_size =14;  ####PLEASE PUT HOW BIG THE STRING IS FROM THE CLIENT
while(read Client, $buff, $string_size)
{
$ID.=$buff;
print LOG "From socket $ID\n";       # Are we getting any
     
              if ($ID =~ /^PROG_ID/)
              {
                      print LOG "IF-LAUSEESSA: $ID\n";
                      $ID =~ s/PROGRAM_START //;      #RIP off "PROG_ID and leave only ASCII string
                  print LOG "ASCII ID is: $ID\n";
                 $ID="";

            }
}
 }
0
 
LVL 8

Expert Comment

by:bebonham
ID: 18775824
you could also use
while($buff=<Client>)

if you don't want to specify string size.

but then I think you will need to know when a PROG_ID xxxxxx code ends like by the newline or somehting
since I can't see what data the client is putting in I can't tell u for sure.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 18777396
Here's your IO::Socket code fixed up.  It assumes your client is sending CRLFs.  If it isn't, it'd block until the socket close.    

#!/usr/bin/perl -w
use IO::Socket;

# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
$MySocket=new IO::Socket::INET->new(Reuse => 1,
                              Listen  => 5,
                              LocalPort => 3319,
                              Proto => 'tcp') or die "$!";

# Next we need to wait so long, that we receive a string "PROG_ID xxxxxxx" ,where xxxx is a some ASCII string. Time to wait is from few seconds to 6 hours

$MySocket->listen();
while ( $conn = $MySocket->accept() ) {
     while ($ID = <$conn>) {
          print LOG "From socket $ID\n";       # Are we getting any
          if ($ID =~ /^PROG_ID/)
          {
               print LOG "IF-LAUSEESSA: $ID\n";
               $ID =~ s/PROGRAM_START //;      #RIP off "PROG_ID and leave only ASCII string
               print LOG "ASCII ID is: $ID\n";
          }
     }
}

---------
If you're client isn't using CRLFs and you really want to read 512 bytes at a time, change this:

   while ($ID = <$conn>) {

To:

   while (read($conn, $ID, 512))  {

The read will block until it receives 512 bytes or until the socket is closed-- not sure what you're after.  Also this:  

    $ID =~ s/PROGRAM_START //;

doesn't really jive with your comment (or with your earlier check):

   #RIP off "PROG_ID and leave only ASCII string

Did you mean?

   $ID =~ s/PROG_ID\s.//;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:salmjuh
ID: 18806204
Hi Gurus


Here is detailed information for the problem.

The string what I need to wait look like this:

"PROGRAM_START 1243564236\r\n"


So how do a wait for a string and 6 to 10 number which end \r\n ?


Thansk for your help

-js
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 18808274
Try:

#!/usr/bin/perl -w
use IO::Socket;

# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
$MySocket=IO::Socket::INET->new(Reuse => 1,
                              Listen  => 5,
                              LocalPort => 3319,
                              Proto => 'tcp') or die "$!";

$MySocket->listen();
while ( $conn = $MySocket->accept() ) {
     while ($ID = <$conn>) {
          chomp;
          print LOG "From socket $ID\n";       # Are we getting any
          if ($ID =~ /PROGRAM_START\s+(\d{6,10)/)
          {
               print LOG "IF-LAUSEESSA: $ID\n";
               print LOG "ASCII ID is: $1\n";
          }
     }
}
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 18808285
Sorry typo.  Change this line:

   if ($ID =~ /PROGRAM_START\s+(\d{6,10)/)

To:

   if ($ID =~ /PROGRAM_START\s+(\d{6,10})/)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 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