Archived

This topic is now archived and is closed to further replies.

A little winsock/char theory

This topic is 5337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m kinda curious as to how large ammounts of data is recieved using windows sockets with the recv function. My code for using the recieve function looks like this:
  
//Check for data

		char buffer[255];
		for(int i=0;i<256;i++) { buffer[i]=0x00;} //Clear the buffer

		recv(client,buffer,sizeof(buffer),0);
  
BUT my buffer is only 255 bytes. What if I wanted to recieve larger ammounts of data? would I have to make the size of buffer much larger? (eg buffer[10000] for a mb of data)? or does recv somehow split it up to make it fit in the buffer variable only I get different parts of the data (in 255 byte increments) everytime I call recv.. Thanks ----- I 4M T3H SIGN4TUR3 FR0M H311x0res!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Receive it into a buffer and append it into whatever you need, like a string, or parse it out into a structure.

Share this post


Link to post
Share on other sites
The way you are doing it, is the way it is done. To receive large amounts of data, you just resize the buffer. But, try to keep the packet size small, preferable 512 or smaller. And to optimize your code a little, you could do this:


  
char szReceiveBuff[512]; // Our receive buffer


memset(szReceiveBuff, 0, sizeof(szReceiveBuff);
recv(Socket, szReceiveBuff, sizeof(szReceiveBuff), 0);


memset just sets all memory inside szReceiveBuff to 0.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
quote:
Original post by Toolmaker
memset(szReceiveBuff, 0, sizeof(szReceiveBuff);   




Or you could use
ZeroMemory(szReceiveBuff, sizeof(szReceiveBuff));   


[edited by - MainForze on May 6, 2003 8:02:01 AM]

[edited by - MainForze on May 6, 2003 8:05:35 AM]

Share this post


Link to post
Share on other sites
Why waste time clearing the buffer at all? recv tells you how many bytes you overwrote, so you know exactly how much of the buffer is used.

To answer the question, the TCP/IP stack has its own buffer and returns as much of it to you as you ask for. So if it has more data waiting than your buffer allows, you''ll get the next bit of data next time. That''s under TCP though - I''m not sure how UDP works, but setting your buffer size to at least equal your MTU (maximum transmission unit) should mean you always have enough space.

Of course, the buffer is just a temporary thing - you usually read into the buffer and then copy out from it into your destination structure. If you want to read directly into the structure, you''re going to have to see how much you managed to read, then start the next recv from that point in the structure.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites