rCore-Tutorial-v3/ping.py
2023-02-06 19:22:44 +08:00

19 lines
497 B
Python

import socket
import sys
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = ('localhost', 26099)
sock.bind(addr)
print("pinging...", file=sys.stderr)
while True:
buf, raddr = sock.recvfrom(4096)
print("receive: " + buf.decode("utf-8"))
buf = "this is a ping to port 6200!".encode('utf-8')
sock.sendto(buf, ("127.0.0.1", 6200))
buf = "this is a ping to reply!".encode('utf-8')
sock.sendto(buf, raddr)
time.sleep(1)