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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:32:05+00:00 2026-05-31T02:32:05+00:00

I am trying to build a server which can serve for many clients. The

  • 0

I am trying to build a server which can serve for many clients.

The server just do a simple job: get the input string from client and then change each letter to upper case.

But the problem is, when I try to shut down one client, for example, type “Ctrl-C”, then my OS will be shut down suddenly.

I use the Ubuntu 10.10 and CentOS to test my program, but always the same problem.
Here’s my source code:

/* client.c */
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXLINE 80
#define SERV_PORT 8000

int main (void)
{
    struct  sockaddr_in servaddr, cliaddr;
    char    buf[MAXLINE];
    int sockfd, n;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
    servaddr.sin_port = htons(SERV_PORT);

    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    while (fgets(buf, MAXLINE, stdin) != NULL) {
        write(sockfd, buf, strlen(buf));
        n = read(sockfd, buf, MAXLINE);
        if(n == 0) printf("Connect closed\n");
        else write(STDOUT_FILENO, buf, n);
    }
    close(sockfd);
    return 0;
}

/* server */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXLINE 80
#define SERV_PORT 8000

void sigchld_func (int signo) {
    wait(NULL);
}

int main (void)
{
    struct  sockaddr_in servaddr, cliaddr;
    char    buf[MAXLINE];
    char    str[INET_ADDRSTRLEN];
    int     listenfd, connfd;
    socklen_t   cliaddr_len;
    int     n, i;
    pid_t   pid;

    signal(SIGCHLD, sigchld_func);

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    listen(listenfd, 20);

    printf("Accepting connections...\n");

    while (1) {
        cliaddr_len = sizeof(cliaddr);
        connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
        if ((pid = fork()) < 0) {
            perror("fork error!");
            exit(1);
        } else if (pid > 0) {
            close(connfd);
        } else {
            close(listenfd);
            while (1) {
                n = read(connfd, buf, MAXLINE);
                if (n <= 0) {
                    printf("Connection closed\n");
                    break;
                }
                inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str));
                printf("Received from %s at port %d\n", str, ntohs(cliaddr.sin_port));
                for (i = 0; i < n; i++)
                    buf[i] = toupper(buf[i]);
                write(connfd, buf, n);
            }
            close(connfd);
        }
    }
    return 0;
}
  • 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-31T02:32:06+00:00Added an answer on May 31, 2026 at 2:32 am

    Your server code is causing a fork bomb, and you’re not seeing it properly because you’re not checking the return value of accept.

    The root cause is that in the child that reads/writes to the client, after the connection is closed, you’re not either calling exit or returning out of main.

    So the child process stays in the toplevel while(1) loop, tries to accept on listenfd (but you closed that one, which is good). accept fails, and you fork regardless. Tight loops of forks will slow down your computer, the scheduler can’t deal with them properly (unless you have counter-measures in place).

    So exit out of the program after close(connfd), and add more error checking in your code.

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

Sidebar

Related Questions

I am trying to post data from PHP to Server which is built in
I'm trying to build a server that will receive files sent by clients over
I'm struggling a lot trying to get our build server going. I am currently
I'm trying to build a Spring server for GWT (you can think of it
I'm trying to build a standard compliant website framework which serves XHTML 1.1 as
My group is trying to build a server using Apache Thrift but we are
I'm trying to build a client server application using POSIX shared memory and POSIX
I am trying to build something portlet server-ish on the google app engine. (as
I'm trying to install Team Build (2008) on a different Build Server (BS) to
I am trying to setup a TeamCity 4.0 build server to run VS2008 (non-TFS)

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.