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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:25:36+00:00 2026-06-13T11:25:36+00:00

I’m connected to certain client then we’re communicating. Another client connects to my server

  • 0

I’m connected to certain client then we’re communicating. Another client connects to my server which will instantly output that there’s a new connection.. e.g Accepting while listening to other clients?.. How can i do that?..

I have already a working one but it only does one thing at a time. When i’m already communicating with my client i can’t accept incoming connection or data to other clients. I can only accept new connection or data from other clients when the client i’m communicating is disconnected. how can i do so that it can process both accept and listen to client at the same time. I don’t want to use threads.

Here’s a part of my code.

do
{
   fduse = fdin;
   printf("Waiting for Connection\n");
   err = select(sMax + 1, &fduse, NULL, NULL, NULL);
   if (err < 0)
   {
      perror("  select() failed");
      break;
   }
   DescRead = err;
   for (SockStorage = 0; SockStorage <= sMax && DescRead > 0; ++SockStorage)
   {
      if (FD_ISSET(SockStorage, &fduse))
      {
         DescRead -= 1;
         if (SockStorage == socketFd)
         {
            printf("  Listening socket is readable\n");
            do
            {
               NewSFD =
                  accept(socketFd, (struct sockaddr *)&cli_addr, &clilen);
               if (NewSFD < 0)
               {
                  if (errno != EWOULDBLOCK)
                  {
                     perror("  accept() failed");
                     DCSERVER = TRUE;
                  }
                  break;
               }
               if (ClientCount < MAX_CLIENTS)
               {
                  for (loop = 0; loop < MAX_CLIENTS; loop++)
                  {
                     if (Clients[loop].connected_sock < 0)
                     {
                        Clients[loop].connected_sock = NewSFD;
                        break;
                     }
                  }
                  ClientCount++;
               }
               else
               {
                  printf("Maximum Client Reached.\n");
                  char *sendtoclient = "Server full. ";
                  send(NewSFD, sendtoclient, strlen(sendtoclient), 0);
                  close(NewSFD);
                  break;
               }
               ip = ntohl(cli_addr.sin_addr.s_addr);
               printf("  Connection from %d.%d.%d.%d\n",
                  (int)(ip >> 24) & 0xff,
                  (int)(ip >> 16) & 0xff,
                  (int)(ip >> 8) & 0xff, (int)(ip >> 0) & 0xff);
               dlogs(ip);

               FD_SET(NewSFD, &fdin);
               if (NewSFD > sMax)
                  sMax = NewSFD;
            }
            while (NewSFD != -1);
         }
         else
         {
            int d;
            for (d = 0; d < MAX_CLIENTS; d++)
            {
               printf("Descriptor ID: %d\n", Clients[d].connected_sock);
            }

            pfds[0].fd = fd;
            pfds[0].events = POLLIN;
            pfds[1].fd = SockStorage;
            pfds[1].events = POLLIN;
            state = FALSE;
            do
            {
               rc = poll(pfds, 2, -1);
               if (pfds[0].revents & POLLIN)
               {
                  while ((nbytes = read(fd, buf, sizeof (buf) - 1)) > 0)
                  {
                     buf[nbytes] = '\0';
                     printf("%s\n", buf);
                  }
                  pfds[0].events = 0;
                  pfds[1].events = POLLIN | POLLOUT;
               }
               if (pfds[1].revents & POLLIN)
               {
                  err = recv(SockStorage, strbuf, sizeof (strbuf), 0);
                  if (err < 0)
                  {
                     if (errno != EWOULDBLOCK)
                     {
                        perror("  recv() failed");
                        state = TRUE;
                     }
                     break;
                  }
                  if (err == 0)
                  {
                     printf("  Connection closed\n");
                     state = TRUE;
                     break;
                  }
                  dSize = err;
                  printf("  %d bytes received\n", dSize);
               }

               if (pfds[1].revents & POLLOUT)
               {
                  int s;
                  for (s = 0; s < MAX_CLIENTS; s++)
                  {
                     if (Clients[s].connected_sock > 0)
                     {
                        err =
                           send(Clients[s].connected_sock, buf,
                           strlen(buf), 0);
                        if (err < 0)
                        {
                           perror("  send() failed");
                           track = s;
                           state = TRUE;
                           break;
                        }
                     }
                  }
                  pfds[0].events = POLLIN;
                  pfds[1].events = POLLIN;
               }

            }
            while (TRUE);
            fopen("/sockF.txt", "w");
            if (state)
            {
               ClientCount--;
               close(SockStorage);
               FD_CLR(SockStorage, &fdin);
               if (SockStorage == sMax)
               {
                  while (FD_ISSET(sMax, &fdin) == FALSE)
                     sMax -= 1;
               }
            }
         }
      }
   }
} while (DCSERVER == FALSE);
cleanUP(SockStorage, sMax);
}

I’ve been working on it for two days and still i can’t get it. thanks..

  • 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-13T11:25:37+00:00Added an answer on June 13, 2026 at 11:25 am

    Well, to give you an example of a working multi-connection daemon, have a look at this example

    If you need a further explanation to go with it, shoot.

    Note, the code in that example has been stripped of things like daemon_init(), etc, so just copy-pasting blindly and trying to compile it won’t work.

    Added setnonblocking() as per request.

    void setnonblocking(sock)
      int sock;
    {
      int opts;
    
      opts = fcntl(sock, F_GETFL);
    
      if (opts < 0)
        {
          syslog(LOG_ERR, "fcntl(F_GETFL) failed");
          exit(EXIT_FAILURE);
        }
    
      opts = (opts | O_NONBLOCK);
    
      if (fcntl(sock, F_SETFL, opts) < 0)
        {
          syslog(LOG_ERR, "fcntl(F_SETFL) failed");
          exit(EXIT_FAILURE);
        }
    
      return;
    }
    

    And daemon_init()

    int daemon_init(void)
    {
      pid_t pid;
      int i;
    
    
      if ((pid = fork()) < 0)
        {
          return -1;
        }
      else if (pid != 0)
        {
          exit(0);
        }
    
      for (i=getdtablesize();i>=0;--i) close(i);
      i = open("/dev/null", O_RDWR); /* open stdin */
      dup(i); /* stdout */
      dup(i); /* stderr */
    
      setsid();
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.