Re: Write-Only Socket

From: Martin Kissner (news_at_chaos-net.de)
Date: 02/23/05


Date: Wed, 23 Feb 2005 15:06:28 +0100

xhoster@gmail.com wrote :
> Martin Kissner <news@chaos-net.de> wrote:
>> The question is:
>> Why do I have to shutdown(0) $session _and_ $socket on the server?
>
> As far as I can tell, you don't. I get the same behavior whether $socket
> is shutdown or not.

Yes, now I get the same result. I can't reproduce the behaviour I had
last time when I tried.

>> I had expected that the client would not be able to write to his $socket
>> if only $session on the server was shutdown(0)
>
> I don't see that happening regardless of what you shutdown. It looks to me
> like the client is prohibited from writing to the socket only because
> server exits. shutdown(0) does not seem to be enforced on the other
> end of the socket.

Yes, that seems to be true.
I changed my server script so it will not die but warn if 'getline'
fails. Now I get another unexpected result. The server starts reading
from the socket after one loop (Code and output below).
Can anyone explain this?
How could I permanently close the socket for reading?

Here is the new code of the server:

----
#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket;
my $socket = IO::Socket::INET->new(
    Listen      => 1,
    LocalPort   => 1025,
    Proto       => 'tcp',
    Reuse       => 1);
my $session = $socket->accept;
$session->shutdown(0) or die;
while(1) {
    my $read = $session->getline or warn "I can not read: $!\n";
    print $read;
    sleep 1;
}
-----
Here is the output after I had started the client (with comments)
--- start output ---
		# first loop
I can not read: 
Use of uninitialized value in print at ./server.pl line 22.
		# second loop
Client tries to write
Client tries to write
Client tries to write
Client tries to write
		# Here I killed the client
I can not read: 
Use of uninitialized value in print at ./server.pl line 22, <GEN1> line 4.
I can not read: 
Use of uninitialized value in print at ./server.pl line 22, <GEN1> line 4.
		# Here I killed the server
^C
end output ---
Here is the client code (if anybody wants to try it him/herself)
--- client code ---
#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket;
my $socket = IO::Socket::INET->new(
    PeerHost    => 'localhost',
    PeerPort    => 1025,
    Proto       => 'tcp') or die "Connection failed: $!\n";
while (1) {
    print "Client is printing to socket\n" if print $socket "Client
tries to write\n";    sleep 1;
}
--- end ---
-- 
perl -e '$|=1;&p(7.74.117.115.116.32);&s();&p(97.110.111);&p(116.104.101
.114);&s;&p(32.112.101.114.108);&s();&p(32.104.97.99.107.101.114.10);sub
 s{sleep 1};sub p(){print "@_"}'


Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)
  • Re: Design issue with WinSock/GetQueuedCompletionStatus
    ... delegate that to a shutdown routine called after all worker threads ... The application I've created is a server accepting connections on a few ... different TCP/IP ports and then lets the client run different commands. ... a TCP/IP socket can be closed for 2 different reasons: ...
    (microsoft.public.win32.programmer.networks)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... Client client = new Client; ...
    (microsoft.public.vc.language)
  • Re: TCP server stop receiving new connections
    ... reset the event mask of your listening socket each time you ... I have a strange problem in my class library used by all our client ... server applications. ... incomming connections, but keeps current connections. ...
    (microsoft.public.win32.programmer.networks)