.net sockets

G

Guest

Does anyone know how to send structure type data over tcp/ip using .net
sockets? I have this vb.net app (client) and a server (C type app). I can
send strings but it does not want to parse a string. It wants to receive the
data in structure format, like

structure student {
char[5] firstname;
char[5] lastname;
and so on
}.

any ideas appreciated. thanks.
 
J

Joshua Flanagan

You need to send the data as an ordered stream of bytes. Try looking at
the BinaryWriter class.

Pay special attention to the data type conversions. My memory is rusty,
but I think in VB.NET, a Char = 2-bytes, and in C, a char = 1 byte.

So if they are expecting a 5 char first name, you will want to send 5
Bytes (not 5 Chars).
 
M

Mahesh Devjibhai Dhola [MVP]

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..."
=================
 

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

Top