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
Solved

Perl and listening TCP socket

Posted on 2007-03-22
7
634 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

838 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