Buffer related to socket.recv()


Size of the buffer and socket.recv() 

Let's look at the sample code below. 
This is a simple server side code that will send "Welcome to the server" when a client connects to the server. 

Now let's look at client side code.
This will receive the message sent by the server with a buffer size of 8. That means whenever a message that is longer than 8 bytes long will be divided into pieces.


We can see that the maximum size of the message is the buffer. So, programmer must ensure all the data has been received properly. This can be done simply utilizing a variable called HEADERSIZE. This code was from sentdex, who is really a great programmer and a great tutor. 

Now let's look at the server side code with HEADERSIZE 
We will simply send a message that includes length of the message and set a header which has a length of HEADERSIZE(If the size of the message is greater than 10 digits.... looking for other methods is recommended). The message will look like this.


So, from the client's perspective, we can expect the size of the message is 21 and using that information we can receive exact message sent from the client. 

cf ) Python library pickle helps us with a more easier way to serialize and de-serialize various python objects. 

REFERENCE 

Comments

Popular posts from this blog

TCP with Python

Operating System - Learn Thread with C/C++, Python

UDP with Python