我從http://www.evolt.org/node/60276得到這個代碼,並修改它來聽一個「1」,從另一個側面來網絡程序崩潰
但每當我運行這個程序,它停止和Python IDLE去無響應的「數據1,地址= UDPSock.recvfrom(1024)」
def get1():
# Server program, receives 1 if ball found
# ff1 is file w/ received data
import socket
import time
# Set the socket parameters
host = "mysystem"
port = 21567
#buf = 1024
addr = (host,port)
# Create socket (UDP) and bind to address
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
UDPSock.bind(addr)
# Receive messages
while 1:
print "waiting..............."
data1,addr = UDPSock.recvfrom(1024)
print "got 1"
if not data1:
print "Client has exited!"
break
else:
print "\nReceived message '", data1,"'"
UDPSock.close() # Close socket
print "socket closed\n"
#call some other function that uses 1
和客戶端
def send1():
# Client program, sends 1 if ball found
# mf1 is file with data to be sent
import socket
# Set the socket parameters
host = "mysystem"
port = 21567
buf = 1024
addr = (host,port)
# Create socket (UDP)
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
mf1=1
print mf1
# Send messages
if(UDPSock.sendto(str(mf1),addr)):
print "Sending message '",str(mf1),"'....."
# Close socket
UDPSock.close()
沒有人知道什麼可能是這個原因嗎? (對不起,很抱歉)
您發佈的版本效果不錯,您遇到的問題不會顯示給我。如果你的版本不起作用,爲什麼不發佈它,而不是它的版本? – Omnifarious 2010-08-06 01:08:52