Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

IO::Socket echo server not echoing
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";


Report this thread to moderator Post Follow-up to this message
Old Post
paulmlieberman@alum.mit.edu
04-23-05 01:56 AM


Re: IO::Socket echo server not echoing
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

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Clements
04-23-05 01:56 AM


Re: IO::Socket echo server not echoing
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 mcse.ms - Unlimited-Uncensored-Secure Usenet News==-
---
http://www.mcse.ms The #1 Newsgroup Service in the World! 120,000+ New
sgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Report this thread to moderator Post Follow-up to this message
Old Post
Jim Gibson
04-23-05 01:56 AM


Re: IO::Socket echo server not echoing
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";


Report this thread to moderator Post Follow-up to this message
Old Post
paulmlieberman@alum.mit.edu
04-23-05 08:57 AM


Re: IO::Socket echo server not echoing
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 mcse.ms - Unlimited-Uncensored-Secure Usenet News==-
---
http://www.mcse.ms The #1 Newsgroup Service in the World! >100,000 New
sgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

Report this thread to moderator Post Follow-up to this message
Old Post
Jim Gibson
04-26-05 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:21 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.