Solved

Listen to A Port For Data & Print The Data

Posted on 2007-12-03
5
1,029 Views
Last Modified: 2012-05-05
Hi,
Some data is being pushed out on another UNIX server to a specific port. I am trying to put a Perl script together that will capture this data and print it out. So far I have the following;

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

my $text = "";
my $port='the_port';
my $svr='the_host';
my $sock = new IO::Socket::INET (
            PeerAddr => $svr,
            PeerPort => $port,
            Proto => 'tcp',
            Type => SOCK_STREAM,
);
die "Could not connect to Port : $!\n" unless $sock;
      binmode $sock;
      $sock->autoflush(1);

print "Listening on $svr $port\n";

while (<$sock>) {
        print;
}
close $sock;

The connect works as I get no error, but nothing is printed. I know data is there so have I misssed something ?
0
Comment
Question by:adlikon
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 20397054
If you don't change the input record separator, the <$sock> will wait for the default input record separator (eg: "\n").  If the sending computer doesn't send this, your code will wait forever.

You can change the input record separator with:
$sock->input_record_separator($new_sep);

0
 

Author Comment

by:adlikon
ID: 20401614
Hi, now I get the following error;

"Can't locate auto/IO/Socket/INET/input_recor.al in @INC (@INC contains: /sbcimp/run/pd/cpan/5.8.5-2004.09/lib /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 . /sbcimp/run/pd/cpan/5.8.6-2005.03/lib /sbcimp/run/pd/cpan/5.8.6-2005.03/bin) at ./a.pl lin
e 30"

I have tried including libraries using;

BEGIN {
        use lib qw( /sbcimp/run/pd/cpan/5.8.5-2004.09/lib );
        my @paths = (
                "/sbcimp/run/pd/cpan/5.8.6-2005.03/lib",
                "/sbcimp/run/pd/cpan/5.8.6-2005.03/bin",
            );
            push @INC, @paths
}

I then get the error;

"Can't locate auto/IO/Socket/INET/input_recor.al in @INC (@INC contains: /sbcimp/run/pd/cpan/5.8.5-2004.09/lib /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 . /sbcimp/run/pd/cpan/5.8.6-2005.03/lib /sbcimp/run/pd/cpan/5.8.6-2005.03/bin) at ./a.pl lin
e 30"

I have done a search and can not find "input_recor.al" in any of our library directories which as far as I am aware are all standard. Is this new error a red herring ?


0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 20403414
Sorry, that was a mistake on my part.

Try this instead.  Add this line right after your "use IO::Socket" line
replace $new_sep with whatever input record separator you want to use:
IO::Socket->input_record_separator($new_sep);
0
 

Author Closing Comment

by:adlikon
ID: 31412380
Now that I have rethought my requirment I need to approach this differently, so technically this is not the answer I need anymore but that is not the fault of Adam314. My orginal question is no longer vaild for my needs.
0
 

Expert Comment

by:molikjain
ID: 26033867
can you give me the complete perl code above, so that I know the complete answer at one place.
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

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 109
PERL get the value for query 4 145
Perl output collect 7 65
Linux rename files by reordering dates in the file name 6 33
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…
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…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 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

16 Experts available now in Live!

Get 1:1 Help Now