-2

What if I had a an array of struct. Is it possible for me to send an array of structs over a socket? The struct size will be updated continuously and at any time I would be able to print out the content of the structs. I hope this makes sense, my explanation might not be cleared. My syntax is definitely not correct for some areas, it's just a snippet of what I think it would look like. I just need some guidance.

This will send the array of structs acorss the socket.

void sendOpenMessage(int num0, int num1, int num2){

  struct openMessage{
     int num0;
     int num1;
     int num2

  };
  struct openMessage open[100];
  int i = 0;
  open[i].num0 = 1;
  open[i].num1 = 2;
  open[i].num2 = 3;  

  int length = sizeof(open);
  if(send(socket, &open[i], length, 0) == -1){
       fprintf(stderr, "Send() failed");
  }else{
      printf("Open message is being sent\n");
  }
  i++;
} 

This will receive the struct and display the contents in a message

  struct openMessage open[100];
  if(recv(clnSocket, &open, sizeof(open), 0) < 0){
      fprintf(stderr,"Recv() failed\n");
      printf("Error code: %d\n", errno);
  }

  //Get size of the current struct 
  //Print out the messages from the structs that have messages?
  void printStruct(struct openMessage open){
     for(int i = 0; i < sizeof(the struct); i++){
           printf("%d\n",open[i].num0);
           printf("%d\n", open[i].num1);
           printf("%d\n", open[i].num2);
     }
  }
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
pennyBoy
  • 397
  • 2
  • 17
  • 1
    Please *try* it first. If you get some specific errors or problems, then you're more than welcome to ask about that. Also please review about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And don't forget how to create a [mcve]. – Some programmer dude Nov 22 '18 at 06:54
  • @Someprogrammerdude I did. Implemented it that way. I got a bunch of errors for example "subscripted value is neither array nor pointer nor vector struct - Google Search". Currently what I have right now I pass just a typedef socket over the socket. – pennyBoy Nov 22 '18 at 07:04
  • @Someprogrammerdude but thanks for the tips for future use. I appreciate it. – pennyBoy Nov 22 '18 at 07:05
  • 2
    If you ask about build errors, then besides the [mcve] also show us the *complete* and *full* output of the compiler, copy-pasted as text into the question. And in the code mark out the places where the errors are with comments. If you get run-time errors or crashes or unexpected results, then please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). And tell us the input, the expected *and* the actual output. Or point out where in your code the crash is. We can't really help you much otherwise. – Some programmer dude Nov 22 '18 at 07:09
  • I **guess**, the actual error is in the code which is not exposed. If `struct openMessage open[100];` is a local variable in the function where `recv()` is called its storage is lost after leaving that function. A returned pointer to that array becomes dangling. Using it later (e.g. to call `printStruct()` with that pointer) would be undefined behavior. Agree: A [mcve] is needed to answer this sufficiently. – Scheff's Cat Nov 22 '18 at 07:36
  • `void printStruct(struct openMessage open)` gets exactly one `struct openMessage` argument by value. There is nothing to subscript in `open` - it's no array nor pointer. If you want to pass a single `openMessage`, please, drop the `for` loop and indexing. If you want to pass the whole `open` array, then the argument should be `*open`. Arrays decay to pointers when passed to functions. Hence, you would need in the latter case the number of array elements as well. This makes even more sence as you might have less than 100 "packages" received - `n` could denote the number of received elements. – Scheff's Cat Nov 22 '18 at 07:49
  • @Someprogrammerdude I just stumbled over nested functions (as OPs 2nd code looks like). I was convinced they are not allowed in C and was surprised that `gcc` did compile and run them. I didn't find any setting in ideone (where I tried first) and switched to godbolt. Even with `-std=c11 -pedantic` I got a successfully compiled binary: [live demo on godbolt](https://gcc.godbolt.org/z/lwgtDG). Phew! At least, I found an SO Q/A [Why nested functions in C are against C standards](https://stackoverflow.com/a/43584863/7478597) that told me that I wasn't that wrong or didn't miss something new... – Scheff's Cat Nov 22 '18 at 07:56

1 Answers1

0

First of all, you need to read up about byte ordering and serialisation.

Secondly, length is the size of the whole array, so when i is zero you send the whole array, when i is 1 you send all but the first element plus some garbage at the end, and so on. Thirdly, when receiving you iterate across sizeof(the struct) members of the array. Don't you want to iterate across the whole array?

When you say the struct size will be updated continuously, do you mean the number of elements in the array? Your struct has a fixed size.

If you are sending variable quantities of data, i.e. if you want to send a piece of data that has one length at one time and another length at another time, or even if you want the number of elements in the array to update dynamically, then you MUST send that size across the socket, and send it before the data. Send the size first, read it first on the receiving end, and then the receiving end will know how much more data to read, and more importantly, where the data ends so that if there is more data later it doesn't all get mixed up.

AlastairG
  • 4,119
  • 5
  • 26
  • 41