473,406 Members | 2,816 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,406 software developers and data experts.

garbled byte offset when receiving data from Socket

i have a C# program that is sending data in a byte array through a
socket.

the VC++ application server receives data in teh following format....

typedef struct advice
{
header sHdr;
char Code[33];
char pn[11];
char State[4];
char szType[4];
char szTravelDate[9];
char szAmount[13];
char szMethod[7];
char CATID[16];
long lStan;
...
}

the weird part is that EVERY char array after lStan is missing 3
characters.

if i do a QUICKWATCH in VC++, i get the value of lStan as "1128415488".
converting this to a byte array , yields the bytes {0,65,66,67} - which
is the first 3 values of the field after lStan.(the field after lStan
has 'ABCDEF')

i CANT use any structures, marshalled or otherwise at the C# end ,
because my structure definitions come out of a XML Schema document.

conversion routine for the value in C#
int nData = int.Parse(strTempData);
//convert to Byte Array; this.bPvtData =
BitConverter.GetBytes(nData);
this converts 3652 into byte array = {68,14, 0 , 0}

something is evidently happening to this when it comes out of the
Socket ... either that or VC++ is not interpreting the size of the
'long' variable (which im passing as a Int32)

please help!

Mar 7 '06 #1
14 2478
A long is a Int64, as per documentation:
Type Range Size .NET Framework type
long –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit
integer System.Int64
Hope that helps :)
On Tue, 07 Mar 2006 15:29:33 +0800, <Ro*********@gmail.com> wrote:
i have a C# program that is sending data in a byte array through a
socket.

the VC++ application server receives data in teh following format....

typedef struct advice
{
header sHdr;
char Code[33];
char pn[11];
char State[4];
char szType[4];
char szTravelDate[9];
char szAmount[13];
char szMethod[7];
char CATID[16];
long lStan;
...
}

the weird part is that EVERY char array after lStan is missing 3
characters.

if i do a QUICKWATCH in VC++, i get the value of lStan as "1128415488".
converting this to a byte array , yields the bytes {0,65,66,67} - which
is the first 3 values of the field after lStan.(the field after lStan
has 'ABCDEF')

i CANT use any structures, marshalled or otherwise at the C# end ,
because my structure definitions come out of a XML Schema document.

conversion routine for the value in C#
int nData = int.Parse(strTempData);
//convert to Byte Array; this.bPvtData =
BitConverter.GetBytes(nData);
this converts 3652 into byte array = {68,14, 0 , 0}

something is evidently happening to this when it comes out of the
Socket ... either that or VC++ is not interpreting the size of the
'long' variable (which im passing as a Int32)

please help!


Mar 7 '06 #2
thanks a ton Mark... but actually i did consider that fact and am using
a Int32 at C# end,

because my VC++ long (int) is a 32-bit Integer.

Mar 7 '06 #3
Ro*********@gmail.com wrote:
i have a C# program that is sending data in a byte array through a
socket.

the VC++ application server receives data in teh following format....

typedef struct advice
{
header sHdr;
char Code[33];
char pn[11];
char State[4];
char szType[4];
char szTravelDate[9];
char szAmount[13];
char szMethod[7];
char CATID[16];
long lStan;
...
}

the weird part is that EVERY char array after lStan is missing 3
characters.

if i do a QUICKWATCH in VC++, i get the value of lStan as "1128415488".
converting this to a byte array , yields the bytes {0,65,66,67} - which
is the first 3 values of the field after lStan.(the field after lStan
has 'ABCDEF')


It sounds like it's CATID that's causing the problem then. How are you
transmitting that?

Jon

Mar 7 '06 #4
hello again Jon,

well i observed that the difference between the VC++ only
implementation and the C# - VC++ implementation is that
the padding bytes are inserted BEFORE the last 'long' value of the
structure.

is that what you mean to say ?

thanks again

Mar 7 '06 #5
http://msdn.microsoft.com/library/de..._Alignment.asp

found the above link and i think it is related with my problem ..

however does there occur a byte padding BEFORE every long / numeric
value in a struct ?

TIA

Ro*********@gmail.com wrote:
hello again Jon,

well i observed that the difference between the VC++ only
implementation and the C# - VC++ implementation is that
the padding bytes are inserted BEFORE the last 'long' value of the
structure.

is that what you mean to say ?

thanks again


Mar 7 '06 #6
<Ro*********@gmail.com> wrote:
hello again Jon,

well i observed that the difference between the VC++ only
implementation and the C# - VC++ implementation is that
the padding bytes are inserted BEFORE the last 'long' value of the
structure.

is that what you mean to say ?


I'm not entirely sure what you mean. If you're feeding data to the
socket yourself, there shouldn't be any padding involved at the .NET
end. If the VC++ end is doing padding, that *could* be the problem.
This is one of the downsides of just reading data into unmanaged
structs - it's not terribly clear what's expected.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '06 #7
good evening Jon,

