Skip to content

Instantly share code, notes, and snippets.

@nicwolff
Created December 15, 2016 23:37
Show Gist options
  • Save nicwolff/0549a79e56af5f4a97c2706c220f8ac4 to your computer and use it in GitHub Desktop.
Save nicwolff/0549a79e56af5f4a97c2706c220f8ac4 to your computer and use it in GitHub Desktop.
Simple Perl test TCP server
#!/usr/bin/perl
use Socket;
# Set up listener
$on_port = $ARGV[0] || 25;
$proto = getprotobyname("tcp");
socket (Server, PF_INET, SOCK_STREAM, $proto) || die ("socket");
setsockopt(Server, SOL_SOCKET,SO_REUSEADDR,1) || die ("setsockopt");
$pbind = sockaddr_in($on_port, $ARGV[1] ? inet_aton($ARGV[1]) : INADDR_ANY) || die ("sockaddr_in");
bind(Server,$pbind) || die ("Can't bind: $!");
listen(Server,SOMAXCONN) || die ("Can't listen: $!");
# await a contact
while ($paddr = accept(Client, Server)) {
print Client "Hi there\n";
close Client;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment