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 6166873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:24:54+00:00 2026-05-23T22:24:54+00:00

I had written a small client-server code where in I was sending integers and

  • 0

I had written a small client-server code where in I was sending integers and characters from my client to server. So I know the basics of socket programming in C like the steps to follow and all. Now I want to create a packet and send it to my server. I thought that I will create a structure

    struct packet  
    { 
    int srcID;
    long int data;
    .....
    .....
    .....
    };
    struct packet * pkt;

Before doing send(), I thought that I will write values inside the packet using

   pkt-> srcID = 01
   pkt-> data = 1 2 3 4 

I need to know whether I am on the right path, and if yes then can I send using

      send(sockfd, &packet, sizeof(packet), 0)

for receiving

    recv(newsockfd, &PACKET, sizeof(PACKET), 0)

I have just started with network programming, so I am not sure whether i am on the right path or not. It would be of great help if anyone can guide me with my question in any form (theoretical,examples etc). Thanks in advance.

  • 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-05-23T22:24:55+00:00Added an answer on May 23, 2026 at 10:24 pm

    The pointer pkt is NOT defined in your application. You have two options:
    1) Declare pkt as a normal variable

     struct packet pkt;
    
     pkt.srcID = 01;
     ....
     send(sockfd, &pkt, sizeof(struct packet), 0);
    

    2) The second approach is useful when your packet contains a header followed by a payload:

     char buffer[MAX_PACKET_SIZE];
     struct packet *pkt = (struct packet *) buffer;
     char *payload = buffer + sizeof(struct packet);
     int packet_size;  /* should be computed as header size + payload size */
    
     pkt->srcID = 01;
     ...
     packet_size = sizeof(struct packet) /* + payload size */ ;
    
     send(sockfd, pkt, packet_size, 0);
     .... 
    

    UPDATED (to answer your comment):
    First, you should know that receiving from a TCP socket MAY NOT provide the whole packet. You need to implement loop (as suggested by Nemo) to read the whole packet. Since you prefer the second option, then you need two loops. The first loop is to read the packet header to extract the payload size and the second loop to read the data. In case of UDP, you don’t need to worry about partial receiving. Here is a sample code (without looping) where sockfd is a UDP socket:

    char buffer[MAX_PACKET_SIZE];
    struct packet *pkt = (struct packet *) buffer;
    char *payload = buffer + sizeof(struct packet);
    int packet_size;  /* should be computed as header size + payload size */
    
    .....
    /* read the whole packet */
    if (recv(sockfd, pkt, MAX_PACKET_SIZE, 0) < 0) {
        /* error in receiving the packet. It is up to you how to handle it */
    }
    /* Now, you can extract srcID as  pkt->srcID */
    /* you can get data by processing payload variable */
    

    Remember:
    * you need to implement serialization as mentioned by other users
    * UDP is unreliable transport protocol while TCP is a reliable transport protocol.

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

Sidebar

Related Questions

I have written a small client server socket application. It is a proof of
I am a trying to learn C# .Net. I had written small (hobby) Analog
In Ruby I have often written a number of small classes, and had a
I was glancing through some code I had written in my Perl class and
A new client of mine has a small VB/Access database application written in 2002
I tried to use acast from reshape2 within a self written function, but had
I had written a code snippet in VC++. However, I cannot continue rest of
I recently inherited some code that someone else had written. I discovered that everywhere
I have had a small game (Written in C#) which I've built up over
I had written a query like this string strUpdateQuery = UPDATE M_QA SET ANSWER

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.