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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:22:52+00:00 2026-06-08T22:22:52+00:00

My terminal keep create and forking . I think i did something wrong in

  • 0

My terminal keep create and forking . I think i did something wrong in my code, i actually want fork when client connect, but when i launch the server, its sort of keep forking as i check with linux command “ps” ,how do i change my code to make it work properly.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>


using namespace std;

int main()
{

int serverFd;
int clientFd;
int serverLen;
int clientLen;
string message;
string serverSockAddrPtr;
struct sockaddr* serverSockAddressPnt;
struct sockaddr* clientSockAddressPnt;
struct sockaddr_un serverAddress;
struct sockaddr_un clientAddress;


// SOCKET CREATION PART - SERVER
serverFd = socket (AF_LOCAL, SOCK_STREAM, 0);

/* Set domain type */
serverAddress.sun_family = AF_LOCAL; 

/* Set name */ 
strcpy (serverAddress.sun_path, "CountryServer"); 


/* GET SIZE OF Server Addres */
serverLen = sizeof serverAddress;
/* GET SIZE OF Client Addres */
clientLen = sizeof clientAddress;

/* Get Server Sock Address Pointer*/
serverSockAddressPnt = (struct sockaddr *) &serverAddress; 
/* Get Client Sock Address Pointer*/
clientSockAddressPnt = (struct sockaddr *) &clientAddress; 

/* Create file */
bind (serverFd, serverSockAddressPnt , serverLen);

/* listen for connection */
listen (serverFd,5);

// SOCKET CREATION END - SERVER


while(1)
{
//accept client connection
clientFd = accept(serverFd, clientSockAddressPnt, (socklen_t*)&clientLen);

if(fork()==0)
{
message="Successfully connected to the server";
write(clientFd,message.c_str(),strlen(message.c_str())+1);
close(clientFd);
exit(0);
}
else
close(clientFd);

}

return 0;
}

My question is:
How do i make the server to fork a process to handle client query when they connect.

I don’t want it keep forking for no reasons.
Thanks for all help, this my first C coding to learn more about programming.

My client and server communicate through localhost, sockaddr_un and not internet.

  • 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-08T22:22:53+00:00Added an answer on June 8, 2026 at 10:22 pm

    What you should have for your loop is:

    while (1) {
        clientFd = accept(serverFd, clientSockAddressPnt, (socklen_t*)&clientlen);
        if (clientFd >= 0) {
            if(fork() == 0) {
                message="Successfully connected to the server";
                write(clientFd,message.c_str(),strlen(message.c_str())+1);
                close(clientFd);
                exit(0);
            }
            else
                close(clientFd);
        }
    }
    

    That way, you only ever fork when accept returns a nonnegative value (which means it completed successfully.

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

Sidebar

Related Questions

I keep forgetting - I am selecting into an output file with terminal MySQL
I am trying to do a terminal update and I keep getting this error,
I use terminal from Mac OS X Leopard to connect to remote Linux machines
On linux something like: <target name=runDo> <exec executable=gnome-terminal spawn=true> <arg line=-t 'Title' --geometry=120x80 -e
I keep running across errors when trying to compile code which utilizes the boost::filesystem
I want to set up some alias and would like to keep them so
I want to run a process in a remote linux server and keep that
I'm trying to connect java to a Oracle database within terminal. I'm not very
I want to be able to bring up an interactive python terminal from my
I would like to change the text color to bright green (#66FF00) but keep

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.