How the recv() function actually works?

by Stark   Last Updated August 13, 2019 20:26 PM - source

I am trying to figure out how the recv() functions in sockets actually work.

Here is what i tried I created a buffer that receives x amount of bytes; in the first case I made it 8192 (to figure out the size of the stream i am receiving).


rfc_number = 2340
host = 'www.ietf.org'
port = 80
sock = socket.create_connection((host, port))

# HTML Request
req = (
 'GET /rfc/rfc{rfcnum}.txt HTTP/1.1\r\n'
 'Host: {host}:{port}\r\n'
 'User-Agent: Python {version}\r\n'
 'Connection: close\r\n'
 '\r\n'
)

#Formatting the request
req = req.format(
 rfcnum=rfc_number,
 host=host,
 port=port,
 version=sys.version_info[0]
)

# Loop for buffering stream
while True:
    buf = sock.recv(8192‬)
    print(len(buf))
    if not len(buf):
        break

Output:

724
5
0

The question is: What are those 5 bytes at the end? Why isn't the whole stream returned in the first recv(8192), in which case the output would be 729 0?



Related Questions


How to recieve data from a raw socket in Python?

Updated December 02, 2017 14:26 PM

Python TCP Server not accepting C++ Clients

Updated August 03, 2017 06:26 AM