我编码了服务器端和客户端,我正在使用TCP连接。
服务器端:
import socket
def Main():
host = '192.168.1.3'
port = 5014
s = socket.socket()
s.bind((host,port))
s.listen(1)
c,addr = s.accept()
while True:
if c.recv(1024).decode('UTF_8') == 'Q':
print('Terminating Connection...')
c.close()
s.close()
break;
else:
print(c.recv(1024).decode('UTF_8'))
if __name__ == '__main__':
Main()
客户端:
import socket
def Main():
host = '192.168.1.3'
port = 5014
c = socket.socket()
c.connect((host,port))
data = ''
while True:
if data == 'Q':
break;
else:
data = input('->')
c.send(data.encode('UTF_8'))
print('Terminating Connection...')
c.close()
if __name__ == '__main__':
Main()
似乎一切都是正确的,但在运行时,当我在客户端提供输入时,消息只是交替打印。
实施例: 客户端输入:
->hello1
->hello2
->hello3
->hello4
->Q
Terminating Connection...
服务器端输出:
hello2
hello4
Terminating Connection...
我不知道为什么hello1和hello3没有打印出来。 请帮忙。在此先感谢