473,394 Members | 1,769 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.

Reading an Int32 in network byte order from NetworkStream?

I'm writing a C# server application that is reading from a socket sent
from a C++ client program. The C++ client program is sending the
following data:

struct
{
int stringLen;
actual ASCII string;
};

stringLen is intialized with the following C++ code:

htonl(std::string::size());

My question is, how do I read in the string length in C#? I can't
seem to locate similiar functionality in C# for the C++ call
ntohl(long);

Thanks for any help!

Nov 15 '05 #1
3 23203
Does BitConverter not help?

What about Marshal?

Or maybe you can define the structure in C#, use StructLayoutAttribute etc.
to order the fields and then deserialize it from the byte array you get from
the .NET Socket object. Note: I've never tried this; it's just an idea...
I'm not particular hopeful that it will work.

- Lee

"Scott" <pu*********@cox.net> wrote in message
news:c1********************************@4ax.com...
I'm writing a C# server application that is reading from a socket sent
from a C++ client program. The C++ client program is sending the
following data:

struct
{
int stringLen;
actual ASCII string;
};

stringLen is intialized with the following C++ code:

htonl(std::string::size());

My question is, how do I read in the string length in C#? I can't
seem to locate similiar functionality in C# for the C++ call
ntohl(long);

Thanks for any help!

Nov 15 '05 #2
"Scott" <pu*********@cox.net> wrote in message
htonl(std::string::size());
My question is, how do I read in the string length in C#? I can't
seem to locate similiar functionality in C# for the C++ call
ntohl(long);


To convert the four bytes you've read from the stream to an integer, use
BitConverter.ToInt32. However, this method expects the bytes in little
endian order, and network byte order is big endian. So before you pass the
array to the BitConverter class you'll first have to reverse them if
necessary. Here's the code:

byte[] array = ...; // 4 bytes from stream
if (BitConverter.IsLittleEndian)
Array.Reverse(array);
int length = BitConverter.ToInt32(array, 0);

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 15 '05 #3
Use the IPAddress Class static methods:

IPAddress.NetworkToHostOrder() or IPAddress.HostToNetworkOrder() depending
upon the direction you're going...

Dan

"Scott" <pu*********@cox.net> wrote in message
news:c1********************************@4ax.com...
I'm writing a C# server application that is reading from a socket sent
from a C++ client program. The C++ client program is sending the
following data:

struct
{
int stringLen;
actual ASCII string;
};

stringLen is intialized with the following C++ code:

htonl(std::string::size());

My question is, how do I read in the string length in C#? I can't
seem to locate similiar functionality in C# for the C++ call
ntohl(long);

Thanks for any help!

Nov 15 '05 #4

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

Similar topics

5
by: localhost | last post by:
Network socket client code works correctly: byte nameBytes = new byte; recvData = inStream.Read( nameBytes , 0 , 1024 ); string fileNameRaw = System.Text.Encoding.UTF8.GetString( nameBytes );...
0
by: Charles Pratt | last post by:
I'm having trouble with a network chat based server I'm working on in C#, It works find during the reads, but as soon as I start sending back data my reads from that point on are corrupted. If...
2
by: David Dvali | last post by:
Hello. I wnat to read some data from server, I'm using following code: ---------------------------------------------------------------------------------------- TcpClient cl = new TcpClient();...
7
by: simonrigby_uk | last post by:
Hi all, Sorry if this is the incorrect group but I couldn't see anything directly relevant. Can someone confirm for me what happens when two network streams are sent to an application at the...
3
by: Adman | last post by:
Hi all. I've done some websearching, and haven't been able to find my question answered, so I thought I'd post. I apologize if this has already been answered. My question seems to be a...
5
by: Dave A | last post by:
I have an application that does lots of socket communications all asynchronously via the TcpClient class. The code has been working 99.9999% of the time (yeah one of those bugs) but occasionally...
0
by: tshad | last post by:
I can't seem to retrieve messages that are not in my mailbox from Exchange. If I am reading mail from my Exchange server, I will get messages that are in my inbox that have already been read but...
3
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk...
8
by: Peter Bradley | last post by:
Hi, I wonder if anyone can help me out? I'm trying to implement an EPP (rfc4934 and rfc4930) client. So far I've managed to connect and authorise using an X509 Certificate. This should...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.