Hello world/Web server: Difference between revisions
Content deleted Content added
Added PicoLisp |
Added Perl implementation |
||
Line 107: | Line 107: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Perl}}== |
|||
<lang Perl>use Socket; |
|||
my $port = 8080; |
|||
my $protocol = getprotobyname( "tcp" ); |
|||
socket( SOCK, PF_INET, SOCK_STREAM, $protocol ) or die "couldn't open a socket: $!"; |
|||
# PF_INET to indicate that this socket will connect to the internet domain |
|||
# SOCK_STREAM indicates a TCP stream, SOCK_DGRAM would indicate UDP communication |
|||
setsockopt( SOCK, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "couldn't set socket options: $!"; |
|||
# SOL_SOCKET to indicate that we are setting an option on the socket instead of the protocol |
|||
# mark the socket reusable |
|||
bind( SOCK, sockaddr_in($port, INADDR_ANY) ) or die "couldn't bind socket to port $port: $!"; |
|||
# bind our socket to $port, allowing any IP to connect |
|||
listen( SOCK, SOMAXCONN ) or die "couldn't listen to port $port: $!"; |
|||
# start listening for incoming connections |
|||
while( accept(CLIENT, SOCK) ){ |
|||
print CLIENT "Goodbye, world!"; |
|||
close CLIENT; |
|||
}</lang> |
|||
Various modules exist for using sockets, including the popular IO::Socket which provides a simpler and more friendly OO interface for the socket layer. I refrained from using this module in order to use only what is provided by the average default Perl installation. |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |