I am using Winsock GetData and it appears that the data being returned is getting truncated. The data is a string and it will appear truncated in the middle of a word or sentence. It seems very simple:
It doesn't seem to make a difference. Is there a certain length that a string can be in general? I can't imagine I am hitting it, but it would explain it. Is there another data type that can be used?
If the data is large, yes it will be chopped up into multiple sends, hence multiple data arrivals, hence your problem.
At the data arrival side, concatenate getdata for the data arrivals until the entire length has been downloaded. To determine if you've received the entire length; send additional header info (total data length in bytes) at the start of send or send special string on send complete or some other implementation.
Basically I do a SendData and then GetData the response over UDP. I have no idea the length of the response from the server and I will not necessarily know if it is chopped off until I am done processing it.
I am new to Winsock and if there is anyway you could give me a sample of what you are talkling about that would be appreciated.
At my company, we use a 52 byte header on all of our records that are sent via socket connections. I use the header in all of my C, VB and Java sockets apps.
Among other things, the header contains a long whose value is the sum of the header length and record length. One of my VB apps gets a record from a C server on another platform that is over 18,500 bytes long. As I recall, when I debugging my code, the record came back in blocks of 1460 bytes followed by one smaller block and I had to append the blocks until I had received everything.
Here is the DataArrival routine from that app. Hopefully, it will give you some idea of what we are saying.
VB Code:
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
UDP will chop packets up, but the size of the packet is determined by the network (sort of). Therefore, don't rely on any particular packet size. Sizes can get down to below 512 Bytes, which really isn't much.
Furthermore, UDP doesn't guarantee that any particular packet makes it. Therefore, if you suspect that the data is going to exceed the packet size, you will need to have some mechanism to be sure that you can know whether you have all the data, and a means to re-submit for the missing data.
A header that tells you the total number of bytes helps, since you can determine whether or not you got the whole thing, but it doesn't tell you much about what was lost if something goes away. Solving that isn't all that easy, since you may not know how big the packet size will be. For that reason, UDP is generally used for REALLY small packet situations.
One possibility for larger packets would be to chop each message into very small pieces, number each piece, and send them. The header would include the number of pieces. There fore, the receiveing end can determine which piece is missing (by the number), the number of total packets, and the order of the packets.
Here's a nice DLL, source code, that replaces the winsock control.
This uses 2 classes...the cSocket class, that is a direct replacement for the winsock control, and my own class, Socket, which is a wrapper class for the cSocket class.
This wrapper class deals with large amounts of data, and removes the hassel of joining packets together.
There is a very basic small client server test app in the zip file also.