I am writing a single threaded blocking tcp client on Linux that just sends requests to existing well tested server and waits for the response from it. Server and client are compiled with different compilers. My client code looks like below :-
char buf[sizeof(MyMsg) + sizeof(MySignon) + 10]={0};
MyMsg *msg = reinterpret_cast<MyMsg*>(buf);
MySignon *signOn = reinterpret_cast<MySignon*>(msg->mstrt); //mstrt is double type
msg->type= SIGNON;
msg->subtype= USER_SIGNON;
strncpy(signOn->szUserId, user, sizeof(signOn->szUserId)-1);
signOn->iApplicationMode = SM_SIGN_ALLOC;
SetMsgLen(msg, sizeof(MySignon));
auto ret = send(fd, reinterpret_cast<char*>(msg), msg->msglen);
and send looks like below :-
Status Send(int fd, char* data, size_t length)
{
size_t written = 0;
errno=0;
do{
MyMsg* pm = reinterpret_cast<MyMsg*>(data+written);
int r = ::write(fd, pm, length-written);
if (errno == 0)
written+=r;
} while (length > written and errno == 0);
}
The Problem :- When I run this, server receives a very large size from msg->msglen
. There is another client which does exactly same steps and which is compiled with same compiler as server that works fine. That is why I suspect different compilers might be treating structures in different way (may be due to struct padding).
What is wrong here. What is work around for this problem.