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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:41:24+00:00 2026-06-02T15:41:24+00:00

My question is : How, (with select()) can I know if a new client

  • 0

My question is : How, (with select()) can I know if a new client is connecting on my server ? A can’t just use accept because accept() is blocking…

Example :
I have two clients set on fd user1 (fd = 4) and user2 (fd = 5). So, I use select to know who write on the socket :

  FD_ZERO(read_fds);
  FD_SET(user1, read_fds);
  FD_SET(user2, read_fds);
  error = select(user2 + 1, read_fds, NULL, NULL, NULL);
  if (FD_ISSET(user1, read_fds) == 1)
     printf("user1 talks.\n");
  ...

But I want know if a new user is connecting to the server, what can I do ?
I have try :

  FD_ZERO(read_fds);
  FD_SET(user1, read_fds);
  FD_SET(user2, read_fds);
  FD_SET((user2 + 1), read_fds);
  error = select(user2 + 2, read_fds, NULL, NULL, NULL);
  if (FD_ISSET(users2 + 1) == 1)
  {
      printf("New user.\n");
      accept(..., ...);
  }
  ...

But with this code, select return -1 every time…

A solution ? ^^

  • 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-02T15:41:26+00:00Added an answer on June 2, 2026 at 3:41 pm

    You have to store the connected clients somewhere, like a list for example.

    struct client_node
    {
        int sockfd;
        /* Other data that might be needed */
        struct client_node *next;
    };
    
    struct client_node *client_list = NULL;
    
    /* ... */
    
    FD_ZERO(&read_fds);
    FD_SET(listening_socket, &read_fds);
    
    int maxfd = listening_socket;
    
    for (struct client_node *c = client_list; c; c = c->next)
    {
        FD_SET(c->sockfd, &read_fds);
        maxfd = MAX(maxfd, c->sockfd);
    }
    
    int res = select(maxfd + 1, &read_fds, NULL, NULL, NULL);
    

    As for the blocking issue for accept, as you can see I have added the a socket (variable listening_socket) that is the socket you are listening on. When select says you can read from it, it means there is a new connection and that you can call accept.

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

Sidebar

Related Questions

HTML Javascript question to get the selected value of a input-select I can use
Question: I can get the SQL Server database language by querying: SELECT @@language And
Just a quick question about using select() . I'm using select() to read from
I am new to web services and i have a question: Should a web
This is probably a basic LINQ question. I have need to select one object
I think it's kind of noob question but I'm new to SQL Server in
That can be silly question but I really don't know the reason. // i
I am new to XML and Linq to XML and I just can't find
a conceptual question: I have a global system which sales agents use to write
I am very new to working with databases. Now I can write SELECT ,

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.