Sockets data errors



I'm unsure of the best group to post this in, so feel free to direct me to
the most appropriate group...

I'm trying to use Sockets to send data from one computer to another. I
have running a fairly simple sockets interface using a blocking socket (I
experimented early on with sync and async and found little performance gains
and reasons to use unblocking code). The sending computer sends small
messages to the receiving computer via a message scheme I developed. This
has been working just fine.

The data I'm sending fits into one of a handful of different structs. As a
result, what I have done, is take a struct of data, serialize it to a byte
array, send it over the socket, and then deserialize it. The first byte of
the struct, is a value, corresponding to a specific struct. I use that value
to determine which type to use to deserialize the struct back to a struct
object. The code to serialize/deserialize is as follows:

public static object DeserializeStruct(byte[] rawdatas, Type anytype)
object retobj = null;
int rawsize = Marshal.SizeOf (anytype);
if (rawsize > rawdatas.Length)
return null;

IntPtr buffer = Marshal.AllocHGlobal (rawsize);
Marshal.Copy (rawdatas, 0, buffer, rawsize);
retobj = Marshal.PtrToStructure (buffer, anytype);
Marshal.FreeHGlobal (buffer);
catch (Exception ex)
SysLog.GetInstance().WriteLog(ex.ToString(), EventLogEntryType.Warning);
return retobj;

public static byte [] SerializeStruct(object anything)
byte [] streamData = null;
int structSize = Marshal.SizeOf (anything);
IntPtr buffer = Marshal.AllocHGlobal (structSize);
Marshal.StructureToPtr (anything, buffer, false);
streamData = new byte[structSize];
Marshal.Copy (buffer, streamData, 0, structSize);
Marshal.FreeHGlobal (buffer);
catch (Exception ex)
SysLog.GetInstance().WriteLog(ex.ToString(), EventLogEntryType.Warning);
return streamData;

This has been working excellent, but I've tasked with now sending large
chuks of variable data (non struct data) over this same port. What I did to
accomplish this, was add a new struct, that will contain only a small amount
of the data (a packet if you will). I split the large chunk of data into
small packets, and send these via the same message sending mechanism, then
recombine when complete. Intially this works fine and is quite fast.

However, after some period of time, some parts of the data seem to get lost
in transit. I've not been able to determine exactly what is being lost, as
any attempt to add traces, logging, etc, seem to affect when and if this
happens (heisenbrg if you will). In fact, adding a 100ms sleep in between
each message actually solves the problem (not a solution I'm willing to
accept). From everything I can gather, I seem to be overloading the socket.
The faster I send the data, the more often this happens. (Note: I've added a
mechanism, to allow for retries, when the data is corrupt). With the 100 ms
delay, it almost never happens. I've also tried, various differnt options in
the socket with no success. In fact, every attempt to alter the buffers, has
caused the app to stop connecting at all.

I'm now at the point, where I'm going to change paradigms: Instead of
sending smaller packets and recombining, I'm planning on sending one very
large packet, dynamic in size, and picking the data out, instead of

If you have A) any ideas on what I'm doiing wrong or B) ideas that I can use
to attempt to determine where I went wrong or C) ideas on how you would send
very large chunks of data over sockets (preferrably with code examples) I
would love to hear what you have to say.

Sadly no, I'm not interested in how I can make lots of money at home in my
spare time :)

Thanks in advance!




Thanks, at least you have confirmed my suspicions. I'm not willing to go as
far as say Sockets is bugged (I've been wrong too many times in the past).
However, I do think there is some sort of option that needs to be set/altered
or some action that needs to be taken.

I've taken the time to alter my code to send entire byte arrays (dynamic)
instead of packets and the same problem cropped up, once the size got over a
certain limit. Smaller arrays seem to work just fine.

Thanks again for your help...

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question