File: unix.dgram.server

package info (click to toggle)
jimtcl 0.79%2Bdfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,992 kB
  • sloc: ansic: 155,746; tcl: 4,984; sh: 4,624; cpp: 1,671; makefile: 243
file content (35 lines) | stat: -rw-r--r-- 740 bytes parent folder | download | duplicates (6)
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
# Example of a udp server which sends a response

# Listen on port 20000. No host specified means 0.0.0.0
file delete unix.sock
set s [socket unix.dgram.server unix.sock]

puts "Listening on dgram socket [$s sockname]"

set count 0

# For each request...
$s readable {
	# Get the request (max 80 chars) - need the source address
	set buf [$s recvfrom 80 addr]

	puts -nonewline "read '$buf' from client $addr"

	try {
		set result "$buf = [expr $buf]"
	} on error {msg} {
		set result "Error: $buf => $msg"
	}

	puts ", sending '$result' to client $addr"

	# Send the result back to where it came from
	$s sendto $result $addr
}

# Handle signals so the socket is removed on exit
signal handle SIGINT SIGTERM

catch -signal {
	vwait done
}