develooper Front page | perl.beginners | Postings from January 2002

Access Transmitted Data Received On "Server" Side Of A TCP Socket

Thread Next
Hewlett Pickens
January 31, 2002 11:19
Access Transmitted Data Received On "Server" Side Of A TCP Socket
Message ID:
Using O'Reilly's "Learning Perl", have set up a "bare bones" socket
connection between two computers to send a small amount of data from the
"client" to the "server".  (For learning, not for the real world.)

The client sends "are you there" and the server responds with "I am here,
what do you want".

Each side prints the data it receives.

The client correctly displays the received:  "I am here ..."

The server displays junk for the data its received, meaning I haven't
correctly acquired it.

Code snippets from the server and client are at the end of this note (tried
making them a text attachment, but that was rejected by the list's server).
I'd appreciate it if someone could point out the error I'm making in the
server code.



Hewlett M. Pickens
B I Moyle Associates, Inc.


              This is the "server"

use strict;
use IO::Socket;
# Start a Socket "receptor"            #
print "HEWSRV001 - $dateTime Socket Receptor is starting \n";

# From Page 441 of Programming Perl
my $server = IO::Socket::INET->new(LocalPort => $server_port,
                                Type      => SOCK_STREAM,
                                Reuse     => 1,
                                Listen    => 10)
     or die "(Couldn't get a socket on port $server_port: $!\n";
print "HEWSRV002 - Result of IO Socket call:  $server \n\n";
# now wait for something to do         #
while ($client = $server->accept())
    print "HEWSRV004 - I have a connection from $client \n";
    $bufferSize = 100;
       # the "->recv" was not in Learning Perl page 441.  Got
       # it from reading doc for IO::Socket
    $dataIn = $client->recv($bufferIn, $bufferSize);
    print "HEWSRV005 - I received this data: $dataIn \n";
    print $client "I am here. What do you want \n";
    print "HEWSRV999 - Data was sent to client\n\n";
Result of contact from the client:

   HEWSRV004 - I have a connection from IO::Socket::INET=GLOB(0x588ef8)
   HEWSRV005 - I received this data: c¬($%^ &#$

   HEWSRV999 - Data was sent to client

The data should be:  "are you there", so I haven't properly acquired it.


              This is the "client" Side

use IO::Socket;
use strict;
# From Page 439-440 of Programming Perl
$socket = IO::Socket::INET->new(PeerAddr => $remote_host,
                          PeerPort => $remote_port,
                          Proto    => "tcp",
                          Type     => SOCK_STREAM)
or die "HEWSTA004 - Connect failed - $remote_host:$remote_port : $@\n";
$socket->autoflush(1);              # flush the buffer immediately
print "HEWSTA005 - Socket created with $remote_host $remote_HostName \n";
print "            $remote_host, Port $remote_port \n";
print $socket "are you there \n";
print "HEWSTA998 Receiving a reply from Server \n";
my $answer = <$socket>;
if ($answer lt "0") {$answer = "None Received"};
print "HEWSTA997 this is the reply: $answer \n";



Result of contact with the server:
  HEWSTA005 - Socket created with LINUXVM1
  , Port 1400
  HEWSTA998 Receiving a reply from Server
  HEWSTA997 this is the reply: I am here. What do you want

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About