socket to listen


C

cerr

Hi There,

I need to write a client that is listening on a certain port and do
sometihng in case there's no data coming.
Now, if I telnet in on port 16010, I see the data coming but my perly
script isn't showing anything, what am I doing wrong?
My code looks like this:
[snip]
my $proto = getprotobyname('tcp');
# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";

my $line;
while ($line != ""){
print $line;
}
print $line;
close SOCKET or die "close: $!";
[snip]
Shouldn't that work exactly like this? I don't get no error message or
anything - i don't see a thing... :eek:
 
Ad

Advertisements

C

cerr

cerr said:
Hi There,
I need to write a client that is listening on a certain port and do
sometihng in case there's no data coming.
Now, if I telnet in on port 16010, I see the data coming but my perly
script isn't showing anything, what am I doing wrong?
My code looks like this:
[snip]

use strict;
use warnings;
my $proto = getprotobyname('tcp');
# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";
my $line;
while ($line != ""){

try reading from the socket!
  print $line;

$line won't have a value will it?
}
print $line;
close SOCKET or die "close: $!";
[snip]
Shouldn't that work exactly like this? I don't get no error message or
anything - i don't see a thing... :eek:

Change `print $line;` to `print "Line='$line'\n";` and you'll see plenty :)
I just made a $line = <SOCKET> which works fine as well! :D

Thanks for your help dude!
 
Ad

Advertisements

U

Uri Guttman

c> I need to write a client that is listening on a certain port and do
c> sometihng in case there's no data coming.

you aren't listening if you create a client socket. please learn proper
socket terminology as it will help you a great deal.

c> My code looks like this:

c> my $proto = getprotobyname('tcp');
c> # get the port address
c> my $iaddr = inet_aton($host);
c> my $paddr = sockaddr_in($port, $iaddr);
c> # create the socket, connect to port
c> socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
c> connect(SOCKET, $paddr) or die "connect: $!";

replace all of that with:

use IO::Socket ;
my $sock = IO::Socket::INET->new( "$host:$port" ) ;

isn't that a little easier?

c> my $line;
c> while ($line != ""){
c> print $line;
c> }

someone else already pointed out that you don't ever read from the
socket.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top