17.2. Writing a TCP ServerProblemYou want to write a server that waits for clients to connect over the network to a particular port. SolutionThis recipe assumes you're using the Internet to communicate. For TCP-like communication within a single Unix machine, see Recipe 17.6. Use the standard (as of 5.004) IO::Socket::INET class: use IO::Socket;
$server = IO::Socket::INET->new(LocalPort => $server_port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 ) # or SOMAXCONN
or die "Couldn't be a tcp server on port $server_port : $@\n";
while ($client = $server->accept()) {
# $client is the new connection
}
close($server);Or, craft it by hand for better control: use Socket;
# make the socket
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
# so we can restart our server quickly
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
# build up my socket address
$my_addr = sockaddr_in($server_port, INADDR_ANY);
bind(SERVER, $my_addr)
or die "Couldn't bind to port $server_port : $!\n";
# establish a queue for incoming connections
listen(SERVER, SOMAXCONN)
or die "Couldn't listen on port $server_port : $!\n";
# accept and process connections
while (accept(CLIENT, SERVER)) {
# do something with CLIENT
}
close(SERVER);DiscussionSetting up a server is more complicated than being a client. The optional The numeric argument to The use Socket;
while ($client_address = accept(CLIENT, SERVER)) {
($port, $packed_ip) = sockaddr_in($client_address);
$dotted_quad = inet_ntoa($packed_ip);
# do as thou wilt
}With the IO::Socket classes, while ($client = $server->accept()) {
# ...
}If you call the while (($client,$client_address) = $server->accept()) {
# ...
}If no connection is waiting, your program blocks in the use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
$flags = fcntl(SERVER, F_GETFL, 0)
or die "Can't get flags for the socket: $!\n";
$flags = fcntl(SERVER, F_SETFL, $flags | O_NONBLOCK)
or die "Can't set flags for the socket: $!\n";Now, when you You might fear that when the return flags from F_GETFL are 0, that this would trigger the See AlsoThe
|