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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:25:39+00:00 2026-06-02T23:25:39+00:00

I’m trying to write simple client and server based on sockets in C. The

  • 0

I’m trying to write simple client and server based on sockets in C.
The client sends the size of the char array (including last cell for ‘\0’) and then array of chars.
Server gets the size and tries to allocate memory for the array of chars from the client.
After this, the server looks for a space and copy the characters from index 0 to space, pastes it at the end of the array and sends it back to the client (if there is no space, then the server sends doubled array).

I’ve noticed that sometimes I’m getting
—
|00|
|02|
—
char at the end of the received array from the server.
I’ve run the server and the client using valgrind and it showed that problem is in the server.

Code below.

Client:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>



int
main ()
{
    int sockfd;
    socklen_t len;
    struct sockaddr_in address;
    int result;

    char ch;
    char *string;
    int i;

    sockfd = socket (AF_INET, SOCK_STREAM, 0);

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr ("127.0.0.1"); 
    address.sin_port = htons (9734);
    len = sizeof (address);

    result = connect (sockfd, (struct sockaddr *) &address, len);

    if (result == -1)
    {
        perror ("oops: netclient");
        exit (1);
    }

    string = (char*)malloc(sizeof(char));

    for (i =0; ch = getchar(); i++)
    {
        string = (char*)realloc(string, (i+1)*sizeof(char));

        if(ch != '\n')
            string[i] = ch;
        else if (ch == '\n')
        {
            string[i]='\0';
            break;
        }
    }

    printf("%s\n", string);
    printf("%d\n", i);
    i=i+1;

    write (sockfd, &i, 4);

    write (sockfd, string, i);


    read (sockfd, &i, 4);


    string = (char*)realloc(string, i*sizeof(char));

    read (sockfd, string, i); 

    printf("String recieved: %s\n", string);

    close (sockfd);

    free(string);

    exit (0);
}

And server:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

int
main ()
{
    int server_sockfd, client_sockfd; 
    socklen_t server_len, client_len; 
    struct sockaddr_in server_address; 
    struct sockaddr_in client_address; 

    server_sockfd = socket (AF_INET, SOCK_STREAM, 0); 
    server_address.sin_family = AF_INET; /*ipv4*/
    server_address.sin_addr.s_addr = htonl (INADDR_ANY); 
    server_address.sin_port = htons (9734);
    server_len = sizeof (server_address);
    bind (server_sockfd, (struct sockaddr *) &server_address, server_len); 


    listen (server_sockfd, 5); 
    signal (SIGCHLD, SIG_IGN);

    while (1)
    {
        char *string;
        int i, j, k, l=0; /*variables to iteration*/
        printf ("server waiting\n");


        client_len = sizeof (client_address);
        client_sockfd = accept (server_sockfd,
                (struct sockaddr *) &client_address,
                &client_len); 


        if (fork () == 0)
        {


            read (client_sockfd, &i, 4);
            printf("recieved int %d\n", i);
            string = (char*)malloc(sizeof(char) * (i));
            read (client_sockfd, string, i);
            printf("\nSTRING recieved: %s \n", string);

            for(j=0; string[j]!='\0'; j++)
            {
                if(string[j] == ' ')
                {
                    i = i+j; /*size to enlarge*/
                    break;
                }
            }

            /*sending new size*/
            write(client_sockfd, &i, 4);

            /*enlarge the string*/
            string = (char*)realloc(string, i*sizeof(char));

            for (k = i-j-1; k < i-1 ; k ++)
            {
                string[k] = string[l];
                l++;
            }
            string[k+1] = '\0';

            write(client_sockfd, string, i);


            close (client_sockfd);

            free(string);

            exit (0);
        }



        else
        {
            close (client_sockfd);
        }
    }
}
  • 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-02T23:25:41+00:00Added an answer on June 2, 2026 at 11:25 pm

    Your code ignores the return value of read():

            read (client_sockfd, &i, 4);
    

    You must not ignore this value. The socket interface does not guarantee that just because you asked for 4 bytes, that you will actually get 4 bytes back. The socket interface only guarantees that you will get at least one byte back. You must continue calling read() until you get all the bytes that you want.

    The same goes for when you read the string a couple of lines later.

    Even though your program may appear to work now, this is the sort of problem that will suddenly appear in unpredictable situations like a busy machine or network, you won’t be able to reproduce it, and your program will become unreliable.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.