我一直在尝试在this tutorial之后创建一个客户端 - 服务器系统,并且出于某种原因,在两台不同的计算机上测试它时,两台计算机将无法连接。
UDP和TCP代码都失败了,我怀疑是因为我无法弄清楚哪个IP转到了哪里。
主持人代码:
import socket
def main():
host = 'ip.ip.ip.ip'
port = 5000
s = socket.socket()
s.bind((host, port))
s.listen(1)
c, addr = s.accept()
print("Connection from: " + str(addr))
while True:
data = c.recv(1024)
if not data:
break
print("From connected user: " + str(data))
data = str(data).upper()
print("Sending " + str(data))
c.send(data)
c.close()
if __name__ == '__main__':
main()
客户代码:
import socket
def main():
host = 'ip.ip.ip.ip'
port = 5000
s = socket.socket()
s.connect((host, port))
message = raw_input("-> ")
while message != 'q':
s.send(message)
data = s.recv(1024)
print("Recived from server: " + str(data))
message = raw_input("-> ")
s.close()
if __name__ == '__main__':
main()