Hi,
May be too much to ask, but I'll give it a go. I've worked in C for almost 30
years, C++ for almost 15, but am new to C#.
We have a large application in C. It stands up a tcp/ip server for
administration. Currently, there is an x-windows GUI client (in C) that
connects into this server for configuring and monitoring the application. We
want to replace the old x-windows client with something that'll run on a PC.
We've been told that C# is a good fit.
All communication between the server application and the client GUI is via
message exchange, with the high-level message format being:
<length> <header> [optional <data>]
2-byte fixed-length 0-or-more fixed-length, or string
- The <length> is a 16-bit unsigned integer with total message size (not
including the <length> itself).
- The <header> is a C-style struct with a mix of signed/unsigned ints of various
sizes (8, 16, 32, and 64-bit), some C-style fixed-length, null-terminated
strings of 8-bit characters, and some bit-fields (unsigned int where each
bit is used to denote an individual flag)
- The <data> field (if it exists) will be either one or more C-style struct's or
a string of characters, all based on the message-type within the <header>.
There are over 100 different structs used in the <data> field (various
configuration items, statistic elements, ...).
I'm having a bit of difficulty mimicking the C-struct/C-strings in C#. I've
seen online a bunch of stuff on marshalling, unsafe code, unmanaged code,
LPStr, UTF8, ..., but haven't seen a concise example of:
a) tcp/ip reading in a C-struct (with C-strings) and making it useable in C#ec
b) doing the reverse (re-building the C-struct to send)
Here is a fictional, 40-octet C struct to use as the header in examples (u16
is an unsigned 16-bit int, s16 is a signed 16-bit int, ...):
typedef struct TestHeader
{
u16 messageType;
s16 num1;
char text1[4];
char text2[8];
u64 num2;
s32 num3;
u8 num4;
u8 num5;
u16 flag1;
char text3[4];
char text4[4];
} TestHeader;
What I have tried:
marshalling, unsafe code, unmanaged code,
LPStr, UTF8, ...,