I have server, client processes written in C with the names NetworkServer.c and NetworkClient.c, and these 2 interact using Linux sockets. When a client sends a request as shown below to receive ethernet statistics,
// rxbuf - character array of 128K // ETHERNET_DIAGNOSTIC_INFO - structure typedefed recv(sockfd, rxbuf, sizeof(ETHERNET_DIAGNOSTIC_INFO), 0)
the server fills the data in rxbuf (like ETHERNET_DIAGNOSTIC_INFO, because the server also uses the same copy of the header file where this structure is defined) and sends the data. As soon as the client receives, he will look as follows to receive the data.
ETHERNET_DIAGNOSTIC_INFO *info = (ETHERNET_DIAGNOSTIC_INFO *) rxbuf;
The structure is defined in NetworkDiag.h, as shown below.
#ifdef __cplusplus extern "C" { #endif typedef struct ETHERNET_DIAGNOSTIC_INFO { uint32_t cmdId; unsigned long RxCount[MAX_SAMPLES]; unsigned long TxCount[MAX_SAMPLES]; time_t TimeStamp[MAX_SAMPLES] ; char LanIpAddress[20]; char LanMacAddress[20]; char WanIpAddress[20]; char LanDefaultGateway[20]; char LanSubnetMask[20]; char LanLease[5000]; }ETHERNET_DIAGNOSTIC_INFO;
This works fine.
Now there is a requirement that I need to create a C ++ file that should work as a client (I deleted the client C file and the server remains as a c file). I defined a header file to define the structure as shown below.
struct ETHERNET_DIAGNOSTIC_INFO { uint32_t cmdId; unsigned long RxCount[MAX_SAMPLES]; unsigned long TxCount[MAX_SAMPLES]; time_t TimeStamp[MAX_SAMPLES] ; char LanIpAddress[20]; char LanMacAddress[20]; char WanIpAddress[20]; char LanDefaultGateway[20]; char LanSubnetMask[20]; char LanLease[5000]; };
I basically removed the C ++ defender and typedef and using the code below in the client.cpp file to get the result from the server.
if(recv(sockfd, rxbuf, sizeof(ETHERNET_DIAGNOSTIC_INFO), 0) > 0) { ETHERNET_DIAGNOSTIC_INFO *info = reinterpret_cast<ETHERNET_DIAGNOSTIC_INFO *> (rxbuf); }
I do not get the right results. Values ββin the structure are inappropriate (some values ββare true, but many values ββare inappropriate). I also tried casting of type C, but did not use.
I doubt that we cannot create a type buffer in a C ++ structure on the client when the server sends data as a c-structure. Is it correct? Can someone please let me know how to solve this problem?