【发布时间】:2018-11-30 10:18:45
【问题描述】:
This is the server side
import socket
UDP_IP = 'localhost'
UDP_PORT = 6000
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
if data:
sock.sendto(data.upper(),(UDP_IP,UDP_PORT) )
sock.sendto(UDP_IP,(UDP_IP,UDP_PORT))
#print "sending"
这是客户端 导入套接字
UDP_IP = 'localhost'
UDP_PORT = 6000
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
message = raw_input("Your Message: ")
sock.sendto(message, (UDP_IP, UDP_PORT))
data=sock.recvfrom(1024)
print "receiving" #this line when running it does not print
localtime= time.asctime(time.localtime(time.time()))
print("Received:",data,"at",localtime)
sock.close()
如何修复客户端的代码以从服务器端接收数据?因为它没有打印出已经收到的数据。
【问题讨论】:
标签: python sockets network-programming