#!/usr/bin/perl use strict; use warnings; use IO::Socket; use IO::Select; #$/ = "\r"; print "Server: Starting server\n"; my $port = 12345; my $host = "localhost"; my $CTIME_String = localtime(time); my $listen_socket = IO::Socket::INET->new( Listen => 1, LocalPort => $port, LocalHost => $host, Proto => 'tcp', Blocking => 0, ) or die "Error during creation of listening socket: $!\n"; my $select = new IO::Select($listen_socket); print "Server: Waiting for client connection on port $port\n"; while (my @ready = $select->can_read) { foreach my $connection (@ready) { if ($connection == $listen_socket) { my $client = $listen_socket->accept(); print "Server: Client $client accepted\n"; $select->add($client); } else { my $data=''; $data = <$connection>; if ($data) { $CTIME_String = localtime(time); print "Server: Received: $CTIME_String: $data"; #$connection->send("SERVER: " . $data); } else { print "Server: Closing connection to $connection\n"; $select->remove($connection); close($connection); } } } }