Home > Archive > PERL Miscellaneous > May 2004 > perl tcp server script works, but no data.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
perl tcp server script works, but no data.
|
|
| Eli Sidwell 2004-05-19, 3:32 pm |
| I am trying to get the following script to accept data, but I never see any.
Is this a flushing issue ?
Any help is appreciated.
#!/usr/bin/perl -w
$server_port = "5001";
use IO::Socket;
# make the socket
$server = IO::Socket::INET->new(LocalPort => $server_port,
Type => SOCK_STREAM,
Proto => 'tcp',
Reuse => 1,
Listen => 10 ) # or SOMAXCONN
or die "Couldn't be a tcp server on port $server_port : $@\n";
$server->autoflush();
while ($client = $server->accept()) {
# $client is the new connection
$client->recv($str, 1024)
or die "Can't recv: $!\n";
$client_ip = $client->peerhost;
$client_port = $client->peerport;
print "Data is: $str \n";
print "Client IP: $client_ip\n";
print "Client port: $client_port\n";
}
close($server);
| |
| Jim Gibson 2004-05-19, 5:35 pm |
| In article <ef9a95b0.0405191046.6ef2960e@posting.google.com>, Eli
Sidwell <sidwelle@alexian.net> wrote:
> I am trying to get the following script to accept data, but I never see any.
> Is this a flushing issue ?
>
> Any help is appreciated.
>
>
> #!/usr/bin/perl -w
>
> $server_port = "5001";
>
> use IO::Socket;
>
> # make the socket
> $server = IO::Socket::INET->new(LocalPort => $server_port,
> Type => SOCK_STREAM,
> Proto => 'tcp',
> Reuse => 1,
> Listen => 10 ) # or SOMAXCONN
> or die "Couldn't be a tcp server on port $server_port : $@\n";
>
> $server->autoflush();
>
> while ($client = $server->accept()) {
> # $client is the new connection
>
> $client->recv($str, 1024)
> or die "Can't recv: $!\n";
recv() is for UDP messages. You have specified a TCP connection. Try
my $nread = $client->read($str,1024);
die "Can't read from socket: $!" unless defined $nread;
instead.
>
> $client_ip = $client->peerhost;
> $client_port = $client->peerport;
>
> print "Data is: $str \n";
> print "Client IP: $client_ip\n";
> print "Client port: $client_port\n";
> }
>
> close($server);
You have not shown the client program, so there could be a problem
there as well.
| |
| Eli Sidwell 2004-05-20, 12:32 pm |
| Still nothing, the $nread even comes up 0.
As far as the client, I tried to telnet a small file to that port and
then I wrote an app using the VB winsock to move text to that port.
Both times it opens the port and accepts the data (I think), I can get
the port and IP from perl but, I don't see any data.
Are there any good reference books on the perl socket with examples.
I have the orilly cookbook, but its a lot of theory.
Thanks
Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message news:<190520041320228983%jgibson@mail.arc.nasa.gov>...
> In article <ef9a95b0.0405191046.6ef2960e@posting.google.com>, Eli
> Sidwell <sidwelle@alexian.net> wrote:
>
>
> recv() is for UDP messages. You have specified a TCP connection. Try
>
> my $nread = $client->read($str,1024);
> die "Can't read from socket: $!" unless defined $nread;
>
> instead.
>
>
> You have not shown the client program, so there could be a problem
> there as well.
| |
| Kevin Collins 2004-05-20, 8:31 pm |
| In article <ef9a95b0.0405200724.2b8f1e58@posting.google.com>, Eli Sidwell
wrote:
Please, no top posting...
> Still nothing, the $nread even comes up 0.
>
> As far as the client, I tried to telnet a small file to that port and
> then I wrote an app using the VB winsock to move text to that port.
> Both times it opens the port and accepts the data (I think), I can get
> the port and IP from perl but, I don't see any data.
>
> Are there any good reference books on the perl socket with examples.
> I have the orilly cookbook, but its a lot of theory.
>
-snip-
perldoc 'perlipc' has a basic TCP client/server that I have used as the basis
for a couple of different things. Its not too complex.
Kevin
| |
| Anno Siegel 2004-05-21, 6:31 am |
| Kevin Collins <spamtotrash@toomuchfiction.com> wrote in comp.lang.perl.misc:
> In article <ef9a95b0.0405200724.2b8f1e58@posting.google.com>, Eli Sidwell
> wrote:
>
> Please, no top posting...
>
> -snip-
>
> perldoc 'perlipc' has a basic TCP client/server that I have used as the basis
> for a couple of different things. Its not too complex.
There's also Net::EasyTCP which covers simple TCP-based client/server
pairs.
Anno
| |
| Eli Sidwell 2004-05-21, 11:31 pm |
| anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<c8kgsg$smk$3@mamenchi.zrz.TU-Berlin.DE>...
> Kevin Collins <spamtotrash@toomuchfiction.com> wrote in comp.lang.perl.misc:
>
> There's also Net::EasyTCP which covers simple TCP-based client/server
> pairs.
>
> Anno
I think that I have narrowed the problem, it seems that I don't
receive any data unless I send more than a 2k of stuff. I have seen
some postings that refer to flushing issues, how do I cause this code
to flush no matter what size data is received?
Thanks
| |
| Ben Morrow 2004-05-22, 12:31 am |
|
Quoth sidwelle@alexian.net (Eli Sidwell):
> I think that I have narrowed the problem, it seems that I don't
> receive any data unless I send more than a 2k of stuff. I have seen
> some postings that refer to flushing issues, how do I cause this code
> to flush no matter what size data is received?
This sounds like a problem on the sending end... you need to flush after
each write. In perl, set $|=1.
Ben
--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
ben@morrow.me.uk Groucho Marx
|
|
|
|
|