Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:30:31+00:00 2026-06-09T05:30:31+00:00

I am trying to implemante a client- server program communication using TCP socket programming

  • 0

I am trying to implemante a client- server program communication using TCP socket programming in C.
It is between between two 64bit machines with linux OS installed.
I want to transfer a c-struct between the two processes.

For this I try used a pack – unpack() functioanlity.

please consider the following code snipt

/*---------------------------------------------------------
on the sending side I have: 
---------------------------------------------------------*/

struct packet {
 int64_t x;
 int64_t y;
 int64_t q[maxSize];
} __attribute__((packed));

int main(void)
{
 // build packet
 struct packet pkt;
 pkt.x = htonl(324);
 pkt.y = htonl(654);
 int i;
 for(i = 0; i< maxSize; i++){
    pkt.q[i] = i; **// I also try pkt.q[i] = htonl(i);**
 }
  // and then do the send
}

/*-----------------------------------------------------------------------------
 in the receiving side: 
 -----------------------------------------------------------------------------*/
struct packet {
 int64_t x;
 int64_t y;
 int64_t q[maxSize];
} __attribute__((packed));

static void decodePacket (uint8_t *recv_data, size_t recv_len)
{
 // checking size
 if (recv_len < sizeof(struct packet)) {
     fprintf(stderr, "received too little!");
     return;
 }

struct packet *recv_packet = (struct packet *)recv_data;

int64_t x = ntohl(recv_packet->x);
int64_t y = ntohl(recv_packet->y);
int i;
printf("Decoded: x=%"PRIu8" y=%"PRIu32"\n", x, y);

 for(i=0;i<maxSize;i++){
  **//int64_t res = ntohl(recv_packet->q[i]);  I also try to print res**          
  printf("%"PRIu32"\n" , recv_packet->q[i]);     
 }
} 

int main(int argc, char *argv[]){
 // receive the data and try to call decodePacket()
 int8_t *recv_data = (int8_t *)&buf; //buf is the data received
 size_t recv_len = sizeof(buf);
 **decode_packet(recv_data, recv_len);**  
}

//-----------------------------------------------------------------------------

Now the problem is that I am receiving the value of x and y in the struct correctly,
but for the array q in the struct I am receiving a strange number, possible a memory grabage value, (I try to use memset() filling the array by zeros before receiving a data from the other side in which case the value of all zeros is received )

I don’t understand why I am not receiving the correct value for the array in struct.

Please Note that I try with and with out htonl() while filling the array before putting in struct,
and on the other side: with and with out ntohl() while decoding the array from struct

Any help will be appreciated,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:30:33+00:00Added an answer on June 9, 2026 at 5:30 am
    size_t recv_len = sizeof(buf);
    decode_packet(recv_data, recv_len);
    

    This piece of code ensures the wrong size is passed to decode_packet. So when decode_packet goes on to check recv_len < sizeof(struct packet), that test is meaningless – it will always pass, no matter how many bytes were received.

    You need to fetch the size from the value returned by the recv call. My best guess is that indeed you’re receiving fewer bytes than you’re expecting.


    While sending and receiving structs is quite convenient, it’s often an exercise in futility. Manually serializing data or using some explicit mechanisms is probably the way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a server-client socket program in Java that can support multiple
I am trying to implement a TCP echo server and client program in Ubuntu
I'm trying to implement Sun's example Socket program, i.e. the KnockKnock server and client
Hello im trying to implement a websocket communication between client and server. the problem
I am trying to implement a bidirectional client-server program, where clients and servers can
im trying to implement simple secured client server communiction using WCF. when im launching
I am trying to implement a simple chat program in linux using bsd sockets.
I am trying to implement a Client/Server model using TCpClient , with its Networkstream.Write()/Read()
I'm trying to implement a client server app which a Server program has a
I'm building a non-browser client-server (XULRunner-CherryPy) application using HTTP for communication. The area I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.