《计算机网络(自顶向下方法)》学习到第二章,章末实践一下。
UDPClient.py
__author__ = 'yang'
import socket
serverName = 'hostname'
serverPort = 12000
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = raw_input('Input lowercase sentence')
clientSocket.sendto(message,(serverName,serverPort))
modifiedMessage, serverAdress = clientSocket.recvfrom(2048)
print(modifiedMessage)
clientSocket.close()
UDPServer.py
__author__ = 'yang'
import socket
serverPort = 12000
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSocket.bind(('',serverPort))
print "The server is ready to receive"
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper() + 'from server'
hostname处修改为本机主机名即可
首先运行UDPServer程序,再运行UDPClient。