471,338 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

.net sockets

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.

Jul 21 '05 #1
2 2122
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).
unluturm wrote:
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.

Jul 21 '05 #2
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..."
=================

"unluturm" <un******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
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.

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tero Saarni | last post: by
1 post views Thread by Dmitry Akselrod | last post: by
reply views Thread by mrpolitics | last post: by
3 posts views Thread by Logan McKinley | last post: by
1 post views Thread by Adam Clauss | last post: by
3 posts views Thread by Michael Maercker | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.