Python连续TCP连接 [英] Python continuous TCP connection
问题描述
我是套接字编程的新手,正在尝试重新使用python。我想编写一个简单的TCP程序,该程序将持续保持连接,直到达到某种最终状态为止,在这种情况下,客户端将发送关闭。
I'm new to socket programming and trying to get back into python. I wanted to write a simple TCP program which will continuously maintain a connection until some end state is reached, in this case "close" is sent by the client.
第一次迭代就可以了,但是它冻结了我发送的第二个东西,我不确定为什么。有人可以解释为什么我的程序死机或如何更好地实现吗?
This works fine for the first iteration, but it freezes on the second thing I send and I'm not sure why. Could someone please explain why my program freezes or how better implement this?
TCPServer.py
TCPServer.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print('The server is ready to recieve')
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
if(sentence == "close"):
connectionSocket.close()
capSentence = sentence.upper()
connectionSocket.send(capSentence.encode())
TCPClient.py
TCPClient.py
from socket import *
serverName = 'localhost'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
while(1):
sentence = raw_input('input sentence: ')
if(sentence == "close"):
break
clientSocket.send(sentence.encode())
retSentence = clientSocket.recv(1024)
print('From server; ', retSentence.decode())
clientSocket.close()
推荐答案
服务器存在多个问题。我会尝试指出我所看到的。
There are multiple problems with the server. I'll try to point out what I see.
它冻结了您发送的第二件事,因为如果服务器再次尝试从客户端套接字读取,则服务器永远不会尝试收到的数据不等于关闭
。您需要另一个循环来完成此操作,例如:
It freezes on the second thing you send because the server never attempts to read from the client socket a second time if the received data is not equal to "close"
. You need another loop to accomplish this, such as:
while True:
connectionSocket, addr = serverSocket.accept()
while True:
sentence = connectionSocket.recv(1024).decode()
if sentence == "close": break
capSentence = sentence.upper()
connectionSocket.send(capSentence.encode())
connectionSocket.close()
另外,请注意,服务器同步运行循环,因此直到第一个连接关闭,它才能接受第二个连接。
Additionally, note that the server runs through the loops synchronously, and so it cannot accept a second connection until the first one is closed.
最后,您缺少错误处理代码。如果客户端过早断开连接,则整个服务器将崩溃。
Finally, you are missing error handling code. The whole server will crash if a client prematurely disconnects.
如果您的目标是让服务器仅处理单个连接,然后终止,最简单的解决方案是将 accept()
调用移到主循环之外(否则,请等待 new (每次迭代)。
If your goal is to have the server only handle a single connection and then terminate, the simplest solution is to move the accept()
call outside of the main loop (otherwise you wait for a new connection on each iteration).
connectionSocket, addr = serverSocket.accept()
# Destroy the server socket; we don't need it anymore since we are not
# accepting any connections beyond this point.
serverSocket.close()
while True:
sentence = connectionSocket.recv(1024).decode()
if(sentence == "close"):
connectionSocket.close()
break
capSentence = sentence.upper()
connectionSocket.send(capSentence.encode())
这篇关于Python连续TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!