473,771 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Structure to C# structure using sockets

Raj
I want to pass a C structure from a windows server to the C# client using the
Sockets. Will there be requirements to cast the data types in the C# client?
Nov 16 '05 #1
3 4182
Hi Raj,

Normally you should use marshal incoming data to a C# struct (or marshal it
field per field if that suits your program). You can map your C-style struct
to a C# struct using decoration.

Check the Marshal class for more details on marshalling the data (or struct).

HTH,
Tom T.

You will

"Raj" wrote:
I want to pass a C structure from a windows server to the C# client using the
Sockets. Will there be requirements to cast the data types in the C# client?

Nov 16 '05 #2
Raj
Is it applicable for the data interchange over socket also? As my C++ server
uses sockets to communicate to the C# Client which is also using the sockets.
For example, lets say the C# client receives a buffer of data from its socket
port. Lets say it knows which structure it has received in the buffer. Now,
can the client directly assign the buffer to the structure variable and
expect the type conversions to occur automatically using the Marshall class?

"TT (Tom Tempelaere)" wrote:
Hi Raj,

Normally you should use marshal incoming data to a C# struct (or marshal it
field per field if that suits your program). You can map your C-style struct
to a C# struct using decoration.

Check the Marshal class for more details on marshalling the data (or struct).

HTH,
Tom T.

You will

"Raj" wrote:
I want to pass a C structure from a windows server to the C# client using the
Sockets. Will there be requirements to cast the data types in the C# client?

Nov 16 '05 #3
Hi Raj,

I think it is applicable (I have little experience with programming sockets
though).

Marshal will convert the types it can map to C# equivalents. Suppose you
send a C-structs like the following:
struct DS
{
unsigned int ID;
unsigned int Data[ 127 ];
};

In C# you would map it as follows:
public struct DS
{
public uint ID;

[ MarshalAs( UnmanagedType.B yValArray,
ArraySubType = UnmanagedType.U 4,
SizeConst = 127 ) ]
public uint[] Data;
}

IIRC, receiving from a socket returns a byte array. Marshal.PtrToSt ructure
needs an IntPtr though. Example:

byte[] data = new byte[ 512 ];

// <-- suppose data is filled from Socket.Receive here

GCHandle hData = GCHandle.Alloc( data, GCHandleType.Pi nned );
try
{
DS ds = (DS) Marshal.PtrToSt ructure( hData.AddrOfPin nedObject(),
typeof(DS) );
// ds is now a regular C# struct instance
}
finally
{
hData.Free();
}

I agree that it doesn't seem very elegant, but it is the only way I know how
to do that.

HTH,
Tom T

"Raj" wrote:
Is it applicable for the data interchange over socket also? As my C++ server
uses sockets to communicate to the C# Client which is also using the sockets.
For example, lets say the C# client receives a buffer of data from its socket
port. Lets say it knows which structure it has received in the buffer. Now,
can the client directly assign the buffer to the structure variable and
expect the type conversions to occur automatically using the Marshall class?

"TT (Tom Tempelaere)" wrote:
Hi Raj,

Normally you should use marshal incoming data to a C# struct (or marshal it
field per field if that suits your program). You can map your C-style struct
to a C# struct using decoration.

Check the Marshal class for more details on marshalling the data (or struct).

HTH,
Tom T.

You will

"Raj" wrote:
I want to pass a C structure from a windows server to the C# client using the
Sockets. Will there be requirements to cast the data types in the C# client?

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3581
by: Google Mike | last post by:
Using Sockets (fsockopen, fputs, fgets, etc.) I'm able to interact with an SMTP server to send mail. However, do you know the steps necessary in order to send a file attachment as well? (Note, if you need GNU tools to help achieve this task, note that I have RH9 Linux.)
2
2334
by: Vinod | last post by:
All, .NET Web Service needs to talk to third party software through TCP/IP sockets. Do we have any .Net Framework in place which can do a TCP/IP
0
1044
by: JC Voon | last post by:
Hi All: I have a problem when trying to set the value of the structure member using reflaction,here is the code: Public Const MemberAccess As BindingFlags = BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance Or BindingFlags.IgnoreCase
9
6646
by: Mike Curry | last post by:
I am looking for a simple sample of using sockets to download a page into a string. If anyone knows where I could get a sample, or post one here, that would be great.
7
1446
by: sareel | last post by:
I need the explanation/code to transmit a complex datastructure like Linked list or binary tree using sockets from client to sever...
3
1660
by: ahmed.maryam | last post by:
Hi all, I have a weird situation here in that I've designed two C# applications that need to communicate with each other using sockets. So I am trying to set the applications up in a way that one acts like the 'server' and the other a 'client'. However, the 'server' is an application that does some work, and one if its jobs is to launch the other application and start communicating with it when a user clicks a certain button. The other...
3
3561
by: prognoob | last post by:
I need help coding a file transfer in C#. It would be over TCP/IP using sockets... most of the solutions i have come across, they use networkstream and i have read online that there is no need to use networkstream with c# sockets... so how do I do it? also, do I need to worry about serialization/deserialization? any help would be greatly appreciated
2
16814
yashg
by: yashg | last post by:
I am building a data backup application in C# using Sockets. It has a server component and a client component. The client is going to upload files to the server through TCP sockets. I've got all figured out and have been able to transfer files between client and server. Now I have some doubts about which is the best method to transfer a file? Option 1 - I use the built in Socket.BeginSendFile (I am using async calls) to transfer a file. Option 2...
0
1044
by: avinashibs | last post by:
Hi, i want to receive mail from my mail server through vb.net using sockets, could any one please help me with code. it's very urgent requirement for me. Thanks,
0
1648
by: toto1980 | last post by:
Hi; I tried look for a simple way of sending a file using sockets, I found one in the old forums but I am not sure why the file when it is sent, the client side some how receive wrong contents, and when having a big file let us say 200 KB, the file transfer is not complete. Here is the code: Client side (receiver): /////////////////////////Receiving a file/////////////////////// Code: FILE *fp; char name="t.txt";
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.