473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2167
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
3
by: Logan McKinley | last post by:
I have a C# program that uses blocking sockets and want to allow the user to stop the server. The problem I am having is the socket blocks on...
1
by: Adam Clauss | last post by:
I am (attempting) to move an existing socket application to use raw sockets. Right now, my application is essentially a port forwarder. Upon receiving a connection, it will open a connection to...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
3
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
7
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.