05-22-2018 06:08 AM
I have a structure having char and int vaiable and i want to send through tcp comm . How to do this??
05-23-2018 02:10 AM - edited 05-23-2018 02:13 AM
For such a simple structure, the simplest way to solve your problem is to format a string with individual struct field values (sprintf) and send that through TCP, scanning out values in the target app (sscanf).
But if you have a more complex struct or if you want to abstract from actual struct definition you could do something as follows:
myStruct str; // Create a string with struct content Fmt (msg, "%*i[zb1w2p0r16]", sizeof (myStruct), (char *)&str);
this will create a string of alphanumeric characters that can be sent through TCP.
The target application will rebuild the struct content with:
int i, ch; myStruct str; // Rebuild original structure if (strlen (buffer)) { for (i = 0; i < strlen (buffer) / 2 - 2; i++) { Scan (buffer, "%s[i*w2]>%i[r16]", i * 2, &ch); sprintf ((char *)&str + i, "%c", ch); } }
Just as an example, this struct:
typedef struct { char name[5]; char channel[10]; int max; } test;
with name = "ABC", channel = "Channel1" and max = 123 gives the following string:
"41424300eb4368616e6e656c310014007b000000"
05-23-2018 01:07 PM
Why alphanumeric? Your fixed-length flat structure can go through tcp as binary.
05-24-2018 12:02 AM
It's been a while since I used tcp for interprocess communication I remember I had problems sending raw bytes sequences that included null bytes so I resolved to go that way. ClientTcpWrite accepts strings and that one is a plain string.
05-24-2018 12:13 AM
Woah! Seems it's been very long time since I used those functions, and my memory failed! Here is a message I posted a while ago that says exactly the opposite: you can actually send by nary data with null bytes embedded.
05-29-2018 10:37 AM
I still think that, for small pieces of data, sending it via text is not bad advice.
You can also send binary data, of course, but you should exercise some caution with issues such as the byte of order of integers, or structure padding, especially if there are different computer platforms involved.
05-30-2018 04:56 AM
If you're going with text, get a JSON library and send standardized JSON, rather than an ad hoc custom format.
06-01-2018 02:33 AM
As Luis has pointed out already, endianess is a concern if you use binary formats. Other than that TCP is fully 8 bit transparent so there is nothing in TCP, IP, UDP that would make trouble with binary data.
What you obviously have to watch out is to not use any C string formatting and processing routines on that data. That is where the issue with NULL bytes in a data stream comes from.
Another issue you have to consider is compatibility. Changing a binary data stream once you distributed your app, is a non-trivial exercise for sure. It doesn't scale well in that respect and can create a maintenance nightmare. Depending on the used JSON or similar library this can be much much easier there, as the better ones do not require the data of named tuples to be in the same order as in the binary structure and can be instructed to drop unsupported message elements.