1

Correct me if I'm wrong.

struct st {
    int a, b, c;
}

struct st test = {1, 2, 3};
send(socket, (char *) &test, sizeof(test), 0);

Now on the other side how should I receive it? Will something like this work:

struct st received;
read = recv(socket, (char *) & received, sizeof(st), 0);

The problem here is what if it doesn't read the whole structure at once, only some of the bytes(the amount is stored in read). Is the code below correct if I now want to append more bytes to this struct to read it completely?

recv(socket, (char *) &received + read, sizeof(st) - read, 0);

etc ? Of course this will go on until all bytes have been read.

Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
Greyshack
  • 1,901
  • 7
  • 29
  • 48
  • `sizeof(struct)`? Is that legal? – luk32 Jun 29 '15 at 14:03
  • I'm no expert but you have to create a protocol so your client can talk to your server etc. It would be kind of impossible to send user-defined data-structures over the net and expect the other side to understand the result. You can have a leading character as a command and send the rest of the data as arguments and put it inside a struct later on the other side. The sizeof(structObj) is the size of all its members so sizeof(structObj) should give you 12 bytes. –  Jun 29 '15 at 14:04
  • edited, sizeof(struct) was a typo. @nilo I know all that. Let's assume that I'm expecting a struct of such size to be send right now, I'm asking if that would be the correct way to get it. – Greyshack Jun 29 '15 at 14:09
  • If you know what you are expecting on the other side, you read the first 12 bytes and put it inside the struct. –  Jun 29 '15 at 14:10
  • How do I put it inside the struct? – Greyshack Jun 29 '15 at 14:11
  • How much bytes do you read? It's quite unlikely that 12 bytes would get split. Also, in general such way is not guaranteed to work, basically every time when the target and and source system are not the same I would be very afraid to use it. – luk32 Jun 29 '15 at 14:14
  • For piece-wise reading, take a `char*` pointer to the structure first and bump that by the bytes read each time (will keep it simpler, less error prone). **NOTE** That this will only work if the sender and receiver have the same byte order _and_ exactly the same structure-packing rules: if either of these [may] differ, you'll need to send member-by-member. – TripeHound Jun 29 '15 at 14:15
  • You keep polling, read chunks of data into a buffer, convert buffer to your data. Take a look at this: http://www.binarytides.com/receive-full-data-with-recv-socket-function-in-c/ –  Jun 29 '15 at 14:18
  • @nilo I understand all of that apart from how to convert the buffer to data when for example buffer size is 512 and struct size is 12. How do I take 12 bytes out of the buffer and put it into the struct? – Greyshack Jun 29 '15 at 14:23
  • @Greyshack `memcpy(&thestruct, thebuffer, sizeof(thestruct));`. – molbdnilo Jun 29 '15 at 14:26
  • @molbdnilo thanks!! exactly what I needed. Forgot about this function completely. One more thing. So you're saying that sending whole structs is not recommended ? Even if I have the same code for the struct on both sides? – Greyshack Jun 29 '15 at 14:28
  • Same code doesn't suffice. You'll need identical platforms. Sending data across a network usually involves *serializing* the data into a byte stream, and deserializing it at the receiving end. A common serialization is XML. – IInspectable Jun 29 '15 at 14:34
  • So json is a good choice as well ? – Greyshack Jun 29 '15 at 14:41
  • possible duplicate of [Serialize a struct and send it via socket with C++](http://stackoverflow.com/questions/4851995/serialize-a-struct-and-send-it-via-socket-with-c) – Sergey Vyacheslavovich Brunov Jun 29 '15 at 14:45
  • possible duplicate of [Passing Object over Server to client](http://stackoverflow.com/q/15391582/490018) – Sergey Vyacheslavovich Brunov Jun 29 '15 at 14:47
  • take a look to http://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c Basically sending a struct over the network requires serialization and de-serializations. Also need to take care of endianness as the network data goes over Big-Endian format but the receiving end may be of Little Endian type. – Abhinav Jun 29 '15 at 15:02

0 Answers0