use IO::Socket::INET; my $port = '7777'; if ( defined $ARGV[0] ) { $port = $ARGV[0] } # auto-flush on socket $| = 1; $SIG{ 'TERM' } = $SIG{ 'INT' } = sub { $socket->close(); }; $SIG{ 'PIPE' } = 'IGNORE'; my $client_socket; # creating a listening socket my $socket = new IO::Socket::INET ( LocalHost => '0.0.0.0', LocalPort => $port, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "cannot create socket $!\n" unless $socket; print "server waiting for client connection on port $port\n"; # waiting for a new client connection $client_socket = $socket->accept(); while(1) { # get information about a newly connected client my $client_address = $client_socket->peerhost(); my $client_port = $client_socket->peerport(); print "connection from $client_address:$client_port\n"; # read up to 1024 characters from the connected client my $data = ""; $client_socket->recv($data, 1024); print "received data: $data\n"; # write response data to the connected client $data = "ok"; $client_socket->send($data); # notify client that response has been sent #shutdown($client_socket, 1); }