Solved

Perl and listening TCP socket

Posted on 2007-03-22
7
632 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

12 Experts available now in Live!

Get 1:1 Help Now