【发布时间】:2014-11-18 22:56:58
【问题描述】:
我正在尝试让 python 套接字代码工作。服务器运行良好,但客户端不会绑定到 IP 地址。这是错误:
Traceback (most recent call last):
File "chatClient.py", line 27, in <module>
s.bind((host, port))
File "C:\Panda3D-1.8.1\python\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10049] The requested address is not valid in its context
Press any key to continue . . .
这是代码...
import socket
import threading
import os
import time
tLock = threading.Lock()
shutdown = False
def receving(name, sock):
while not shutdown:
try:
tLock.acquire()
while True:
data, addr = sock.recvfrom(1024)
print str(data)
except:
pass
finally:
tLock.release()
host = '76.106.199.228'
port = 0
server = ('76.106.199.228', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)
rT = threading.Thread(target=receving, args=("RecvThread",s))
rT.start()
alias = raw_input("Name: ")
message = raw_input(alias + ": ")
while message != 'q':
if message != '':
s.sendto(alias + ": " + message, server)
tLock.acquire()
message = raw_input(alias + ": ")
tLock.release()
time.sleep(0.2)
shudown = True
rT.join()
s.close()
代码有什么问题?我一直在谷歌、这个网站和其他一些网站上搜索,但似乎找不到任何东西。当我尝试解决方案时,它们只是不起作用... 感谢您的帮助。
【问题讨论】: