Yes, there will definitely be problems with portability.
Coordination of structural elements can be different even among different compilers on the same platform, not to mention different platforms. And thatβs all, assuming that sizeof(int) same for all of them (although provided, usually this is --- but do you really want to rely on "usually" and hope for the best?).
This is done even if MAX_WORD on both computers (I assume they are from here, and if not, then you have problems here).
What you need to do is send (and receive) each field separately. There is also a problem with sizeof(int) and endianness, so I added a htonl() call to convert from the system to the network byte order (the reverse function is ntohl() ). They both return uint32_t , which has a fixed, known size.
send(sockFd, myData.max_word, sizeof(myData.max_word)); // or just MAX_WORD send(sockFd, myData.min_word, sizeof(myData.min_word)); uint32_t count = htonl(myData.word_count); // convert to network byte order send(sockFd, &count, sizeof(count)); // error handling! if((ret = recv(sockFd, myData.max_word, sizeof(myData.max_word))) != sizeof(myData.max_word)) { // handle error or read more data } ... // and so on // remember to convert back from network byte order on recv! // also keep in mind the third field is now `uint32_t`, and not `int` in the stream
source share