J
jk
Hello, I am trying to write a client-server program. Without going
into details, I'm simply reading the terminal on the server and
sending the contents to the client. I'm having an issue where neither
the client nor server are autoflushing their sockets by default, and
the client won't readlines from the server, they come as a big chunk
only after the server fails.
I have the same issue on Mac OS 10.5 (perl: 5.8.8) and Ubuntu (kernel
2.6.27) (perl: 5.10.0)
Here is some base code:
tserver.pl:
#! /usr/bin/perl -w
use strict;
use Socket;
use IO::Handle;
my $port = shift || 7890;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
"socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
bind(SERVER, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";
while(not accept(CLIENT, SERVER)){}
if(fork()){
CLIENT->autoflush(1);
print CLIENT "Who is this?\n";
my $line = <CLIENT>;
print $line;
my ($rpid, $rid, $rhost) = split(/:/, $line, 3);
print CLIENT "Hello, $rid\n";
} else {
while(<> ne "quit"){}
}
wait();
close CLIENT;
tclient.pl:
#! /usr/bin/perl -w
use strict;
use Socket;
use FileHandle;
my $host = shift || 'localhost';
my $port = shift || 7890;
socket(SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
"socket: $!";
connect(SOCKET, sockaddr_in($port, inet_aton($host)) ) or die
"connect: $!";
SOCKET->autoflush(1);
print SOCKET "JIM:TEST:".`hostname`."\n";
print SOCKET "WHOOO\n";
print <SOCKET>;
close SOCKET or die "close: $!"
If the first autoflush is removed (in the server) no text appears on
the client if the server is killed. If the autodlush in the client is
removed, no text appears on the server. Ok, so that's not what the
docs say (sockets should autoflush, right?) but I'm sure this is a
weird, well know exception, I can live with that, but I'd like to know
what the exception is.
The other problem is that the print <SOCKET> in the client prints both
lines, and blocks until the connection is lost. The $line = <CLIENT>
line in the server correctly returns the first line sent from the
client, immediately and solely. Why doesn't the client correctly read
lines from the server, and instead block until the connection is lost.
Thanks so much for any help. I've been banging my head against my
desk for a few days looking at docs and forums. I'm sorry if this is
just some small thing I'm not seeing.
Thanks,
Jim
into details, I'm simply reading the terminal on the server and
sending the contents to the client. I'm having an issue where neither
the client nor server are autoflushing their sockets by default, and
the client won't readlines from the server, they come as a big chunk
only after the server fails.
I have the same issue on Mac OS 10.5 (perl: 5.8.8) and Ubuntu (kernel
2.6.27) (perl: 5.10.0)
Here is some base code:
tserver.pl:
#! /usr/bin/perl -w
use strict;
use Socket;
use IO::Handle;
my $port = shift || 7890;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
"socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
bind(SERVER, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";
while(not accept(CLIENT, SERVER)){}
if(fork()){
CLIENT->autoflush(1);
print CLIENT "Who is this?\n";
my $line = <CLIENT>;
print $line;
my ($rpid, $rid, $rhost) = split(/:/, $line, 3);
print CLIENT "Hello, $rid\n";
} else {
while(<> ne "quit"){}
}
wait();
close CLIENT;
tclient.pl:
#! /usr/bin/perl -w
use strict;
use Socket;
use FileHandle;
my $host = shift || 'localhost';
my $port = shift || 7890;
socket(SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
"socket: $!";
connect(SOCKET, sockaddr_in($port, inet_aton($host)) ) or die
"connect: $!";
SOCKET->autoflush(1);
print SOCKET "JIM:TEST:".`hostname`."\n";
print SOCKET "WHOOO\n";
print <SOCKET>;
close SOCKET or die "close: $!"
If the first autoflush is removed (in the server) no text appears on
the client if the server is killed. If the autodlush in the client is
removed, no text appears on the server. Ok, so that's not what the
docs say (sockets should autoflush, right?) but I'm sure this is a
weird, well know exception, I can live with that, but I'd like to know
what the exception is.
The other problem is that the print <SOCKET> in the client prints both
lines, and blocks until the connection is lost. The $line = <CLIENT>
line in the server correctly returns the first line sent from the
client, immediately and solely. Why doesn't the client correctly read
lines from the server, and instead block until the connection is lost.
Thanks so much for any help. I've been banging my head against my
desk for a few days looking at docs and forums. I'm sorry if this is
just some small thing I'm not seeing.
Thanks,
Jim