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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:23:48+00:00 2026-06-17T21:23:48+00:00

The objective of my program is to use select to manage multiple sockets. However,

  • 0

The objective of my program is to use select to manage multiple sockets. However, I thought of trying it with one socket first. Now, the problem that I am facing is that initially client sends data to server, and server receives it and displays it, but then when client again sends some data, the server code remains still at select command.

here are some snippets that will give you an idea of how I am initializing the socket.

if((master_socket = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
      exit(1);
    }

    if((bind(master_socket, (struct sockaddr *)&req, sizeof(req))) < 0)
    {
      exit(1);
    }

    listen(master_socket, 5);
    FD_SET(master_socket,&listening);

    /* wait for connection, then receive and print text */
    len = sizeof(struct sockaddr);
    while(1)
     {
            FD_ZERO(&listening);  //Flush out everything in socket
        FD_SET(master_socket,&listening); // Add master
        if(f_client>0)           // Add client if any
        {
            FD_SET(f_client,&listening);
        }
        printf("Checking for new connection \n");
           //Timeout is null, so waiting indefinitely
        rc = select(FD_SETSIZE, &listening, NULL, NULL, NULL);

     if (FD_ISSET(master_socket, &listening))
        {
          printf("Master side invoked\n");
          if((f_client = accept(master_socket, (struct sockaddr *)&req, &len)) < 0)
          {
             exit(1);
              }
            }

    else if (FD_ISSET(f_client,&listening))
    {
         if ((valread = read( f_client , buf, 1024)) == 0)
         {
            close(f_client);
            f_client=0;
         }
         else
         {
            fputs(buf, stdout);  
         }

    }

}

Basically in above program, it connects to the server, maintains a file descriptor for client f_client and add it. And in every round, it clears the socket, add master socket, and client socket if any, and then checks. Problem here is, first time it works, but second time when client sends some data. it gets hang to rc = select(FD_SETSIZE, &listening, NULL, NULL, NULL);

I am not to understand what is wrong here. Can anyone help?

  • 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-17T21:23:49+00:00Added an answer on June 17, 2026 at 9:23 pm
         if ((valread = read( f_client , buf, 1024)) == 0)
         {
            close(f_client);
            f_client=0;
         }
         else
         {
            fputs(buf, stdout);  
         }
    

    This code is broken. The fputs function can only be used with a C-style string. You just have arbitrary data with no particular structure. Since you ignore valread, you also have no idea how many bytes you read. (Think about it, how could fputs possibly know how many bytes to output? That information is only in valread, and you don’t pass it that information.)

    You’ve already received the data, this broken code just threw it away. If you log valread, you’ll see that you actually already read it in your last call to read before the call to select that hung.

    instead of fputs, you could use something like this:

    for (int i = 0; i < valread; ++i)
       putchar(buf[i]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new in objective-c, and I'm now trying to write a simple user-sign-in program
I'm trying to write a server-client socket program in C. The objective is for
I am encountering a confusing problem in my Objective-C program. I am trying to
I'm trying to debug an objective C program. It used to run, and I'm
I'm making a simple program in Objective-C. It has one class with a lot
Ok so I've just started learning java (I usually program in Objective-C). My first
I am transferring a program from java to Objective C and need to use
Right now I program exclusively with Objective-C using the Cocoa frameworks to write applications
Objective : programmatically upload documents to Sharepoint Issues : When trying to use :
I am trying to debug my Objective-C program, and I need to print my

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.