Solved

Perl and listening TCP socket

Posted on 2007-03-22
7
630 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now