473,472 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Struct Socket C#

I want to send the struct to another Program by using TcpClient. I found a
method that converts struct to byte but it doesnot working correctly. What
am i missing?
[StructLayout(LayoutKind.Sequential)]
public struct student
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=33)]
public char[] sName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
public char[] sLastName;
}

private byte[] StructureToByteArray(object obj)
{
int len = Marshal.SizeOf(obj);
byte[] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
Marshal.FreeHGlobal(ptr);
return arr;
}

Nov 15 '08 #1
3 4446
Tolga Ongunsu wrote:
I want to send the struct to another Program by using TcpClient. I found
a method that converts struct to byte but it doesnot working correctly.
What am i missing?
You can start by giving a more specific description of how it is not
working correctly.

Arne
Nov 15 '08 #2
This result of mine
DataLen:66sName:Tolga Ongunsu
‡cv€’hvÔı¯sLastName:Ongunsu ‡cv€’hvÔı¯

Corrent one has to be
DataLen:66sName:TolgasLastName:Ongunsu

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:49***********************@news.sunsite.dk...
Tolga Ongunsu wrote:
>I want to send the struct to another Program by using TcpClient. I found
a method that converts struct to byte but it doesnot working correctly.
What am i missing?

You can start by giving a more specific description of how it is not
working correctly.

Arne
Nov 15 '08 #3
Tolga Ongunsu wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:49***********************@news.sunsite.dk...
>Tolga Ongunsu wrote:
>>I want to send the struct to another Program by using TcpClient. I
found a method that converts struct to byte but it doesnot working
correctly. What am i missing?

You can start by giving a more specific description of how it is not
working correctly.
This result of mine
DataLen:66sName:Tolga Ongunsu
‡cv€’hvÔı¯sLastName:Ongunsu ‡cv€’hvÔı¯

Corrent one has to be
DataLen:66sName:TolgasLastName:Ongunsu
The result you get looks correct to me because:

[MarshalAs(UnmanagedType.ByValArray,SizeConst=33)]
public char[] sName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
public char[] sLastName;

tell that the fields are 33 long, so some kind of padding should
happen.

Arne
Nov 15 '08 #4

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

Similar topics

3
by: Gordon Scott | last post by:
Hi All, I've got a problem I'm seeing when trying to use the struct module to send data to a different machine. Actually I'm making a condensed file that gets transferred to and read on a BREW...
2
by: Mike Spindler | last post by:
I am passing structs via UDP socket to my Python app from an external C program. The structure is made up almost entirely of bit fields. struct example: unsigned int var1 : 3; unsigned int...
4
by: James Harris | last post by:
Having updated my Debian system it now complains that I am using an incompatible pointer type in warnings such as "passing arg 2 of 'bind' from incompatible pointer type" from code, struct...
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
1
by: bofh1234 | last post by:
I am trying to write a small program that sends some data over the network to a server. The data I want to send is read from the command line i.e. dms 2 somedata otherdata value hostname port. I...
3
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack...
0
by: ambeer | last post by:
Hello, i have created the struct: #region PacketStructures struct MainPacket { HeaderData headerData; public byte moduleType; ...
4
by: Christopher Pisz | last post by:
This is a solution I thought about to some of the problems I had talked about in the "zero memory" above in the ng. While this isn't a MS specific post, let me walk you though the problem that...
3
by: Ashutosh | last post by:
Hello, I am a newbie in CSharp. In C++ if we want to read from socket into the struct object we directly type cast it as recv(socketid,(char*),&struct_object,sizeof(struct object)) But C...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return...
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
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.