Passing structure over TCP socket in C -
i'm writing small client server application in c.
at client side have 1 structure like,
#pragma pack(1) // helps avoid serialization while sending on network. typedef struct _viewboxclient_info { unsigned long viewboxid; int brt_flag; int nframenum; char framedata[1000]; }viewboxclient_info_send ; #pragma pack(0) // turn packing off
and filling variable as,
struct _viewboxclient_info client_info; client_info.brt_flag= 10;/*0 false, 1 true*/ client_info.viewboxid=10000; memcpy(client_info.framedata,buf,sizeof(client_info.framedata)); //char buf[] data "is 1st line" client_info.nframenum=1;
and sending server using following function,
send(sock, (char *)&client_info, bytesread, 0);
at server side,i have 1 structure (same client side structure),
#pragma pack(1) // helps avoid serialization while sending on network. typedef struct _lclviewboxclient_info { unsigned long viewboxid; int brt_flag; int nframenum; char framedata[1000]; }viewboxclient_info_receive ; #pragma pack(0) // turn packing off
and receiving message , printing on screen as,
viewboxclient_info_receive lcl_viewbox; ssize_t bytesreceived = recv( *nfd, &lcl_viewbox, sizeof(struct _lclviewboxclient_info), 0); printf("\nlcl_viewbox.brt_flag:%d\n",lcl_viewbox.brt_flag); printf("lcl_viewbox.nframenum:%d\n",lcl_viewbox.nframenum); printf("lcl_viewbox.framedata:%s\n",lcl_viewbox.framedata);
o/p @ server screen,
lcl_viewbox.brt_flag:1 lcl_viewbox.nframenum:1936287860 lcl_viewbox.framedata: 1st line
i not know happening @ server side. i'm sending 10 brt_flag client , receiving 1 @ server. sending nframenum=1 client , receiving nframenum:1936287860 @ server. framedata @ client side i'm sending "this 1st line" , @ server receiving "is 1st line". in rid of problem?
thanks , regards,
sri
shouldn't this:
send(sock, (char *)&client_info, bytesread, 0);
be:
send(sock, (char *)&client_info, sizeof( client_info ), 0);
and should check return value of send() , particularly of recv() - there no guarantee call recv fetch structure in 1 go.
Comments
Post a Comment