I tried to look into it further but you code is truncated and won't compile.

Oops! I had missed that, thanks.
Below is the cycle subroutine with the truncated parts, for anyone kind enough to try to figure this out:

sub cycle { my $self = shift; my (@new_readable) = $self->{readable_handles}->can_read(.1); foreach my $socket (@new_readable) { if ($socket == $self->{main_socket}){ #new connection my $new_socket = $socket->accept(); #Make it non-blocking #fcntl($new_socket, F_SETFL(), O_NONBLOCK()); $self->{players}->{$new_socket} = { read => 0, message => '' }; $self->{readable_handles}->add($new_socket); print "Added new connection\n"; #added to list of connections #may not have data yet } else { #check for connection closure if (not eof($socket)){ my $message = ''; my $read_count; #Read from socket #First, get size #1) Does this always work? #2) How can we get rid of the hardcoded sizeof(int)? $read_count = sysread($socket, $message, 4); if($read_count < 4) { print "Nope, can't guarantee getting message_lengt +h\n"; } else { #store length of incoming #Note that we'll have to use pack/unpack to make i +nt $self->{players}->{$socket}->{read} = unpack("l",$ +message); $self->{players}->{$socket}->{message}=""; print "Got incoming length: $message\n"; $read_count = sysread($socket, $message, $self->{p +layers}->{$socket}->{read}); #was missing end of this line if($read_count < $self->{players}->{$socket}->{rea +d}){ print "Only got $read_count of $self->{players +}->{$socket}->{read}\n"; #was missing end of this line } else { print "Got message: $message\n"; $self->{players}->{$socket}->{read}=0; } } } else { #closed socket, remove from list $self->{readable_handles}->remove($socket); print "Another closed socket\n"; } #end checking for socket closure } #end checking for main/non-main handle } #end foreach }

In reply to RE:(4) More sockets (sob!) by swiftone
in thread More sockets (sob!) by swiftone

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.