473,539 Members | 4,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 23216
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
2420
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 ); string fileName = fileNameRaw.Substring( 0 , fileNameRaw.IndexOf("***") ); Console.WriteLine( fileName );
0
1307
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 there's any advice, it'd be greatly appreciated :) // created on 11/07/2004 at 19:26 using System;
2
3464
by: David Dvali | last post by:
Hello. I wnat to read some data from server, I'm using following code: ---------------------------------------------------------------------------------------- TcpClient cl = new TcpClient(); cl.Connect("SomeHost", 9000); NetworkStream networkStream = cl.GetStream(); if (!networkStream.CanRead) return;
7
3129
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 same time. My scenario is a small server application that listens on a particular TCP port. I am sending streams of data to it via a client app....
3
1703
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 variation of a FAQ. I'm writing a small online game (client/server). The server spits out a steam of data. The data can be described as a Code,...
5
9547
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 the receiving thread would get 'stuck'. The pattern that I have used from the reading is quite common... AutoResetEvent waitForReadToComplete;...
0
1359
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 not deleted (outlook). What I am trying to get are messages that haven't been read yet? Is there a way to do this? Here is the code I am...
3
6748
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 on the stream. What am I doing wrong? There is more code than this, but this is the problem code.
8
4345
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 elicit a <greetingresponse from the server, and it's the reading of this response that is giving me a bit of grief. The response shoud be
0
7310
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...
0
7704
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...
1
7295
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...
0
7652
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...
0
5833
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...
0
4859
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...
0
3359
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1764
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
934
muto222
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.