#!/usr/bin/perl #This is a version that can read and write #from as many clients as your machine supports. use strict; use warnings; use IO::Socket; my $server = IO::Socket::INET->new ( LocalPort => 1337, Type => SOCK_STREAM, Reuse => 1, Listen => 5 ) or die "could not open port\n"; warn "server ready waiting for connections..... \n"; my $client; while ($client = $server->accept()) { my $pid; while (not defined ($pid = fork())) { sleep 5; } if ($pid) { close $client; # Only meaningful in the client } else { $client->autoflush(1); # Always a good idea close $server; &do_your_stuff(); } } sub do_your_stuff { warn "client connected to pid $$\n"; while(my $line = <$client>) { print "client> ", $line; print $client "pid $$ > ", $line; } exit 0; } #### #!/usr/bin/perl use IO::Socket::INET; use strict; my $socket = new IO::Socket::INET(Proto => "tcp", PeerAddr => 192.168.0.15, PeerPort => 1337); while (1) { print $socket "1234\n"; my $msg = <$socket>; print $msg; sleep(5); }