Skip to content

Instantly share code, notes, and snippets.

@chankeypathak
Created September 23, 2015 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chankeypathak/2ea2886db683b154472d to your computer and use it in GitHub Desktop.
Save chankeypathak/2ea2886db683b154472d to your computer and use it in GitHub Desktop.
Simple chat server in perl
#!/usr/bin/perl -w
# chat_server.pl
use strict;
use IO::Socket::INET;
my $port = shift or die "Port required!\n";
my $socket = IO::Socket::INET->new(
LocalPort => $port,
Proto => 'tcp',
Listen => SOMAXCONN
) or die "Can't create socket: $!!\n";
my $child;
print "Listening for clients on $port...\n";
REQUEST:
while(my $client = $socket->accept) {
my $addr = gethostbyaddr($client->peeraddr, AF_INET);
my $port = $client->peerport;
if($child = fork) {
print "New connection from $addr:$port\n";
#close $client;
#next REQUEST;
} die "fork failed!\n" unless defined $child;
while (<$client>) {
print "[$addr:$port] says: $_";
print $client "[$addr:$port] says: $_";
}
}
close $socket;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment