How to indicate the buffer size before calling function recv, WSARecv in socket programming???
-
Hi all, I wrote client-server applicaion communicating via socket. I met a big problem that I did not know which function can be use to indicate the buffer size will be received before calling function recv() or WSARecv(). I want to know this information to allocate the buffer enough to receive the message coming. For example: int bufSize; /*Do which thing here to indicate buffer size bufSize = ???*/ char*buffer = new char[bufSize]; recv(hSOCKET,buffer,bufSize,0); If we dont know this information before calling receiving function, the problem is we won't know how big the buffer size should is. Although, we can do: char buffer[100000]; but the size of coming message can be bigger or the coming message is too smaller than that. Any boddy know this problem, Can you help me? Thanks very much & best regards, Tin Le
-
Hi all, I wrote client-server applicaion communicating via socket. I met a big problem that I did not know which function can be use to indicate the buffer size will be received before calling function recv() or WSARecv(). I want to know this information to allocate the buffer enough to receive the message coming. For example: int bufSize; /*Do which thing here to indicate buffer size bufSize = ???*/ char*buffer = new char[bufSize]; recv(hSOCKET,buffer,bufSize,0); If we dont know this information before calling receiving function, the problem is we won't know how big the buffer size should is. Although, we can do: char buffer[100000]; but the size of coming message can be bigger or the coming message is too smaller than that. Any boddy know this problem, Can you help me? Thanks very much & best regards, Tin Le
Send the buffer size first, then send the actual data.
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
-
Hi all, I wrote client-server applicaion communicating via socket. I met a big problem that I did not know which function can be use to indicate the buffer size will be received before calling function recv() or WSARecv(). I want to know this information to allocate the buffer enough to receive the message coming. For example: int bufSize; /*Do which thing here to indicate buffer size bufSize = ???*/ char*buffer = new char[bufSize]; recv(hSOCKET,buffer,bufSize,0); If we dont know this information before calling receiving function, the problem is we won't know how big the buffer size should is. Although, we can do: char buffer[100000]; but the size of coming message can be bigger or the coming message is too smaller than that. Any boddy know this problem, Can you help me? Thanks very much & best regards, Tin Le
You do not need to know the buffer size before recieving the data, although it would help. C++ solves many problems like this by providing [classes like] the vector template class. What I am trying to say is that you can declare a vector to recieve the data, and just keep calling push_back(...) to stuff the bytes into the buffer. If you have a good idea of how many bytes of data you expect to recieve, then set the initial size to that many bytes (as an added safety measure you might double the number, but why waste memory). The reason you want to set a minimum (or maximum) for expected bytes of data, is that you want to avoid forcing the code to reallocate a new memory block that is large enough to hold all the data. You can do the same thing in C, but C does not provide nice little pieces of code to do it for you. INTP Every thing is relative...