How to get over 1440 through a socket

I wrote two simple program servers and a client using sockets in C ++ (Linux). And initially it was an example client-server application (an echo message sending and receiving a response). Then I changed the client to implement HTTP GET (now I no longer use my sample server). It works, but no matter what buffer size is set, the client receives only 1440 bytes. I want to get the whole page to the clipboard. I think this applies to TCP properties, and I have to implement some kind of loop inside my client code to capture all parts of the response. But I don’t know what exactly I should do.

This is my code:

... int bytesSent = send(sock, tmpCharArr, message.size()+1, 0); // Wait for the answer. Receive it into the buffer defined. int bytesRecieved = recv(sock, resultBuf, 2048*100, 0); ... 

2048 * 100 is the size of the buffer, and I think this is more than enough for a relatively small WEB page used for testing. But, as I mentioned, I only get 1440 bytes.

What should I do with the recv () function call to capture all parts of the response when the server response is more than 1440 bytes?

Thanks in advance.

+4
source share
2 answers

The size of the buffer is determined by factors outside your control (routers, ADSL links, IP stacks, etc.). The standard way to transfer large amounts of data is to call recv() again.

+6
source

HTTP works over TCP and better understands the operation of TCP sockets, so you need to think of them as a stream, not packets.

For clarity, read my previous post: recompile TCP packet with flash sockets

As for why you only get 1400 (or so) bytes, you have to understand MTU and fragmentation. To summarize, MTU (Maximum Transmission Unit) is the ability of a network to transmit one packet of a certain maximum size. The network-wide MTU is the lowest MTU of all routers involved. Fragmentation is packet splitting if you are trying to send one packet larger than the MTU of this network.

For a better understanding of MTU and fragmentation, read: http://www.miislita.com/internet-engineering/ip-packet-fragmentation-tutorial.pdf

Now, how to get the whole page in the buffer, one of the alternatives is to continue calling recv() and add the data you get in the buffer until recv() returns zero . This will work, because normally the web server closes the TCP connection after sending the response. However, this method will not work if the web server does not close the session (perhaps keep-alives are supported).

So the correct solution would be to keep getting until you get the HTTP header. Take a look and determine the length of the entire HTTP response ( Content-Length: , and then you can continue to receive until you get the exact number of bytes that you should have received.

+1
source

All Articles