Hi
When we are passing data with the help of Sockets we have to pass the data in byte format. But when we want to use the data we normally use the class structure. So how to work with classes and sockets. Lets say we want to pass a class structure through sockets.
For our example we will take a simple class MessageClass
Our class has three fields and three properties for those fields namely messageType, message, counter
class MessageClass
{
private string messageType;
private string message;
private int counter;
public string MessageType
{
get { return messageType; }
set { messageType = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}
public int Counter
{
get { return counter; }
set { counter = value; }
}
}
The class is good for the code. But in the network, we can't send and receive an object or a type. Everything should be converted to byte array. So, we should convert this object to a byte array part by part and send or receive it over the network. The following code shows the send the class object. Here msg is an object of the MessageClass
byte [] buffer = new byte [4];
buffer = BitConverter.GetBytes((int)msg.MessageType);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
// counter
byte []counterBuffer =
Encoding.Unicode.GetBytes(msg.counter.ToString());
buffer = new byte [4];
buffer = BitConverter.GetBytes(counterBuffer.Length);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
this.networkStream.Write(counterBuffer, 0,
counterBuffer.Length);
this.networkStream.Flush();
//Message
byte [] ipBuffer =
Encoding.ASCII.GetBytes(msg.Message.ToString());
buffer = new byte [4];
buffer = BitConverter.GetBytes(ipBuffer.Length);
this.networkStream.Write(buffer , 0 , 4);
this.networkStream.Flush();
this.networkStream.Write(ipBuffer , 0 , ipBuffer.Length);
this.networkStream.Flush();
As you can see we are sending each property of the class object one by one.
The send and receive have to be bi-directional. Meaning that when we send some data the other end of the socket should be ready to receive the data in the same format. Example when we send 4 bytes the other end should read those 4 bytes. This operation is repeated till we have sent all the data.
Here is the code for receiving data.
//Read the Message Type.
byte [] buffer = new byte [4];
int readBytes = this.networkStream.Read(buffer , 0 , 4);
if ( readBytes == 0 )
break;
string msgType =
(string)( BitConverter.ToInt32(buffer , 0) );
//Read the Message's counter size.
string msgcounter = "";
buffer = new byte [4];
readBytes = this.networkStream.Read(buffer , 0 , 4);
if ( readBytes == 0 )
break;
int ipSize = BitConverter.ToInt32(buffer , 0);
//Read the Message's counter.
buffer = new byte [ipSize];
readBytes = this.networkStream.Read(buffer , 0 , ipSize);
if ( readBytes == 0 )
break;
msgcounter = System.Text.Encoding.ASCII.GetString(buffer);
//Read the Message size.
string msgMessage= "";
buffer = new byte [4];
readBytes = this.networkStream.Read(buffer , 0 , 4);
if ( readBytes == 0 )
break;
int MessageSize = BitConverter.ToInt32(buffer , 0);
//Read the Message
buffer = new byte [MessageSize];
readBytes = this.networkStream.Read(buffer , 0 , MessageSize);
if ( readBytes == 0 )
break;
msgMessage = System.Text.Encoding.Unicode.GetString(buffer);
Now you can create an instance of the messageClass here and put the values to the property.
Hence you saw how we could pass a class object through socket
Hope this helps
Thanks
Vikram