Nayan Sonthalia
Nayan Sonthalia

Reputation: 247

different socket buffer size for client and server

I have a question:

Can we have different socket buffer size for client and server? For example settting the send and receive buffer to 2048 at server and to 13312 at client will have any problem (buffers at sever are of lesser size than clent)? If yes, what will be the issues?

Upvotes: 1

Views: 1993

Answers (1)

Zaboj Campula
Zaboj Campula

Reputation: 3360

I think you are asking about buffers in your application. Buffer used by operating system is a different story.

It is legal to use buffers with different length at client and server. Actually it must be legal because for example web browser has no information buffer size in web server and web server has no idea about client buffer.

But you have to keep in mind that TCP is a stream oriented protocol and it does not keep message boundaries.

For example let client has a buffer with size 10 bytes and sends 3 pieces of data:

send(sock1, "0123456789", 10, 0);
send(sock1, "ABCDEFGHIJ", 10, 0);
send(sock1, "abcdefghij", 10, 0);

The data is transferred in stream and it is up to the underlying TCP stack if they will be transmitted via 3 IP packets:

0123456789      ABCDEFGHIJ      abcdefghij

or one big packet:

0123456789ABCDEFGHIJabcdefghij

or even something more weird:

0123456789A    BCDEFGHIJab     cdefghij

The OS at the receiver side stores all received data in its internal buffer when data is received. OS copy data to application buffer when application calls receive. If receiving application has buffer bigger that size of already received data then all data is copied to application buffer. If application buffer is smaller then OS copies only data that fits to buffer and remaining data will be copied in next receive call.

Upvotes: 2

Related Questions