Originally Posted by
azjherben
I know, maybe afraid of virus(es) or something.
More like I'm too lazy to boot Vista. Takes too long to start. (Although now that I'm discovering the joys of virtualization...)
Originally Posted by
azjherben
Well I belive maybe I have an attempt somewhere that is trying to put a quote...
(like "Hi it is Bob Saget.")
That is to big or small into a char.
Like:
char bob[5] = "Wow this is big";
Did you fix your code from the suggestions on the Network board? This segment, specifically:
Code:
// Display message from server
char buffer[1000];
memset(buffer,0,999);
int inDataLength=recv(Socket,buffer,1000,0);
std::cout<<buffer;
First, why do you not zero that last byte?
Second, say I send you more than 1000 characters. recv() will stuff 1000 of them in your buffer, filling it. Then we pass it to std::cout, which requires that the buffer be zero-terminated. However, if I send you 1000 'X', then your buffer is not zero terminated, and you'll get a buffer overflow. (And likely a crash.)
Instead, zero that last byte: (And leave room for that zero.)
Code:
char buffer[1000];
int inDataLength = recv(Socket, buffer, 999, 0);
if(inDataLength <= 0)
{
// Handle error / disconnect.
}
else
{
buffer[inDataLength] = '\0';
std::cout << buffer;
}
...but we're still not there. TCP sockets model a stream. Therefore, there are no 'packets'. If you want a packet, you have to build that yourself - either with some sort of "here's where a packet starts/stops" marker (newline, \0, etc.), or prefix packets with a length. (The latter is more robust in my opinion, and has the advantage that after so many bytes you know how big of a buffer you need.)
Also, you cannot assume that everything will come in one recv(). For one, what if I sent a string of length 2000? Or, what if I sent a string of length 500, but TCP broke the string apart into two pieces during transport, and the second piece was slower than the first? (You would get the second piece on the next call to recv() if it hadn't arrived yet.)