DanielPHuber has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to write a simple server in a rather standard way:
In princip it works, you can connect with a telnet client, send data from the client to the server and vice versa. But there is a problem sending data from the server to the client. The data is not sent (print $new_sock $_;) until the server recieves a line from the client. Somthing is blocking. As you can see in the inactive lines, I tried all sort if thing to unblock this, to no avail. I seem to miss something concerning blocking of sockets.
use IO::Socket; use warnings; use strict; #use Term::ReadKey; my $flush = 1; my $sock = new IO::Socket::INET ( LocalHost => 'PK5', LocalPort => '7070', Proto => 'tcp', Listen => 1, # max concurrent caller Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; print "SERVER Waiting for client connection on port 7070\n"; my $new_sock = $sock->accept(); # $new_sock->autoflush(1); # so output gets there right +away my $peer_address = $new_sock->peerhost(); my $peer_port = $new_sock->peerport(); # $new_sock->blocking(0); # $| = 1; print "Accepted New Client Connection From : $peer_address, $peer_port +\n"; my $pid = fork(); if (not defined $pid) {print "resources not available.\n";} elsif ($pid == 0) { # child, receiver while(<$new_sock>) { print $_; if($_ eq "end\n"){last;} } exit(0); } else { # parent, sender while(<>){ # $new_sock->flush; # ReadMode 1; print $new_sock $_; # $new_sock->flush; if($_ eq "end\n"){last;} } waitpid($pid,0); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bidirectional Socket, Blocking
by BrowserUk (Patriarch) on Oct 13, 2012 at 20:48 UTC | |
by DanielPHuber (Initiate) on Oct 16, 2012 at 13:33 UTC | |
by BrowserUk (Patriarch) on Oct 16, 2012 at 13:49 UTC | |
by DanielPHuber (Initiate) on Oct 16, 2012 at 17:13 UTC |
Back to
Seekers of Perl Wisdom