cant send a whole struct with sockets (c code)
Hi all!! Thats a new problem that i have discovered..
I have an array of structs and i want to pass them to the other pc struct { field field field }mystructs[10]; if (sendto(sock,mystructs,sizeof(mystructs[10]), 0,(struct sockaddr 1*) &server, server_len) <0){ the other pc receives that if ((n=recvfrom(sock,&mystructs,sizeod(mystructs[0]),0, mpla mpla mpla..... I have spent a whole night changing and trying the following parameters mystructs instead of &mystructs sizeof(mystructs) sizeof(mystructs[10]) sizeof(mystructs[0]) all these methods have the same results.. to pass only the first struct of the array which is the mystructs[0]... all the others mystruct[1] mystructs[2] are null.. Plz guys help me its an emergency |
try sizeof(mystructs)*10
|
I dont think this works... any new suggestion plz?
|
Quote:
Have you initialized mystructs[1] to [9]? Try to declare a type (no direct definition): struct mystruct_s { field field field }; struct mystruct_s mystructs[10]; Then use the following: &mystructs[0] sizeof(mystructs) /* Or alternatively: sizeof( mystruct_s[10] ) */ |
Please re-read post #5 in this thread:
http://www.linuxquestions.org/questi...hreadid=363900 Forget about the "real" program you're trying to develop, forget about "sockets" and "passing data to the other PC" for a moment. The issues you need to deal with are: "what is a struct?" "what is an array?" "how are they represented in memory?" "how do I access (or pass to some other function) just that one piece of a complex structure I really mean to access?" ... and .. "am I actually accessing (or passing) the piece I *think* I'm accessing?" Basic stuff ... but crucial. Your .. PSM |
Quote:
|
I've never used recvfrom() and sendto(), only send() and recv() but this code (not complete could use more robust error detection and displaying) works for me:
Test struct declaration: Code:
#ifndef GLOBALS_H Code:
static void Code:
void |
have you considered endianness of machine ?
sender x86 but receiver sparc type. |
Either that, or the possibility of time-travel.
|
*****Drat Should have paid attention to NevemTeve's time travel comment
Quote:
Although you haven't posted any code there is a lot of confusion being expressed. I've always found that in C, and really any programming language, it is best to both test concepts, and read, how things are supposed to work. I've posted a small sample program if you compile and run it you'll we see possible issues expressed with how you are approaching this. I've posted my output, as well as the output from compiling it. Things to note. First that sizeof for a struct isn't necessarily the same as the size of its aggregate parts. In fact, in some instances, the compiler is padding the struct with extra bytes in the interest of alignment; and it can be different on a different machine or with a different compiler. If you look at the output from my compiler you will see a lot of warnings. Essentially they are saying that sizeof, in the function call, may not be doing what the programmer thinks it is doing. The reason I bring these things up is that for a program to act as expected there are things that have to be understood. Sending a struct over a socket may work under some conditions but won't under others. It's a better idea to serialize a struct before sending it over a tream - i.e. send the individual fields - and deserialize at the other end. You might benefit from spending some time considering what an array of structs is and how you refer to a single element (and its size) vs the array (and its size) - where it's being passed to a function and where it's not. Personally I write a lot of test programs to help me understand how some piece of code will perform. This lets me get one part right without the confusion of a complicated program. This is also a good practice when asking for help. Post the smallest program you can devise that demonstrates your problem or confusion. Code:
#include <stdlib.h> Code:
sizeof struct data = 12 Code:
$ gcc -Wall array_of_structs.c -o array_of_structs |
All times are GMT -5. The time now is 02:20 PM. |