recv and recvfrom, socket programming using python

前端 未结 3 2073
慢半拍i
慢半拍i 2021-01-02 18:04

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

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

I understan

3条回答
  •  不知归路
    2021-01-02 18:43

    You have them switched. TCP sockets should use socket.recv and 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 recvfrom so you know to whom you should send data back. Recvfrom does not work on TCP sockets the same way.

    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).

提交回复
热议问题