well the only reason im padding the data at the .NET end is to handle
the EXTRACTION of the data at the VC++ end.

are there any padding guidelines that you are aware of ?

i figure i'll have to check every field type and then pad the previous
fields accordingly.
thanks again

Mar 8 '06 #8
<Ro*********@gmail.com> wrote:
good evening Jon,

well the only reason im padding the data at the .NET end is to handle
the EXTRACTION of the data at the VC++ end.
Yes - but my point is that if the VC++ end explicitly read the data a
field at a time, the data format would be a lot clearer.
are there any padding guidelines that you are aware of ?
I'm afraid not - there will be some around, I'm sure, but I don't know
them.
i figure i'll have to check every field type and then pad the previous
fields accordingly.


Yup.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 8 '06 #9
hello again Jon,

well the VC++ code DOES read the data coming out of the socket into the
structure (from my first message).

it just doesnt read it in the properly aligned order (ie ... it reads
the long value offset by 3 bytes)

snippet --- >

// Cast to request packet
pPass = (ADVICE_STRUCT)pCliThread->pData; //the pCliThread->pData is
the data coming out of the socket at the VC++ end

now if i look at the byte arrays at the C# end ... the long value is
the SAME as the one in the VC++ array (68,14,0,0)..

its just starting from buffer position 120 instead of buffer position
123 (where its expected in the VC++ struct).

anyway, thanks a lot.. im guessing ill have to go through every
element, find its type and then pad the PREVIOUS string accordingly.

Mar 8 '06 #10
Ro*********@gmail.com wrote:
well the VC++ code DOES read the data coming out of the socket into the
structure (from my first message).
Exactly - and that's the problem. Because it doesn't explicitly read it
field by field, it just reads it as one lump, the precise nature of
that lump isn't very clear.

<snip>
anyway, thanks a lot.. im guessing ill have to go through every
element, find its type and then pad the PREVIOUS string accordingly.


You actually probably need to keep track of the offset, work out what
kind of alignment the "current" type uses, and pad accordingly. I
wouldn't think of it as padding the previous field though - it's
padding *between* the fields.

Jon

Mar 8 '06 #11
hey Jon,

way im doing it now is ... discovering the type of each field...
checking if the sum of the bytes till now + size of current field is
divisible by 4 ... if not ... pad bytes.

if the type is System.String... im not padding it at all ...

thanks a ton for your help!

Mar 12 '06 #12
ConfNoob <Ro*********@gmail.com> wrote:
way im doing it now is ... discovering the type of each field...
checking if the sum of the bytes till now + size of current field is
divisible by 4 ... if not ... pad bytes.

if the type is System.String... im not padding it at all ...


That might do it - to be honest, I'm far from an expert (read: I know
nothing!) on alignment in unmanaged C/C++. I suspect you're best off
either looking at the documentation or asking in a C/C++ group for what
the exact rules are.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 12 '06 #13

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
ConfNoob <Ro*********@gmail.com> wrote:
way im doing it now is ... discovering the type of each field...
checking if the sum of the bytes till now + size of current field is
divisible by 4 ... if not ... pad bytes.

if the type is System.String... im not padding it at all ...


That might do it - to be honest, I'm far from an expert (read: I know
nothing!) on alignment in unmanaged C/C++. I suspect you're best off
either looking at the documentation or asking in a C/C++ group for what
the exact rules are.


Padding out to multiples of 4 should do the trick (align binary data on a Word boundary)
However, I have never coded on a 64 bit platform, so I am not sure if double word alignment is
required for anything.

Good luck
Bill
Mar 12 '06 #14
thanks Bill,

for now this will be a 32-bit platform application.

i'll let you folks know how it comes along.

thanks again.

Mar 13 '06 #15

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

Similar topics

0
by: Jack | last post by:
I am trying to develop a simple telnet client in MS.NET I have a small wrapper around the System.Net.Sockets.Socket class which takes care of sending and receiving data and handing it off to a...
7
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. ...
1
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....
5
by: Olaf Baeyens | last post by:
I have another problem, maybe it is simple to fix. I have this: byte Test=new byte; But I now want to have a second pointer Test2 to point to a location inside this Test. But with no...
7
by: Wilfried Mestdagh | last post by:
Hi, how to typecast or convert a byte to a string and the way arount ? I have string from a textbox and wants to transmit it trough a socket. witch accepts a byte. Now I make a copy from the...
0
by: vinothgsd | last post by:
Hi, I have created Client/Server application. In that Client side Program i have used Asynchronous Callback method for send and receive the stream. In the Receive method I want to make the...
2
by: freeurmind | last post by:
hello, i'm trying to recieve some binary data on a socket and then decode it, to decode my data i'm using a dll library, my problem is that the function i need in my library to decode my message...
3
by: Ashit Vora | last post by:
Hi, I had one small query. I have a data in char pointer (actually 4 char pointers) I wanna make one char pointer out of it (without terminating it with NULL. so cant use STRCPY). I used...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
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,...

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.