#SERVER: use strict; use warnings; use 5.010; use Socket; #SERVER: my $protocol = getprotobyname 'tcp'; socket my $LISTEN_SOCK, AF_INET, SOCK_STREAM, $protocol or die "Can't make socket: $!"; setsockopt $LISTEN_SOCK, SOL_SOCKET, SO_REUSEADDR, 1 or die "Cant set SO_REUSADDR: $!"; my $port = 12555; my $listen_addr = sockaddr_in $port, INADDR_ANY; bind $LISTEN_SOCK, $listen_addr or die "bind failed: $!"; listen $LISTEN_SOCK, 5; warn "processing sockets...\n"; my %openStreamsSock; my $streamRequest; my $count = 0; while (1) { # Set up bit vectors for polling my $fin = ''; my $fout; vec ($fin, fileno ($LISTEN_SOCK), 1) = 1; foreach my $streamID (keys %openStreamsSock) { vec ($fin, fileno($openStreamsSock{$streamID}), 1) = 1; } # Wait for incoming message my $nfound = select ($fout=$fin, undef, undef, undef); say "select worked after child ended" if $count == 1; if ($nfound) { if (vec($fout, fileno($LISTEN_SOCK),1)) { say "in 2nd if"; #$openStreamsSock{$streamRequest++} = $LISTEN_SOCK->accept(); my $packed_remote_addr = accept(my $CONNECTION, $LISTEN_SOCK) or warn "Couldn't connect: $!"; say "before 3rd if"; if ($packed_remote_addr) { say 'in 3rd if'; $openStreamsSock{$streamRequest++} = $CONNECTION; } } say 'starting for loop'; foreach my $streamID (keys %openStreamsSock) { say "in for loop after child ended" if $count == 1; if (vec($fout, fileno($openStreamsSock{$streamID}),1)) { # read data off the socket; not a message here, just raw data my $msgSize = sysread ($openStreamsSock{$streamID}, my $msgReceived, 1048576); if ($msgSize > 0) { #writeStreamData ($streamID, $msgReceived); syswrite(STDOUT, $msgReceived); } else { # $msgSize being 0 indicates end of stream, or # $msgSize being undef indicates error, so close #vec($fin, fileno $openStreamsSock{$streamID}, 1) = 0; close ($openStreamsSock{$streamID}); delete ($openStreamsSock{$streamID}); say 'did deleting'; $count = 1; } } } } else { print "$0: Normal timeout of select...\n"; } } #### #CLIENT: use strict; use warnings; use 5.010; use Socket; my $protocol = getprotobyname 'tcp'; socket my $SOCK, AF_INET, SOCK_STREAM, $protocol or die "Couldn't create socket: $!"; my $port = 12555; my $host = 'localhost'; my $packed_host = gethostbyname $host or die "Unknown host: $!"; my $sock_addr = sockaddr_in $port, $packed_host; connect $SOCK, $sock_addr or die "couldn't connect: $!"; my $old_out = select $SOCK; $| = 1; select $old_out; print "Enter some text: "; while (my $to_send = ) { print $SOCK $to_send; } close $SOCK;