Directly on the code:
Server End:
Copy Code code as follows:
#!/usr/bin/env python
# UDP Echo server-udpserver.py
Import socket, Traceback
Host = '
Port = 54321
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
S.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1)
S.bind (host, Port)
While 1:
Try
message, address = S.recvfrom (8192)
print ' Got data from ', Address, ': ', message
S.sendto (message, address)
Except (Keyboardinterrupt, Systemexit):
Raise
Except
Traceback.print_exc ()
Client side:
Copy Code code as follows:
1 #!/usr/bin/env python
# UDP client-udpclient.py
Import socket, sys
Host = Sys.argv[1]
Textport = sys.argv[2]
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
Try
Port = Int (textport)
Except ValueError:
Port = socket.getservbyname (textport, ' UDP ')
S.connect (host, Port)
While 1:
Print "Enter data to transmit:"
data = Sys.stdin.readline (). Strip ()
S.sendall (data)
Print "Looking for replies; Press CTRL-C or Ctrl-break to stop. "
BUF = S.RECV (2048)
If not Len (BUF):
Break
Print "Server replies:",
Sys.stdout.write (BUF)
print "\ n"