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); }

In reply to Bidirectional Socket, Blocking by DanielPHuber

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.