File: listen_and_display.pl

package info (click to toggle)
xastir 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,676 kB
  • sloc: ansic: 121,100; perl: 7,529; sh: 5,535; makefile: 383; python: 312; sql: 102
file content (46 lines) | stat: -rwxr-xr-x 924 bytes parent folder | download | duplicates (2)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/perl
use warnings;

# listen_and_display.pl:  Create a listener port which dumps anything received to STDOUT

$server_port = 2024;

$| = 1;

use Socket;
use IO::Handle;
use POSIX qw(:errno_h);

# 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)) {

  printf("\n*** A client connected on port $server_port ***\n");

  CLIENT->autoflush(1);

  while (<CLIENT>) {
    print;
  }

  printf("\n*** The client disconnected ***\n");

}

close(SERVER);