Created
September 23, 2015 05:03
-
-
Save chankeypathak/2ea2886db683b154472d to your computer and use it in GitHub Desktop.
Simple chat server in perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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