Send Different Structures using TcpClient and TcpListener

onepieceking

Well-known member
Joined
Sep 20, 2006
Messages
64
Programming Experience
1-3
Hi,

I know how to send structures using the help of serializing the structure and sending it through the network stream.

The client will send a serialized structure and the listener will listen the stream for that particular type of structure. It works this way.

However, i have a problem now. The client might be sending different type of serialized structures at different times. How do I re-code the listener such that the listener can deserialize them correctly?

Thanks alot in advance.
 
This is a very simple issue sending different type data to a continuous stream, either the receiver must know the order and type of data sent and decode it in accordance to this schema or each transmission must be prepended with a descriptor telling the receiver what comes next. (variable length fields should usually always be described by length of byte stream)
 
One solution for sending many different data is to make them a common 'packet' (and serialize this), example:
VB.NET:
Public Class CommonPacket
Public data() As Byte
Public datadescription As String
End Class
 
Supposed I use the common packet method,

VB.NET:
<serializable>
public class CommonPacket
 
   public structure test1
      dim var1 as string
      dim var2 as string
   end structure
 
   ... ... ...
 
   public structure test20
      dim var1 as string
      dim var2 as string
   end structure
 
end class

Only one structure will be sent at a time (different structures at different times). If i send this common packet with only one structure "filled", will the common packet be very large in size?
 
That could be done, in that case as much larger than necessary as the combined sizes of the initial values of the redundant structures member types.

What I meant was for example you make any kind of data into byte array (which you have to anyway) and just make up some kind of description that will tell the receiver what kind of data the bytes array is. If you see this you also see that this method is much more versatile in all situations than a preset number of structures.
 
Hi,

Is it possible to put the descriptor together with the byte array?

As in the descriptor could be an integer then convert to byte array. Then this byte array, together with the actual data byte array, is sent.

If it is possible, how do I (at the receiver) read and convert the first part of the byte array to Integer?
 
Of course, you define your own communication schema or protocol or what you want to name it.

To read it you know what to expect, for example integer is 4 bytes. To convert these 4 bytes to integer you can use shared BitConverter.ToInt32() method for example. Wrapping a BinaryReader around the stream is also convenient sometimes, for example does it by default write a length descriptor used to write/read strings to stream. I have an attachment projects that use this here: http://www.vbdotnetforums.com/showthread.php?t=16379 (post2)
 
Ok, thanks alot John.

I have sortof used your idea and use the marshal and intptr to be able to just use one byte array to send across the commumication.

The receiver will just need to read off the first integer and i will know what structure to assign.
 
Back
Top