473,609 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting to a byte array for a socket.send

Hi,

I'm trying (or struggling) to convert some C code to C#.

The existing C client is sending a structure via a TCP socket to a network
server. The structure contains a mix of int and char data types [with NO
data alignment i.e. pack(1)].

I can re-create the structure in c# with LayoutKind.Expl icit, but how do I
convert this structure to a byte array for use with the Socket.Send()
method?
Nov 17 '05 #1
2 10329
The solution I know of consists of creating a byte array of size of your
structure and using BitConverter class to obtain values of integers and
chars from the struct in the order they are defined. Maybe there's another
possible solution I don't know of, but this one works.

Example:
struct Foo
{
int number;
char c;

public byte[] ToBytes()
{
byte[] numberBytes = BitConverter.Ge tBytes(number);
byte[] charBytes = BitConverter.Ge tBytes(c);
byte[] result = new byte[numberBytes.Len gth + charBytes.Lengt h];
//use Array.Copy to copy contents of both arrays to the result, I
don't know the exact signature off my head
return result;
}
}

"Shreddy" <sh*****@nospam .protean-systems.com> wrote in message
news:TP******** ************@pi pex.net...
Hi,

I'm trying (or struggling) to convert some C code to C#.

The existing C client is sending a structure via a TCP socket to a network
server. The structure contains a mix of int and char data types [with NO
data alignment i.e. pack(1)].

I can re-create the structure in c# with LayoutKind.Expl icit, but how do I convert this structure to a byte array for use with the Socket.Send()
method?

Nov 17 '05 #2
Hi,

A simple solution would be adding a method like :

public byte[] GetBytes()
{
byte[] buff = new byte[ XXX ] ; //you know the size apriori.
Array.Copy( BitConverter.Ge tBytes( aInt), buff, 0, 0 , 4 ); // not sure
if this is the correct set of parameters !!!
Array.Copy( BitConverter.Ge tBytes( aInt), buff, 0, 4 , 4 ); //similar

return buff;
}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Shreddy" <sh*****@nospam .protean-systems.com> wrote in message
news:TP******** ************@pi pex.net...
Hi,

I'm trying (or struggling) to convert some C code to C#.

The existing C client is sending a structure via a TCP socket to a network
server. The structure contains a mix of int and char data types [with NO
data alignment i.e. pack(1)].

I can re-create the structure in c# with LayoutKind.Expl icit, but how do
I convert this structure to a byte array for use with the Socket.Send()
method?

Nov 17 '05 #3

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

Similar topics

5
13914
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do something like: char chars = "Hello!"; byte bytes = (byte) chars;
2
4330
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit this sound (file) on a Windows socket (not using FTP but TCP), can I read the file into a byte array? Because if it is possible then I can send it on the socket but the problems I foresee are: How will I repack it into the file. Will just reading...
7
52455
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte array.. Thanks & Regards Prakash Prabhu
7
6741
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ Where S is the command for SEND and should just be the character S. Where W is a byte representing how long the filename (testfile.txt) is. In this case 12. Where XXXXXXX is converted from a string that...
4
3117
by: Winston Nimchan | last post by:
Hi: I'm currently developing a socket application and would like to precede the data being sent with a 4 byte message length header (bin4). Can anyone help Regards Winston
6
10174
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array) "Usuario Sin Atribuciones Para Esta Función"
4
1707
by: TomHL | last post by:
Hello, I want to send an additional info with byte array wiith sockets as "one packet". I know how to send the byte array by himself, but how can I send the additional info with it at the same time (as one packet)? thx for yout answers.
3
1547
by: Fireangel | last post by:
I want to cast a class into a byte array. I've seen some examples of this floating around, but they all have simple data members. What happens if I cast something that has a ArrayList or an Array?? Does it loop through the array and cast them all and I end up with a big array (This is what I hope will happen)? Or does it simple cast the pointer address and I end up with a small array?? Related question: How do I make sure everything...
2
6264
by: Sisnaz | last post by:
I'm reading byte information from a C++ program via TCP sockets. At one time I thought I came across away to convert C Float data type into a VB single data type. I'm reading the C data type in bytes from the socket stream but can't seem to convert it. Any help would be greatly appreciated. Here is my data: The actual values in the C++ application: Node 1 LL40 - Position
0
8093
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
8555
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...
0
8408
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6067
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4033
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
4100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
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 we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1404
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.