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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:11:06+00:00 2026-05-26T20:11:06+00:00

Say I want a have a C program that does this: 1. User will

  • 0

Say I want a have a C program that does this:
1. User will run a client with a input string, for example, ‘abc’
2. Server will get the string and capitalize it, and then return ‘ABC’
3. Client will NOT be disconnected, but he can enter more string at command line to get the result.
4. Multiple clients (under 5) can be connected at the same time.

What’s the code structure for server look like? Here is what I got:

master_socket = socket();
bind();
listen();

while(true)
{
     **int newsockfd = accept();
     if (newsockfd < 0)
         //server keeps coming to here
         continue; //no new connection
     else
     {**
        int pid = fork();

        if (pid == -1) {      /* fork() failed */
            perror("fork");
            exit(EXIT_FAILURE);
        }
        //parent
        if (pid > 0)
        {
            close(newsockfd);
            waitpid(pid, NULL, WNOHANG);
        }
        else
        {
            close(master_socket);
            //receive input string
            receive();
            modify();
            //send back string
            send();

        }
        //close(newsockfd);  **//not sure where to put**

     }

}

particularly, I don’t know where to put close(newsockfd) and how to use accept in this case.

The server right now just keep going to the the continue. When there is new connection, it will respond correctly. But it will ignore any existing client who wants to send something again.

So the user is able to input the first string, but the user’s second string cannot reach the server. But if I open another terminal, and try to connect to the server again, it can still work.

Thanks a lot.

  • 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-26T20:11:06+00:00Added an answer on May 26, 2026 at 8:11 pm

    The child process must loop processing input, and it mustn’t get back to the accept(): at EOS it must close the accepted socket and exit.

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

Sidebar

Related Questions

Lets say a have program.exe , i will create a python script that will
Say I want to have a method that takes any kind of number, is
Let's say that I want to have a table that logs the date and
Say I have the following interface that I want to share between my server
Say I have a list and I want it arranged so that the sum
I have an existing program that has its own main loop, and does computations
trying to run this program. I think that to setup all of the web
I have a question regarding dependency injection. say i want to create a class
I have some kind of problem with jQuery selectors. Let's say i want to
Say I have two tables I want to join. Categories: id name ---------- 1

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.