I have a problem. I'm trying to make a simple server where several clients can connect & send me stuff, and it should send everything received by the clients to STDOUT. The problem is that accept() is blocking the I/O, even though I made socket FH non-blocking. How can I either make accept() non-blocking or use a different function to accomplish the same thing without blocking? Here's the code:
#!/usr/bin/perl my @sock; my $numsocks = 0; my @message; print "Waiting for client to connect...\n"; use Socket; socket(FH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die $!; setsockopt(FH, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)); my $sin = sockaddr_in(20000, INADDR_ANY); bind(FH, $sin) || die $!; listen(FH, SOMAXCONN); my $read = ''; vec($read, fileno(FH), 1) = 1; select(FH); $| = 1; select(STDOUT); while (1) { #this is where it blocks if ($cn = accept($sock[$numsocks], FH)) { my($port,$iaddr) = sockaddr_in($cn); my $name = gethostbyaddr($iaddr,AF_INET); print "Connection from $name (", inet_ntoa($iaddr), ") on +port $port\n"; vec($read, fileno($sock[$numsocks]), 1) = 1; $numsocks++; select($sock[$numsocks-1]); $| = 1; select(STDOUT); $| = 1; } #uh.. i probably messed up somewhere below.. please excuse its slo +ppiness : ) $read = ''; for($i=0; $i<$numsocks; $i++) { vec($read, fileno($sock[$i]), 1) = 1; } select($readx=$read,undef,undef,undef); for($i=0; $i<$numsocks; $i++) { print "Checking socket $i...\n"; if (vec($readx, fileno($sock[$i]), 1)) { sysread $sock[$i], $a, 10; print "$a"; #my @lines = split(/\n/, $a); #if (@lines > 0) { # $lines[0] = $message[$i] . $lines[0]; # $message[$i] = ""; #} #$message[$i] .= pop(@lines); #foreach $ii (@lines) { # print "$lines[$ii]\n"; #} print "Read socket $i..\n"; } } } exit;

Edit 2001-03-16 by tye to change title (was "Zip")


In reply to accept() on non-blocking socket still hangs by Zip

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.