Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > windows problem with socket write

Reply
Thread Tools

windows problem with socket write

 
 
mastermagrath
Guest
Posts: n/a
 
      09-25-2006
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);

 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      09-25-2006

"mastermagrath" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
 
 
 
mastermagrath
Guest
Posts: n/a
 
      09-26-2006
Hi Rob,

Thanks so much...this works a treat!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket problem: read & write to same socket liyaohua.bupt@gmail.com Java 19 03-15-2012 12:30 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments