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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:57:36+00:00 2026-05-28T06:57:36+00:00

I wrote a simple server in C. It answers connections on port 8888. It

  • 0

I wrote a simple server in C. It answers connections on port 8888. It works great…until I try to run it as a background process.

When I run it like

$ ./server

And then attempt to connect with a client it works fine. When I attempt to run it like:

$ ./server &

Or if I run it like

$ ./server 

And then detach it with CTRL+z and attempt to connect with a client I get

Connection Refused

Has anyone encountered this problem before? I would greatly appreciate a solution.

Here is the code surrounding the accept( ) call as requested:

  char remoteIP[ INET6_ADDRSTRLEN ];
  int yes=1;    /* for setsockopt() SO_REUSEADDR, below */
  int i, rv;
  struct addrinfo hints, *ai, *p;

  FD_ZERO( &master );  /* clear the master and temp sets */
  FD_ZERO( &read_fds );

  /* get us a socket and bind it */
  memset( &hints, 0, sizeof hints );
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;

  if ( ( rv = getaddrinfo( NULL, URL_PORT, &hints, &ai ) ) != 0 )
  {
    /* fprintf( stderr, "selectserver: %s\n", gai_strerror( rv ) ); */
    exit( 1 );
  }

  /* printf( "Listening on port %s for URLs...\n", URL_PORT ); */
  for( p = ai; p != NULL; p = p->ai_next )
  {
    listener = socket( p->ai_family, p->ai_socktype, p->ai_protocol );
    if ( listener < 0 )
    {
      continue;
    }

    /* lose the pesky "address already in use" error message */
    setsockopt( listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof( int ) );
    if ( bind( listener, p->ai_addr, p->ai_addrlen) < 0 )
    {
      close( listener );
      continue;
    }
    break;
  }

  /* if we got here, it means we didn't get bound */
  if ( p == NULL )
  {
    /* fprintf( stderr, "selectserver: failed to bind\n" ); */
    exit( 2 );
  }

  freeaddrinfo( ai ); /* all done with this */

  /* listen */
  if ( listen( listener, 10 ) == -1 )
  {
    perror( "listen" );
    exit( 3 );
  }

  /* add the listener to the master set */
  FD_SET( listener, &master );

  /* keep track of the biggest file descriptor */
  fdmax = listener; /* so far, it's this one */

  /* main loop */
  for( ; ; ) {


    for( i = 0; i <= fdmax; i++ )
    {
      if ( SOCKETS[ i ].in_progress )
      {
        if ( pthread_join( SOCKETS[ i ].thread, NULL ) != 0 )
        {
          /* fprintf( stderr, "Error terminating thread %i\n", i ); */
        }
        else
        {
          SOCKETS[ i ].in_progress = FALSE;
        }
      }
    }

    read_fds = master; /* copy it */

    if ( select( fdmax + 1, &read_fds, NULL, NULL, NULL ) == -1 )
    {
      perror( "select" );
      exit(4);
    }

    /* run through the existing connections looking for data to read */
    for( i = 0; i <= fdmax; i++ ) {

      if ( FD_ISSET( i, &read_fds ) && SOCKETS[ i ].in_progress == FALSE )
      {
        /* we got one!! */
        if ( i == listener )
        {
          /* handle new connections */
          addrlen = sizeof remoteaddr;
          newfd = accept( listener, ( struct sockaddr * ) &remoteaddr, &addrlen );
          if ( newfd == -1 )
          {
            perror( "accept" );
          }
          else
          {
            FD_SET( newfd, &master ); /* add to master set */

            if ( newfd > fdmax )
            {  /* keep track of the max */
              fdmax = newfd;
            }

            /*
            printf( 
              "selectserver: new connection from %s on socket %d\n",
              inet_ntop( remoteaddr.ss_family, get_in_addr( ( struct sockaddr* ) &remoteaddr ), remoteIP, INET6_ADDRSTRLEN ), newfd );
            */
          }
        }
  • 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-28T06:57:36+00:00Added an answer on May 28, 2026 at 6:57 am

    Your process reads from or writes to its controlling terminal and hence is stopped by SIGTTIN or SIGTTOU when you run it in the background with &.

    Relevant excerpt from the bash manpage:

    Only foreground processes are allowed to read from or write to the
    terminal. Background processes which attempt to read from (write to)
    the terminal are sent a SIGTTIN (SIGTTOU) signal by the terminal
    driver, which, unless caught, suspends the process.

    The sending of SIGTTOU is controlled by a flag which is off by default, so your problem is likely caused by reading from the controlling terminal. If you want to prevent background processes from writing to the terminal (i.e. re-enable the sending of SIGTTOU to processes attempting a background write), use this command:

    stty tostop
    

    You can revert back to the default behavior with:

    stty -tostop
    

    When you press Ctrl+Z you cause SIGTSTP to be sent to the process. Default disposition of this signal is also to stop the process. If you wish to make the process continue running in the background, use this command:

    bg %1
    

    Note that the job number may be different in your case. Check using the job command.

    Note that unlike SIGSTOP these three signals can be handled or ignored by your process if you dislike the behavior. The read/write system calls will then return EINTR instead of blocking.

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

Sidebar

Related Questions

I have a server app that listens for connections on port 8888. I am
I wrote an XML RPC server in python and a simple Test Client for
I wrote a simple server and client apps, where I can switch between TCP,
I wrote two simple programs server and a client using sockets in C++ (Linux).
I've wrote a simple client and a server. They both can configured to use
I want to write a simple socket server, however I'd like it to be
I wrote a simple server client socket program and when I recompile the server
I'm trying to write simple proxy server for some purpose. In it I use
I'm trying to write a simple server in c that plays a two player
I want to write a simple SMPP Server that basically forwards traffic to another

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.