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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:43:55+00:00 2026-06-14T02:43:55+00:00

I want to write a multi clients socket program, but I get Bad file

  • 0

I want to write a multi clients socket program,
but I get Bad file descriptor when the stage of accept.
How can I correct my code? Thanks!

Here is my code
http://codepad.org/q0N1jTgz
Thanks!
Here is my part of code!

while(1)
{
    struct sockaddr_in client_addr;
    int addrlen = sizeof(client_addr);

    /*Accept*/
    if(clientfd = accept(sockfd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen) < 0)
    {
        perror("Accpet Error");
        close(sockfd);
        exit(-1);
    }

    /*Fork process*/
    if(child = fork() < 0)
    {
        perror("Fork Error");
        close(sockfd);
        exit(-1);
    }
    else if(child == 0)
    {
        int my_client = clientfd;
        close(sockfd);

        send(my_client, welcome, sizeof(welcome), 0);

        while ((res = recv(my_client, buffer1, sizeof(buffer1), 0)) > 0)
        {
            string command(buffer1);
            cout << "receive from client:" << command << ", " << res << " bytes\n";
            memset(buffer1, '\0', sizeof(buffer1));     
        }
    }

    close(clientfd);

} 
  • 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-14T02:43:56+00:00Added an answer on June 14, 2026 at 2:43 am

    there are a few bugs in your code
    first you need to use parentheses around the assignments for child and clientfd.

    line 68 should be changed to

     if((clientfd = accept(sockfd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen)) < 0)
    

    and line 76 should be

      if((child = fork()) < 0)
    

    additionally you must return or exit() from the forked process since you have already closed the listening socket.

    so add exit(0); or return 0; after line 94

    I highly recommend you compile your code with warnings enabled, to catch the assignment problems early. e.g use the -Wall and -Wextra flags if you are using gcc or g++

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

Sidebar

Related Questions

I have write a multi-level Preferences in program with xml File. and i want
i want to write a program using multi-threading, raw sockets, to scan the ports
as titled. I have use synchronized to guard multi-thread write. but I want know
i want to write a function that prints multi-dimensional objects which are text (or
I want to write a program in Prolog that confirms if a b-tree of
I want to write a program to contact a PHP script online, and show
I am in Mac OSX, Cocoa . I want to read and write multi
I'm trying to write a multi-threaded program, the number of threads based on command-line
I'm new to Scala, and I want to write some multi-threaded code with pattern
Python's string-literal juxtaposition makes multi-line strings much easier and prettier to write, but when

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.