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

  • Home
  • SEARCH
  • 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 6170977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:08:01+00:00 2026-05-23T23:08:01+00:00

I’ve been trying to send a packet from a client to a server via

  • 0

I’ve been trying to send a packet from a client to a server via sockets. With the help of some of the tips I have made quite bit of progress in my code. However, the server only receives eight bytes from the client and prints them on the console whereas at my client side, It seems that it has sent everything.

Now I am not sure whether the problem is at the sending side or the receiving side. My hunch is that something is wrong at my client side. Could someone please help in verifying my assumption?

Client code:

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    data_struct client_data;
    struct packet
    { 
        long int srcID;
        long int destID;
        int pver;
        int profiles;
        int length;
        long int data;
    };


    if (argc < 3) {
        fprintf(stderr,"usage: %s hostname port\n", argv[0]);
        exit(0);
    }
    portno = atoi(argv[2]); //Convert ASCII to integer
    sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor

    if (sockfd < 0) 
        error("ERROR DETECTED !!! Problem in opening socket\n");

    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR DETECTED !!!, no such server found \n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr)); //clear the memory for server address

    serv_addr.sin_family = AF_INET;    
    bcopy((char *)server->h_addr, 
        (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);

    serv_addr.sin_port = htons(portno);

    printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno); 


    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR in connection");

    printf("SUCCESS !!! Connection established \n");

    char buffer[256];
    struct packet *pkt = (struct packet *) buffer;
    char *payload = buffer + sizeof(struct packet);
    long double packet_size;



    printf("Started Creating packet\n");
    pkt->srcID = 01;
    pkt->destID = 02;
    pkt->pver = 03;
    pkt->profiles = 01;
    pkt->length = 16;
    pkt->data = 1; 2; 3; 4; 5; 6; 7; 8;
    {
        if (send(sockfd,pkt,sizeof(packet_size),0) <0)
            printf ("error\n");
        else
            printf ("packet send done");
    }

    return 0;
}

Server code:

int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno, clilen;
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    char wish;

    long int SrcID;
    long int DestID;
    int Pver;
    int Profiles;
    long int Data;
    int Length;
    char bytes_to_receive;
    int received_bytes;
    struct packet
    { 
        long int srcID;
        long int destID;
        int pver;
        int profiles;
        int length;
        long int data;
    };

    if (argc < 2) {
        fprintf(stderr,"usage: %s port_number1",argv[0]);
        exit(1);
    }
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR DETECTED !!! Problem in opening socket");

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR DETECTED !!! There was a problem in binding");

    listen(sockfd, 10);
    clilen = sizeof(cli_addr);
    printf("Server listening on port number %d...\n", serv_addr.sin_port); 
    newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

    if (newsockfd < 0) 
        error("ERROR DETECTED !!! the connection request was not accepted");

    char buffer[256];
    struct packet *pkt = (struct packet *) buffer;
    char *payload = buffer + sizeof(struct packet);
    long double packet_size;

    bytes_to_receive = sizeof(packet_size);
    received_bytes = 0;
    int rc =0;


    while ((rc = recv(newsockfd,pkt,sizeof(packet_size),0)) > 0)
    {
        received_bytes+=rc;
        SrcID = pkt->srcID;
        DestID = pkt->destID;
        Pver = pkt->pver ;
        Profiles = pkt->profiles;
        Length = pkt->length;
        Data = pkt->data;
        printf("Data Received from Client_1 are :\n");
        printf("Source ID: %ld\n", SrcID);
        printf("Destination ID: %ld\n", DestID);
        printf("profile Version: %d\n", Pver);
        printf("No of Profiles: %d\n", Profiles);
        printf("Length: %d\n", Length);
        printf("data : %ld\n", Data);
    }
    if (rc == 0)
    {
        printf("Connection closed by Server\n");
        printf("Bytes received: %d\n",received_bytes);
    }

    if (rc == -1)
    {
        perror("recv");
    }
    {
        if (close(newsockfd) == -1) {
            error("Error closing connection with client 1");
        }

        printf("Connection with client 1 has been closed\n");
    }
    return 0; 

}

The output that I see on the client’s console is:

Client Side:  Client 1 trying to connect with server host 130.191.166.230 on port 1234
SUCCESS !!! Connection established 
Started Creating packet
packet send done

and on the server’s console I see:

Server Side:  Data Received from Client_1 are :
Source ID: 1
Destination ID: 2
profile Version: 0
No of Profiles: 1074462536
Length: 0
data : 0
Connection closed by Server
Bytes received: 8
Connection with client 1 has been closed
  • 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-23T23:08:02+00:00Added an answer on May 23, 2026 at 11:08 pm

    First of all

    recv(newsockfd,pkt,sizeof(packet_size),0)) /* What is packet_size ? */
    recv(newsockfd,pkt,sizeof(struct packet),0)) /* You probably mean this. */
    

    That might solve your problems, but there are a few issues with the way you are using TCP sockets.

    But at my client side, it prints that it has sent everything

    Where ? I don’t see you actually checking the number of bytes sent. send(2) can return after sending less that you asked it to.

    It shows me that only 8 bytes were sent by Client and prints them out.

    TCP is a stream-oriented protocol. You send bytes and they arrive, in the same order. So when you recv(2) something, you might get less (or more than you wrote). So, the following can be true:

    client:
    send 100 bytes
    send 400 bytes
    
    server:
    recv 50 bytes
    recv 150 bytes
    recv 250 bytes
    recv 50 bytes
    

    The number of send and recv calls need not be identical when using TCP.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.