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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:40:07+00:00 2026-05-17T18:40:07+00:00

I have developed (TCP) server to listen to a client and interact with it.

  • 0

I have developed (TCP) server to listen to a client and interact with it. Now I’m trying to adapt that server code to listen to mulitple clients. I am wanting to use select, but I’m getting confused with some of the examples and explainations I’ve found.

I have been reading: http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/select.htm and http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/select.htm both of which have been recommended to me.. :S

The first site’s example seems less complex.. (though still don’t know how to adapt it to my code as I am very very very new to network etc.) but I worry that I’m missing out on key aspects due to the complexity of the second site’s example.

Below is kind of a snapshot of my server code when only listening for one client (some psuedocode included to minimize not so important stuff):

int main(int argc, char *argv[])
{
    int    sockfd, newsockfd, portno, clilen;
    char   buffer[3];
    struct sockaddr_in serv_addr, cli_addr;
    int    n;

    if (argc < 2)
    {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0){error("ERROR opening socket");}

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno                    = atoi(argv[1]);
    serv_addr.sin_family      = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port        = htons(portno);


    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        {error("ERROR on binding");}

    listen(sockfd,5);

    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

    if (newsockfd < 0){error("ERROR on accept");}


    while (UNTIL END OF FILE)
    {
        <DO SOME FILE READING STUFF>

        n = write(newsockfd, "test/n", 5);

        if (n < 0){error("ERROR writing to socket");}


        bzero(buffer,3);
        n = read(newsockfd,buffer,3);

        if (n < 0){error("ERROR reading from socket");}

        buffer[n] = 0;

        <DO SOME STRING STUFF>

        while(done != 1)
        {
            bzero(buffer,3);
            n = read(newsockfd,buffer,3);

            if (n < 0){error("ERROR reading from socket");}

            buffer[n] = 0;    

            if(strcmp(buffer, "CO"))
            {
                done = 1;
            }
        }

        done = 0;
    }

    <DO STUFF>


    n = write(newsockfd, "DN\n", 2);

    if (n < 0){error("ERROR writing to socket");}

    close(sockfd);
    close(newsockfd);

    return 0;   

}

Which site’s example for select() would work best for my what I’m trying to do (change the server code to listen to multiple clients)? Could someone explain select() in some more simplier terms for me? (since I’m so new to this and all…)

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-05-17T18:40:08+00:00Added an answer on May 17, 2026 at 6:40 pm

    You need to wrap a loop around the accept() and following code, so that you can accept more than one connection over the life of the program. You then need to decide how your server is going to handle multiple connections. You have a few choices:

    • Use multiple threads, one thread per client.
    • Use a forking server, one process per client.
    • Use a single server, which will use select() to decide which file descriptor(s) are ready.

    In the first two cases, the child thread or process is single-mindedly dealing with one client; it will wait while the client is working out how to respond to what it sends, which means that other threads or processes get a turn at the CPU. There are variations on the theme that prelaunch some number of worker threads or processes and arrange for those to pick up the workload, but that’s more complex.

    In the last case, you’ll set up an array of the file descriptors you want select() to work on, and then pass a copy of that list to select() (since it tramples all over it). When you’ve got work to do (because select() returns), you perform an accept() on the file descriptor that you’re listening on if that’s ready, or you read from the already opened socket descriptors if they’re ready.

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

Sidebar

Related Questions

I have developed some software(vb.net) that records fees paid by the students. The purpose
I have developed a wordpress theme with header, footer, sidebar, index, page, single and
I have developed a small C# form application which calls a web service. Everything
I have developed an application which allows the user to switch between themes. I'm
Is there a distributed application framework (commercial is okay as well) that supports iPhone
I need to develop a WCF server (basically a web service which will eventually
I am going to develop a WCF service that connects back-end system with two
I'm making an iOS app - real-time game, wanna use UDP protocol. I'm searching
I'm going to develop mostly Django sites on a MacBook Pro and would like
Like in the title, I want to encrypt data, which is sent over network.

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.