| Simple Perl Server |
file.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use strict;
use Socket;
print "Input the number of the port you would like to open.n";
my $port = shift || <STDIN>;
chomp($port);
print "How would you like to greet visitors?n";
my $var1 = <STDIN>;
chomp($var1);
my $proto = getprotobyname('tcp');
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
my $paddr = sockaddr_in($port, INADDR_ANY);
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port ";
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER))
{
my ($client_port, $client_ip) = sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host = gethostbyaddr($client_ip, AF_INET);
print "Got a connection from: $client_host","[$client_ipnum]n";
print CLIENT "$var1";
close CLIENT;
}
Parsed in 0.016 seconds, using GeSHi 1.0.8.6
Please Login to Post a Comment.
Rating is available to Members only.
Please login or register to vote.
Please login or register to vote.
No Ratings have been Posted.


