简体   繁体   English

超时套接字的替代方案

[英]Alternative to timeout socket

Is there an alternative technique of timeout to interrupt communication with a server in case it has received all the messages?如果它已收到所有消息,是否有另一种timeout技术来中断与服务器的通信?

Let me explain, I want to communicate through an SSL socket with a server (XMPP) unknown to me.让我解释一下,我想通过 SSL 套接字与我未知的服务器 (XMPP) 进行通信。 The communication is done through XML messages.通信是通过 XML 消息完成的。 For each request I can receive one or more response messages of different types.对于每个请求,我都可以收到一个或多个不同类型的响应消息。 Or a single message that is sent in multiple chunks.或者以多个块发送的单个消息。 Therefore I create a loop to receive all messages related to a request from the server.因此,我创建了一个循环来接收与来自服务器的请求相关的所有消息。 So I do not know the number and the size of messages for each request a priori.所以我不知道每个请求先验消息的数量和大小。 Once the messages are finished, the client waits for no more responses from the Server to stop listening (ie, it waits for the timeout ).消息完成后,客户端不再等待服务器的响应以停止收听(即等待timeout )。 However, this results in a long wait (eg, for each request I have to wait 2s which at the end of the communication could be as long as 1 minute for all requests).然而,这会导致长时间等待(例如,对于每个请求,我必须等待 2 秒,而在通信结束时,所有请求可能长达 1 分钟)。

Is there a faster way to stop listening when we have received all the messages for each request?当我们收到每个请求的所有消息时,有没有更快的方法停止监听?

I attach here my code:我在这里附上我的代码:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = ssl.wrap_socket(s, ssl_version=ssl. ssl.PROTOCOL_TLSv1_2)
sock.connect((hostname, port))

sock.timeout(2)

#example for a generic unique message request
sock.sendall(message.encode())

data = “”
while True:
  try:
    response = sock.recv(1024)
    if not response: break
    data += response.decode()
  except socket.timeout:
    break

You know how much data to expect because of the protocol.由于协议,您知道预期有多少数据。

The XMPP protocol says ( section 4.2 ) what the start of a stream looks like; XMPP 协议说明(第 4.2 节)stream 的开头是什么样的; ( section 4.3 ) tells you about stream negotiation process which the <stream:features> tag is used for. ( section 4.3 ) 告诉你关于 stream 协商过程,其中 <stream:features> 标签被用于。 Don't understand them yet?还不明白他们? Section 4.1 tells you about what a stream is and how it works. 4.1 节告诉您 stream 是什么以及它是如何工作的。

In one comment, you complained about receiving a response in multiple chunks.在一条评论中,您抱怨收到多块回复。 This is absolutely normal in TCP because TCP does not care about chunks.这在 TCP 中是绝对正常的,因为 TCP 不关心块。 TCP can split up a 1kB message into 1000 1-byte chunks if it feels like it (usually it doesn't feel like it). TCP 可以将 1kB 的消息拆分为 1000 个 1 字节的块,如果它愿意(通常感觉不喜欢)。 So you must keep receiving chunks until you see the end of the message.因此,您必须继续接收数据块,直到看到消息结束为止。

Actually, XMPP is not even a request-response protocol.实际上,XMPP 甚至不是请求-响应协议。 The server can send you messages at any time, which are not responses to something you sent, for example it could tell you that someone said "hello" to you.服务器可以随时向您发送消息,这不是对您发送的内容的响应,例如它可以告诉您有人对您说“你好”。 So you might send an IQ request, but the next message you receive might not be an IQ response, it might be a message saying that your friend is now online.所以你可能会发送一个 IQ 请求,但你收到的下一条消息可能不是 IQ 响应,它可能是一条消息说你的朋友现在在线。 You know which message is the response because: the message type is IQ;您知道哪个消息是响应,因为:消息类型是 IQ; the 'type' is 'result' or 'error'; “类型”是“结果”或“错误”; and the 'id' is the same as in the request.并且“id”与请求中的相同。 Section 8.2.3 .第 8.2.3 节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM