recv 和 recvfrom,使用 python 进行套接字编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36115971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:26:34  来源:igfitidea点击:

recv and recvfrom, socket programming using python

pythonsockets

提问by user859385

i am new to python and new to socket programming as well.

我是 python 的新手,也是套接字编程的新手。

I am confused about about socket.recvfrom()and socket.recv()

我对socket.recvfrom()和感到困惑socket.recv()

I understand that usually for UDP, people use recvfrom()and for TCP, people use recv().

我知道通常对于 UDP,人们使用,recvfrom()而对于 TCP,人们使用recv().

For example,

例如,

serverSocketUDP = socket(AF_INET, SOCK_DGRAM)
serverSocketTCP = socket(AF_INET, SOCK_STREAM)
#... define server...
#...
message, clientAddress = serverSocketUDP.recvfrom(2048) #why 2048 for UDP? Ive seen several examples like this.
message2 = serverSocketTCP.recv(1024) #Again, why 1024 for TCP? 

As seen in the example above, what i am confused about is the numbers. Why 2048 and 1024 for different protocols? What does these numbers represent? Please explain. I hope i was clear enough. Thank you.

如上例所示,我感到困惑的是数字。为什么 2048 和 1024 用于不同的协议?这些数字代表什么?请解释。我希望我足够清楚。谢谢你。

回答by tdelaney

Why 2048 and 1024 for different protocols?

为什么 2048 和 1024 用于不同的协议?

Those are very arbitrary numbers and depend on the protocol being implemented. And even though the TCP number works, the UDP number you give is very likely wrong.

这些是非常随意的数字,取决于正在实施的协议。即使 TCP 编号有效,您提供的 UDP 编号也很可能是错误的。

TCP implements a stream protocol that you can read in any sized chunk you want. You could do recv(1)to get a byte at a time or recv(100000)if you want to grab large chunks. recvis free to return smaller blocks that you ask for, so you may get a different size than you want. 1024 is pretty small, you could read much larger chunks without a problem.

TCP 实现了一个流协议,您可以读取任何您想要的大小的块。您可以recv(1)一次获取一个字节,或者recv(100000)如果您想获取大块。recv可以自由地返回您要求的较小的块,因此您可能会得到与您想要的不同的大小。1024 非常小,您可以毫无问题地读取更大的块。

UDP implements a message protocol. You have to ask for enough bytes to cover the entire message or it will be dropped. What that size is depends on the protocol. Its common for protocols to limit messages to 1500 (the max size of a standard ethernet packet) but it could be anything up to 65535. Check the actual protocol specs for maximums.

UDP 实现了一种消息协议。您必须要求足够的字节来覆盖整个消息,否则它将被丢弃。大小取决于协议。它通常用于将消息限制为 1500(标准以太网数据包的最大大小)的协议,但它可以是任何高达 65535 的任何东西。检查实际协议规范的最大值。

回答by Goodies

You have them switched. TCP sockets should use socket.recvand UDP sockets should use socket.recvfrom. This is because TCP is a connection-oriented protocol. Once you create a connection, it does not change. UDP, on the other hand, is a connectionless ("send-and-forget") protocol. You use recvfromso you know to whom you should send data back. Recvfrom does not work on TCP sockets the same way.

你让他们换了。TCP 套接字应该使用socket.recv,UDP 套接字应该使用socket.recvfrom. 这是因为 TCP 是面向连接的协议。创建连接后,它不会更改。另一方面,UDP 是一种无连接(“发送即忘记”)协议。您使用recvfrom它是为了知道应该将数据发送给谁。Recvfrom 不能以同样的方式在 TCP 套接字上工作。

As for the 1024/2048, these represent the number of bytes you want to accept. Generally speaking, UDP has less overhead than TCP allowing you to receive more data, but this is not a strict rule and is almost negligible in this context. You can receive as much or as little as you would like. 4096 is common as well (for both).

至于 1024/2048,这些代表你想要接受的字节数。一般来说,UDP 的开销比 TCP 少,允许您接收更多的数据,但这不是一个严格的规则,在这种情况下几乎可以忽略不计。您可以随心所欲地收到多少。4096 也很常见(对于两者)。

回答by akash12300

I think people usually use recvfrom for UDP. Because in TCP, once the connection gets established, the address information does not change and hence recvfrom always returns None for connection-information field.

我认为人们通常将 recvfrom 用于 UDP。因为在 TCP 中,一旦建立连接,地址信息不会改变,因此 recvfrom 总是为连接信息字段返回 None 。

And in the above code, it will error out in this line:

在上面的代码中,它会在这一行出错:

message2, clientAddress2 = serverSocketTCP.recv(1024)

Because: recvfrom() returns (data, connection_information), and recv() returns just the data. So it will raise ValueError because you are trying to unpack a non-tuple value.

因为:recvfrom() 返回(data, connection_information),而recv() 只返回数据。所以它会引发 ValueError 因为你试图解包一个非元组值。

1024, or 2048 just defines the buffer size but it does not wait for exactly that much amount of data before returning. For example:

1024 或 2048 只是定义了缓冲区大小,但它不会在返回之前完全等待那么多数据量。例如:

#One side, 'receiver' is a socket
receiver.recv(2048)

#Second side, 'sender' is a socket
sender.send('abc'.encode('utf-8'))

Clearly the data sent by 'sender' is much less than 2048 bytes but the 'recv' call will return immediately after it receives the data sent to it from 'sender'

显然,'sender' 发送的数据远小于 2048 字节,但 'recv' 调用将在收到从 'sender' 发送给它的数据后立即返回