TCP connection close after starting a command session

B

bahat

Hi,
I'm trying to implement a simple client/server application based on
the example given in perlipc.
I added a server command that starts a command session (system("cmd")).
In the client I called this command and immediately called the 'exit'
command, which should close the connection.
What I'm seeing is that although the server closed the socket, the
client appears to wait until the started command session closes before
existing.
When I changed the 'close $client' to 'shutdown($client,2) it worked
but I want to understand what I'm seeing.

Kind Regards,
Lior
 
B

Brian McCauley

(e-mail address removed) wrote:

[ lot's of prose]
When I changed the 'close $client' to 'shutdown($client,2) it worked
but I want to understand what I'm seeing.

Rather than trying to describe what you've done in English, please post
a _minimal_ but _complete_ script to illustrate what you are saying.

It got rather lost as to exactly which bits of what you were describing
were happening on the client and which on the server.
 
B

bahat

Ok. It is just the code from PERLIPC with one added command.
What I'm seeing is that the client is stuck until the command session
opened in the server is closed.

Server
~~~~~
#!/usr/bin/perl -w
use IO::Socket;
use strict;
use Net::hostent; # for OO version of gethostbyaddr

$|=1 ;

my $PORT = 9000; # pick something not in use

my $server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1)
or die "can't setup server";

print "[Server $0 accepting clients]\n"; #$0 == file name

my $client;

while ($client = $server->accept())
{
$client->autoflush(1); # cleans buffer
print $client "Welcome to $0.\n"; #message for client when connected

my $hostinfo = gethostbyaddr($client->peeraddr); #get the client host
printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;

print $client "Command? ";

while ( <$client>)
{
print "Recieved: $_\n" ;
next unless /\S/; # if blank line move next
if (/quit|exit/i) { printf $client "bye bye..."; last; }
elsif (/date|time/i) { printf $client "%s\n", scalar localtime; }
elsif (/mycommand/i) {system("start cmd") ; print $client
"started command " ; }
else {print "Unknown command $_ \n" ; }

print "Command handled\n" ;
}
continue {
print $client "Command? ";
}
close $client || warn $!;
#shutdown($client, 2) ;
printf "Client connection closed\n" ;
}

Client
~~~~
#!/usr/bin/perl -w
use IO::Socket;
my $host = 'server_hostname';
my $port = 9000 ;

my $remote = IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or die "cannot connect to port $port on
$host";

print $remote "mycommand\n" ;
print $remote "exit\n" ;

print "Sent commands. Waiting for server\n" ;
while ( <$remote> ) { print }
print "About to close\n" ;
close $remote
 

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
474,168
Messages
2,570,914
Members
47,455
Latest member
Delilah Code

Latest Threads

Top