windows problem with socket write

M

mastermagrath

Hi all,

I've a problem with a simple socket client/server script which has bee
described in several threads already but alas none of them have been
able to help me.

Its the problem wherein any text i write to the server socket doesn't
get outpu until i close the client connection. All of the threads on
the subject all suggest the same thing i.e. make sure you send a
newline or carriage return or both with the string in question, i've
tried everything, i've tried sending:

"text to send\n"
"text to send\r\n"
"text to send\n\r"

Nothing works, nothing gets processed from the servers end until i
close down the client end, only then does the text get printed in the
server. I even tried sending an explicit EOF character with the string
but even that doesn't work!
I've dumped the very simple code below if anyone could take a look. Has
anyone got a very simple example script similar to this which works on
XP acitve state v5.8.8???

SERVER CODE:

$server = IO::Socket::INET->new(
Listen => 5,
LocalAddr => 'localhost',
LocalPort => 9000,
Proto => 'tcp');
die "ERROR: $!\n" unless $server;

print "Waiting on connections...\n";
while($client = $server->accept()){
print "Connection made, reading data...\n";
print <$client>;
print "Connection closed...\n";
}

$server->close();

CLIENT CODE:

$sock = IO::Socket::INET->new(
PeerAddr => 'localhost',
PeerPort => 9000,
Proto => 'tcp');
die "Error: $!\n" unless $sock;
$sock->autoflush(1);
print $sock "take me to your leader\r\n";
$answer=<$sock>;
print $answer;

close($sock);
 
S

Sisyphus

mastermagrath said:
Hi all,

I've a problem with a simple socket client/server script ..
..

SERVER CODE:

$server = IO::Socket::INET->new(
Listen => 5,
LocalAddr => 'localhost',
LocalPort => 9000,
Proto => 'tcp');
die "ERROR: $!\n" unless $server;

print "Waiting on connections...\n";
while($client = $server->accept()){
print "Connection made, reading data...\n";
print <$client>;
print "Connection closed...\n";
}

$server->close();

CLIENT CODE:

$sock = IO::Socket::INET->new(
PeerAddr => 'localhost',
PeerPort => 9000,
Proto => 'tcp');
die "Error: $!\n" unless $sock;
$sock->autoflush(1);
print $sock "take me to your leader\r\n";
$answer=<$sock>;
print $answer;

close($sock);

Your 'print <$client>;' in the server script does not function as you
expect, the specification of 'LocalAddr => 'localhost,' in the server script
seems to stuff things up, and the client script is waiting for a reply from
the server but is not getting one. This modified server script works ok for
me:

use IO::Socket;
use warnings;

$server = IO::Socket::INET->new(
Listen => 5,
LocalPort => 9000,
Proto => 'tcp');
die "ERROR: $!\n" unless $server;

print "Waiting on connections...\n";
while($client = $server->accept()){
print "Connection made, reading data...\n";
$data_read = <$client>;
print $data_read, "\n";
print $client "Message received ... bye\n";
print "Connection closed...\n";
}

$server->close();

(All I think I've done differently is have the server print a reply to
$client, delete the 'LocalAddr' setting from the new() constructor, and
assign '<client>' to $data_read, rather than try to print it out directly.)

Cheers,
Rob
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top