我从服务器向我的客户发送了一些请求,但我遇到了一些问题。
当我向客户发送消息时,如果我发送了很多消息,我 socket.recv()
是否有办法逐个获取消息?
由于
答案 0 :(得分:1)
You need to use some kind of protocol over otherwise bare sockets.
See python twisted
or use something like nanomsg
or ZeroMQ
if you want a simple drop-in replacement which is message-oriented.
It is not transport-agnostic though, meaning they will only work if they are used on both ends.
答案 1 :(得分:0)
No. TCP is a byte stream. There are no messages larger than one byte.
答案 2 :(得分:0)
I assume that you are using TCP. TCP is a streaming protocol, not a datagram protocol. This means that the data are not a series of messages, but instead a single data stream without any message boundaries. If you need something like this either switch the protocol (UDP is datagram, but has other problems) or make your own protocol on top of TCP which knows about messages. Typical message based protocols on top of TCP either use some message delimiter (often newline) or prefix each message with its size.