Skip to content

Commit f957f39

Browse files
committed
socket UDP
1 parent 8913607 commit f957f39

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

socket/basic-UDP/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# UDP example
2+
3+
Minimal UDP server for single client.
4+
5+
You can test with `netcat` as client with `-u` for UDP
6+
7+
````
8+
$ nc -u localhost 3000
9+
````
10+
11+
---
12+
13+
- [netcat Homepage](http://netcat.sourceforge.net/)
14+
- [netcat Wikipedia](https://en.wikipedia.org/wiki/Netcat)

socket/basic-UDP/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# you can test with `netcat` as client with `-u` for UDP
5+
#
6+
# $ nc -u localhost 3000
7+
#
8+
9+
import socket
10+
11+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
12+
s.bind(('127.0.0.1', 3000))
13+
14+
while True:
15+
data, address = s.recvfrom(1024)
16+
print(data.decode('ascii'), end='')
17+
s.sendto(data, address)

0 commit comments

Comments
 (0)