0

I am writing a client server application using TCP sockets on C. For writing data to the server I am sending a structure like this:

typedef struct { uint64_t IEEE_ADDR; uint8_t endpoint; char requestedAction; uint8_t level; uint8_t wc; uint8_t address_mode; uint8_t gID; uint8_t sID; uint8_t val1; uint8_t val0; uint8_t status; }dsfeedbackPkt; 

For serialising and sending data I have written following function:

void fb_serialise_send(dsfeedbackPkt *pkt) { unsigned char buffer[SIZE]; int bytes; buffer[0] = ((pkt->IEEE_ADDR) >> 56); buffer[1] = (pkt->IEEE_ADDR) >> 48; buffer[2] = (pkt->IEEE_ADDR) >> 40; buffer[3] = (pkt->IEEE_ADDR) >> 32; buffer[4] = (pkt->IEEE_ADDR) >> 24; buffer[5] = (pkt->IEEE_ADDR) >> 16; buffer[6] = (pkt->IEEE_ADDR) >> 8; buffer[7] = (pkt->IEEE_ADDR); buffer[8] = (pkt->endpoint); buffer[9] = pkt -> requestedAction; buffer[10] = pkt->level; buffer[11] = pkt-> wc; buffer[12] = pkt->address_mode; buffer[13] = pkt-> gID; buffer[14] = pkt->sID; buffer[15] = pkt->val1; buffer[16] = pkt->val0; buffer[17] = pkt->status; bytes = write(cli_sockfd,buffer,sizeof(buffer)); } 

The problem is the data is being received at the server twice, as in two different packets. The second packet mostly contains garbage.

I have browsed through the internet, there are mostly issues related to endianess. But that is not a problem here, also both client and server know the message length beforehand, only the data is received at the server side twice.

Please can anyone suggest what I have missed?

I made following changes:

#define SIZE 18 

on client side and

#define RSIZE 18 

on server side

and its working the problem was using sizeof(dsfeedbackPkt) for defining the size.

7
  • 1
    Is the size of the first received packet less than SIZE? Commented Oct 16, 2015 at 8:22
  • Show us the actual code, where you send and receive data. Commented Oct 16, 2015 at 9:25
  • Bug in your unposted receiving code. TCP doesn't deliver data twice. Commented Oct 16, 2015 at 9:25
  • The size of first received packet is equal to SIZE Commented Oct 16, 2015 at 9:47
  • 2
    You need to help us help you, there is not enough information here to answer your question. Commented Oct 16, 2015 at 10:05

1 Answer 1

1

I am defining SIZE which is equal to the sizeof(dsfeedbackPkt)

Right here is your problem. Due to struct padding, the struct is probably 20 bytes, not 18.

Change SIZE to 18 and it should work.

Sign up to request clarification or add additional context in comments.

4 Comments

I changed that to #define SIZE 18 but, I am still receiving the same error
The remaining problem is not in the code you have posted so far. Could you pleases add the send/receive code to your question? Also, do you write to the socket anywhere else in the code?
Thanks! I was saving the size of received packet at the server side using sizeof, I made the same change at the server side too and now its working fine...
I looked up struct padding and now I get it, and I beleive that was the problem.. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.