For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > IO::Socket echo server not echoing









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 IO::Socket echo server not echoing
paulmlieberman@alum.mit.edu

2005-04-22, 8:56 pm

The following code, modified from Lincoln Stein's "Network Programming
with Perl", doesn't work properly on Unix (5.6.1) or Windows systems
(5.8.6). The server receives the first message, but doesn't echo it
properly. Why?

#!/usr/bin/perl
# IOserver.pl

use strict;
use IO::Socket::INET;

use constant SIMPLE_TCP_PORT => 4001;
use constant MAX_RECV_LEN => 65536;

print "Hello, World...\n";

my $local_port = shift || SIMPLE_TCP_PORT;
my $server = IO::Socket::INET->new(Proto => 'tcp',
Type => SOCK_STREAM,
Reuse => 1,
Timeout => 60 * 60,
LocalPort => $local_port,
Listen => SOMAXCONN)
or die "Can't create server\n";

my $fromWho;
my $peeraddr;

while($fromWho = $server->accept())
{
my $hostinfo;
my $data;
$fromWho->recv($data, MAX_RECV_LEN);
if($fromWho)
{
$peeraddr = $fromWho->peeraddr();
$hostinfo = gethostbyaddr($peeraddr, AF_INET);
warn "Received from server: ", length($data), ' -> ', $data, "\n";
}
else
{
warn "IOserver: problem with recv: $!\n";
next;
}
sleep(3);
warn "Sending back to client...\n";
if (! $server->send($data, 0, $fromWho)) {
my $so_error = $server->sockopt(SO_ERROR);
die "IOserver: error $so_error sending message to $hostinfo: $!\n";
}
}
$server->close()
or warn "IOserver: close failed: $!\n";

========================================
===================================

#!/usr/bin/perl
# IOclient.pl

use strict;
use IO::Socket::INET;

use constant SIMPLE_TCP_PORT => 4001;
use constant REMOTE_HOST => 'localhost';

use constant MAX_RECV_LEN => 65536;

my $remote_host = shift || REMOTE_HOST;
my $remote_port = shift || SIMPLE_TCP_PORT;
my $msg_count = 1;
my $big_chunk = 'x' x 65000;

my $client = IO::Socket::INET->new(Proto => 'tcp',
Type => SOCK_STREAM,
PeerAddr => $remote_host,
PeerPort => $remote_port)
or die "connection to remote system failed: $!\n";

while($msg_count)
{
next unless $client;

print "Enter string to echo: ";
$big_chunk = <STDIN>;
my $data = $client->sockhost() .' '. $msg_count . ' -> ' .
$big_chunk;

warn "Sending echo string to server...\n";

$client->send($data, 0)
or warn "problem with send...\n";

sleep(1);

my $fromWho = $client->recv($data, MAX_RECV_LEN, 0)
or die "Problem with recv(): $!\n";

if($fromWho)
{
my $remote_name = $client->peerhost();
warn "Received from $remote_name: ", length($data), ' => ',$data,
"\n";
}
else
{
warn "problem with recv: $!\n";
$client->close()
or warn "close failed: $!\n";
}
}
continue{
$msg_count++;
}
$client->close()
or warn "tcp_cli: close failed: $!\n";

Mark Clements

2005-04-22, 8:56 pm

paulmlieberman@alum.mit.edu wrote:
> The following code, modified from Lincoln Stein's "Network Programming
> with Perl", doesn't work properly on Unix (5.6.1) or Windows systems
> (5.8.6). The server receives the first message, but doesn't echo it
> properly. Why?
>
>

<snip>

