473,503 Members | 939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best way to convert byte array (from TCP socket) to C# Object?

Hi All,
Basically my situation is this, I have a server implemented in C++,
unmanaged code, using proprietery protocol over TCP/IP to communicate
with the cilent(c++ also).
Now, I am implementing the another client in C#. Server side can not
be
changed :(
So, I am using tcp/ip sockets. In the end I get byte[] on the client
side, which originally was some C++ class object, converted to byte
array.

What is the most efficient way to convert that byte array to C#
object?
I've checked few sources, writing custom serializer doesn't seem to be
a good idea, as the serialize will receive a stream object, which only
has byte read.

So far the best I've come up with is to create BinaryReader based on
the NetworkStream which in turn is created from the socket. More or
less works, but string reading is a hassle, because the server won't
send it in the format needed by BinaryReader, and I have to create 2
additional objects.

Is there anything that can operate on byte[] to read simple types from
it based on the offset and length? I.e. interpret bytes as a type?
Nov 15 '05 #1
6 15296
Hi Gator,

Without putting much thought in (I'm finishing for the day) have
you considered doing it in "unsafe" C# using pointers to your heart's
content? Being C++ers you'll know what you're doing.

Out of curiosity - am I right in thinking that you want to import
a C++ object into C# and have it become a managed object ?

Regards,
Fergus
Nov 15 '05 #2
Hi Fergus,
you considered doing it in "unsafe" C# using pointers to your heart's
content? Being C++ers you'll know what you're doing. Nopers, I haven't considered actually. If I could I'd rather do it
safe ;)

Out of curiosity - am I right in thinking that you want to import
a C++ object into C# and have it become a managed object ?

Hmm, I guess oyu could say so. In short both ends of the socket are
dealing with message class objects. On send, class is converted to
byte buffer, on receive it has to be restored from the bute buffer.
Now if it ws C# on both end it's be all dandy, serialize and be done
with it.
Because of C++/C# combo I have to go through those contortions.
Nov 15 '05 #3
"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
From what I understand, you definately DO NOT want to try
to get it back into the same C++ object on the client side
(using some unmanaged DLL), right? You want to create some
type of managed .NET object (C# is just a language, remember :) Yes.
that closely resembles the C++ object, right? Yes.
I don't see why you wouldn't want to use that (or use a byte[]
like you said, since BinaryReader does it the way you're asking,
just from a stream of bytes rather than a fixed byte[]). Well, with ints and numbers it's fine. however to read a string from
that stram is a problem, I have to copy bytes from that stram into
byte array and then convert it to the string, because the string
wasn't serialized by C++ side as C# expects it.
I assume it's a null-terminated string. You'll have to create
MemoryStream and copy the bytes (byte-by-byte) from the stream

Actually binaryreader has ReadBytes function which can read multiple
bytes in one shot, directly into byte array and then convert it to
string.

Thanks, looks like there's no better way.
Nov 15 '05 #4
Hi Gator,

|| > you considered doing it in "unsafe" C# using pointers to your
heart's
|| > content? Being C++ers you'll know what you're doing.
||
|| Nopers, I haven't considered actually. If I could I'd rather do
it
|| safe ;)

I hear you on that, buddy, though I think "unsafe" is Microsoft's
word for "keep away kiddies". You don't strike me as a kiddy, lol.
Perhaps you could use it just for the strings.

Good luck, whichever way,
Fergus
Nov 15 '05 #5

"Gator" <zv*@zvis.com> wrote in message
news:2c**************************@posting.google.c om...
"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
But how do you know how many bytes to read if it's
null terminated?

Pardon, forgot to mention that the string has length prefix.


Hrm, according to the docs, BinaryReader.ReadString()...
"Reads a string from the current stream. The string is
prefixed with the length, encoded as an integer seven bits
at a time."

I'm not sure what they mean by the 7-bits part...I thought
ASCII was encoded with 8 bits, but the 8th bit was never
used.

Do they expect the bits to be packed or to be 1-bit
padded to fit on the byte boundary?

-c
Nov 15 '05 #6
Chad Myers <cm****@N0.SP.AM.austin.rr.com> wrote:
Hrm, according to the docs, BinaryReader.ReadString()...
"Reads a string from the current stream. The string is
prefixed with the length, encoded as an integer seven bits
at a time."

I'm not sure what they mean by the 7-bits part...I thought
ASCII was encoded with 8 bits, but the 8th bit was never
used.

Do they expect the bits to be packed or to be 1-bit
padded to fit on the byte boundary?


Nope - it's the *length* which is encoded as an integer 7 bits at a
time, not the string. I believe it's basically a system where you take
the bits of the length, divide them into blocks of 7 (instead of the
typical 8) and use the top bit of each byte to say whether there's
another byte to come. So, for lengths < 128, you end up with it being a
single byte which is the length in itself. 128-16384 would be encoded
in two bytes (but without knowing the endianness I don't know how it
would work out exactly) etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7

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

Similar topics

5
2606
by: PHS | last post by:
I have a c# client application that connects to a network socket. When connected, the listener sends a network stream containing bitmap, jpeg, data from a file nominated by the client. The client...
1
7708
by: Jim Shank | last post by:
I am adding support to my application for Oracle 10g and using Enterprise Library Data Access Application Blocks and trying to determine the best way to convert the GUID's which are stored as...
2
4313
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...
7
52418
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...
1
1601
by: Jim | last post by:
Here is a snipet of the code. // State object for receiving data from remote device. public class StateObject { // Client socket. public Socket workSocket = null; // Size of receive buffer....
6
10134
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...
5
9546
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...
19
5308
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
2
15888
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
7203
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
7282
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
7339
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...
1
6995
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...
0
7463
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...
1
5017
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
4678
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...
0
3168
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
1515
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 ...

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.