Hi,
Here is C# code for it,
//STUCTURE
public struct MyStruct
{
public char[] firstname;
public char[] lastname;
}
//CLASS to send structure - SERIALIZATION
public class SendStucture
{
MyStruct myObj = new MyStruct();
public SendStucture()
{
char[] arr = {'a','b','c'};
myObj.firstname = arr;
myObj.lastname= arr;
}
public bool sendStruct()
{
try
{
IFormatter formatter = new BinaryFormatter();
Stream streamOfData = new MemoryStream();
formatter.Serialize(streamOfData, myObj);
streamOfData.Position=0;
int numBytesToRead = (int) streamOfData.Length;
byte[] dataArray = new byte[numBytesToRead];
int numBytesRead = 0;
while (numBytesToRead>0)
{
int n = streamOfData.Read(dataArray,0,dataArray.Length);
if (n<=0)
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
streamOfData.Close();
//create your own end point
IPHostEntry IPhst = Dns.Resolve("localhost");
IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 2345);
Socket sock= new Socket(endPt.AddressFamily,
SocketType.Stream,ProtocolType.Tcp);
sock.Connect(endPt);
sock.Send(dataArray,dataArray.Length,0);
return true;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
}
I have no idea that how server in C lang will deserialize it but the
following code is for deserializing the data in C#
This is partial class, you may complete it as per your need. or i will post
this complete sample on some site when i will get some more time. I will
send you the link at that time.
//RECEIVING SIDE CODE - DESERIALIZAITON
while ( true)
{
bytesRead=sock.Receive(buffer, bufferLength, 0);
if (bytesRead==0)
break;
message.Append(Encoding.Default.GetChars(buffer, 0, bytesRead));
}
IFormatter formatter = new BinaryFormatter();
Stream streamOfData = new MemoryStream();
byte[] dataArray = Convert.FromBase64String(message.ToString());
streamOfData.Write(dataArray,0,dataArray.Length);
streamOfData.Position=0;
MyStruct myObj = (MyStruct) formatter.Deserialize(streamOfData);
streamOfData.Close();
Console.WriteLine("First Name: {0}", myObj.firstname.ToString());
Console.WriteLine("Last Name: {0}", myObj.lastname.ToString());
So you can get the stucture object (myObj in above sample code) and it
should print the firstname and lastname.
HTH,
Mahesh Devjibhai Dhola
"Empower yourself..."
=================