> while($fromWho = $server->accept())
> {
> my $hostinfo;
> my $data;
> $fromWho->recv($data, MAX_RECV_LEN);
> if($fromWho)
> {
> $peeraddr = $fromWho->peeraddr();
> $hostinfo = gethostbyaddr($peeraddr, AF_INET);
> warn "Received from server: ", length($data), ' -> ', $data, "\n";
> }
> else
> {
> warn "IOserver: problem with recv: $!\n";
> next;
> }
> sleep(3);
> warn "Sending back to client...\n";
> if (! $server->send($data, 0, $fromWho)) {

You are trying to send on the listening socket, not the connected one.
try
$fromWho->send( $data, 0 )

<snip>

> my $fromWho = $client->recv($data, MAX_RECV_LEN, 0)
> or die "Problem with recv(): $!\n";
>
> if($fromWho)
> {
> my $remote_name = $client->peerhost();
> warn "Received from $remote_name: ", length($data), ' => ',$data,
> "\n";
> }
> else
> {
> warn "problem with recv: $!\n";
> $client->close()
> or warn "close failed: $!\n";
> }
> }

You need to look at the return value of recv:

recv SOCKET,SCALAR,LENGTH,FLAGS
Receives a message on a socket. Attempts to receive
LENGTH characters of data into variable SCALAR from
the specified SOCKET filehandle. SCALAR will be
grown or shrunk to the length actually read. Takes
the same flags as the system call of the same name.
Returns the address of the sender if SOCKET's
protocol supports this; returns an empty string
otherwise. If there's an error, returns the
undefined value. This call is actually implemented
in terms of recvfrom(2) system call. See "UDP

ie the error condition is undef, so your check should be


if(defined $fromWho){

With these modifications:

bob 606 $ perl testechoclient.pl
Enter string to echo: sdfg
Sending echo string to server...
Received at server: 20 -> 127.0.0.1 1 -> sdfg

Sending back to client...
Received from 127.0.0.1: 20 => 127.0.0.1 1 -> sdfg


regards,

Mark
Jim Gibson

2005-04-22, 8:56 pm

In article <1114205460.155209.275830@o13g2000cwo.googlegroups.com>,
<paulmlieberman@alum.mit.edu> wrote:

> The following code, modified from Lincoln Stein's "Network Programming
> with Perl", doesn't work properly on Unix (5.6.1) or Windows systems
> (5.8.6). The server receives the first message, but doesn't echo it
> properly. Why?


You are sending data over the server socket, which is connected to
nobody. You want to send data to the client socket.

>
> #!/usr/bin/perl
> # IOserver.pl
>
> use strict;
> use IO::Socket::INET;
>
> use constant SIMPLE_TCP_PORT => 4001;
> use constant MAX_RECV_LEN => 65536;
>
> print "Hello, World...\n";
>
> my $local_port = shift || SIMPLE_TCP_PORT;
> my $server = IO::Socket::INET->new(Proto => 'tcp',
> Type => SOCK_STREAM,
> Reuse => 1,
> Timeout => 60 * 60,
> LocalPort => $local_port,
> Listen => SOMAXCONN)
> or die "Can't create server\n";
>
> my $fromWho;
> my $peeraddr;
>
> while($fromWho = $server->accept())
> {
> my $hostinfo;
> my $data;
> $fromWho->recv($data, MAX_RECV_LEN);
> if($fromWho)
> {
> $peeraddr = $fromWho->peeraddr();
> $hostinfo = gethostbyaddr($peeraddr, AF_INET);
> warn "Received from server: ", length($data), ' -> ', $data, "\n";
> }
> else
> {
> warn "IOserver: problem with recv: $!\n";
> next;
> }
> sleep(3);
> warn "Sending back to client...\n";


Use $fromWho in the next two lines instead of $server.

> if (! $server->send($data, 0, $fromWho)) {
> my $so_error = $server->sockopt(SO_ERROR);
> die "IOserver: error $so_error sending message to $hostinfo: $!\n";
> }


close $fromWho;

> }
> $server->close()
> or warn "IOserver: close failed: $!\n";
>
> ========================================
===================================
>
> #!/usr/bin/perl
> # IOclient.pl


[client snipped]

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
paulmlieberman@alum.mit.edu

2005-04-23, 3:57 am

OK! With those changes, it now works for the first iteration: the
client sends, the server echos. But when the client tries to send a
second string, it fails.

Here's my modified code.

- Paul

#!/usr/bin/perl
# IOserver.pl

use strict;
use IO::Socket::INET;

use constant SIMPLE_TCP_PORT => 4001;
use constant MAX_RECV_LEN => 65536;

print "Hello, World...\n";

my $local_port = shift || SIMPLE_TCP_PORT;
my $server = IO::Socket::INET->new(Proto => 'tcp',
Type => SOCK_STREAM,
Reuse => 1,
Timeout => 60 * 60,
LocalPort => $local_port,
Listen => SOMAXCONN)
or die "Can't create server\n";

my $fromWho;
my $peeraddr;

while($fromWho = $server->accept())
{
my $hostinfo;
my $data;
$fromWho->recv($data, MAX_RECV_LEN);
if(defined $fromWho)
{
$peeraddr = $fromWho->peeraddr();
$hostinfo = gethostbyaddr($peeraddr, AF_INET);
warn "Received from server: ", length($data), ' -> ', $data, "\n";
}
else
{
warn "IOserver: problem with recv: $!\n";
next;
}
sleep(3);
warn "Sending back to client...\n";
if (! $fromWho->send($data, 0)) {
my $so_error = $fromWho->sockopt(SO_ERROR);
die "IOserver: error $so_error sending message to $hostinfo: $!\n";
}
close($fromWho)
or warn "IOserver: close failed: $!\n";
warn "Waiting for another connection!\n";
}
========================================
=========================
#!/usr/bin/perl
# IOclient.pl

use strict;
use IO::Socket::INET;

use constant SIMPLE_TCP_PORT => 4001;
use constant REMOTE_HOST => 'localhost';

use constant MAX_RECV_LEN => 65536;

my $remote_host = shift || REMOTE_HOST;
my $remote_port = shift || SIMPLE_TCP_PORT;
my $msg_count = 1;
my $big_chunk = 'x' x 65000;

my $client = IO::Socket::INET->new(Proto => 'tcp',
Type => SOCK_STREAM,
PeerAddr => $remote_host,
PeerPort => $remote_port)
or die "connection to remote system failed: $!\n";

while($msg_count)
{
next unless $client;

print "Enter string to echo: ";
$big_chunk = <STDIN>;
my $data = $client->sockhost() .' '. $msg_count . ' -> ' .
$big_chunk;

warn "Sending echo string to server...\n";

$client->send($data, 0)
or warn "problem with send...\n";

sleep(1);

my $fromWho = $client->recv($data, MAX_RECV_LEN, 0)
or die "Problem with recv(): $!\n";

if(defined $fromWho)
{
my $remote_name = $client->peerhost();
warn "Received from $remote_name: ", length($data), ' => ',$data,
"\n";
}
else
{
warn "problem with recv: $!\n";
$client->close()
or warn "close failed: $!\n";
}
}
continue{
$msg_count++;
}
$client->close()
or warn "IOclient: close failed: $!\n";

Jim Gibson

2005-04-25, 8:56 pm

In article <1114219080.844579.316010@o13g2000cwo.googlegroups.com>,
<paulmlieberman@alum.mit.edu> wrote:

> OK! With those changes, it now works for the first iteration: the
> client sends, the server echos. But when the client tries to send a
> second string, it fails.


Your server is closing the socket after sending the echoed output back
to the client. The client should also close its socket connection to
the server and reconnect before trying to send more data.

>
> Here's my modified code.
>
> - Paul
>
> #!/usr/bin/perl
> # IOserver.pl
>
> use strict;
> use IO::Socket::INET;
>
> use constant SIMPLE_TCP_PORT => 4001;
> use constant MAX_RECV_LEN => 65536;
>
> print "Hello, World...\n";
>
> my $local_port = shift || SIMPLE_TCP_PORT;
> my $server = IO::Socket::INET->new(Proto => 'tcp',
> Type => SOCK_STREAM,
> Reuse => 1,
> Timeout => 60 * 60,
> LocalPort => $local_port,
> Listen => SOMAXCONN)
> or die "Can't create server\n";
>
> my $fromWho;
> my $peeraddr;
>
> while($fromWho = $server->accept())
> {
> my $hostinfo;
> my $data;
> $fromWho->recv($data, MAX_RECV_LEN);
> if(defined $fromWho)


The value of $fromWho will not be changed by the recv call. You should
capture the return of recv and test that instead.

> {
> $peeraddr = $fromWho->peeraddr();
> $hostinfo = gethostbyaddr($peeraddr, AF_INET);
> warn "Received from server: ", length($data), ' -> ', $data, "\n";
> }
> else
> {
> warn "IOserver: problem with recv: $!\n";
> next;
> }
> sleep(3);
> warn "Sending back to client...\n";
> if (! $fromWho->send($data, 0)) {
> my $so_error = $fromWho->sockopt(SO_ERROR);
> die "IOserver: error $so_error sending message to $hostinfo: $!\n";
> }
> close($fromWho)
> or warn "IOserver: close failed: $!\n";
> warn "Waiting for another connection!\n";
> }
> ========================================
=========================
> #!/usr/bin/perl
> # IOclient.pl
>
> use strict;
> use IO::Socket::INET;
>
> use constant SIMPLE_TCP_PORT => 4001;
> use constant REMOTE_HOST => 'localhost';
>
> use constant MAX_RECV_LEN => 65536;
>
> my $remote_host = shift || REMOTE_HOST;
> my $remote_port = shift || SIMPLE_TCP_PORT;
> my $msg_count = 1;
> my $big_chunk = 'x' x 65000;


Why are you setting $big_chunk to a value here? It is not used and gets
over-written below.

>
> my $client = IO::Socket::INET->new(Proto => 'tcp',
> Type => SOCK_STREAM,
> PeerAddr => $remote_host,
> PeerPort => $remote_port)
> or die "connection to remote system failed: $!\n";
>
> while($msg_count)
> {
> next unless $client;


This test is useless. $client will always be true here.

>
> print "Enter string to echo: ";
> $big_chunk = <STDIN>;


Be aware the $big_chunk has a trailing new-line here. That may not
always be what you want (use chomp($big_chunk) to remove it.)

> my $data = $client->sockhost() .' '. $msg_count . ' -> ' .
> $big_chunk;
>
> warn "Sending echo string to server...\n";
>
> $client->send($data, 0)
> or warn "problem with send...\n";
>
> sleep(1);
>
> my $fromWho = $client->recv($data, MAX_RECV_LEN, 0)
> or die "Problem with recv(): $!\n";
>
> if(defined $fromWho)
> {
> my $remote_name = $client->peerhost();
> warn "Received from $remote_name: ", length($data), ' => ',$data,
> "\n";
> }
> else
> {
> warn "problem with recv: $!\n";
> $client->close()
> or warn "close failed: $!\n";
> }
> }
> continue{
> $msg_count++;
> }
> $client->close()
> or warn "IOclient: close failed: $!\n";
>



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com