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

The Archive Base Latest Questions

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

I store my clients like this.. int MAXCLIENTS = 4; int ClientCount = 0;

  • 0

I store my clients like this..

   int MAXCLIENTS = 4;
   int ClientCount = 0;
   int FreeSpot[MAXCLIENTS];

    typedef struct CLIENTS_FD{

    int sock;
    struct sockaddr_in cli_adr;

    }cliuse;

    cliuse MYCLIENTS[4];

do{

    NewSFD = accept(ServerFD,(struct sockaddr *) &cli_addr, &clilen);
              if (NewSFD < 0)
              {
                 if (errno != EWOULDBLOCK)
                 {
                    perror("  accept() failed");
                    DCSERVER = TRUE;
                 }
                 break;
              }
               if(ClientCount < MAXCLIENTS){
                for(loop = 0; loop < MAXCLIENTS; loop++){

                if(FreeSpot[loop]<0){

                Clients[loop].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);


}while(NewSFD != -1);

I know i can store my clients file descriptor but how can i store my clients struct and use it afterwards i want to send message to it?.. say i want to send message to client with ip 192.168.5.10.

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:08:16+00:00Added an answer on June 13, 2026 at 11:08 am

    I think you miss some important point about network programming. Maybe you should read this for more details and infos how to start.

    Nevertheless accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) will fill client addres structure which is second parameter (struct sockaddr *addr). You can then easily add this address to your CLIENTS_FD structure.

    Clients[loop].sock = NewSFD; 
    
    Clients[loop].cli_adr = cli_addr;
    

    assuming that Clients is cliuse (or struct CLIENTS_FD).

    Anyway, like mentioned in some comments above you don’t need to store this address anywhere. All you need to communicate with your client is its sockfd (which is returned by accept).

    What is more there could be some bugs in your code:

    int FreeSpot[MAXCLIENTS];
    

    is uninitialized so when you try to check it

    if(FreeSpot[loop]<0)
    

    this could lead to wrong behaviour. You could simply write int FreeSpot[MAXCLIENTS] = {0}; You should then somewhere (probably inside if(FreeSpot[loop]<0) statement) add something like this FreeSpot[loop] = 1; to set it properly before next checks.

    int MAXCLIENTS = 4; 
    
    int FreeSpot[MAXCLIENTS];
    

    Since C99 it is possible to declare tables using something else than constant. This is called VLA (variable length array). Nevertheless in your case I can see no point to use VLA. Try #define MAXCLIENTS 4 instead (as suggested in some comment above).

    To write to the clients, after returning from your do-while loop you can simply use something like below:

    send(Clients[i].sock, msg, len, flags);
    

    where i is number of your client (range 0-3), again assuming that Clients is cliuse (or struct CLIENTS_FD).

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

Sidebar

Related Questions

I've got a table that looks like this: Foo FooId : int (PK) BarId
I have a SQL Server database designed like this : TableParameter Id (int, PRIMARY
I have developed a web application and various clients can store records to that
I am writing clients in Java that store the data on a database server.
I'd like to create a class where the client can store a lambda expression
I'm using redis in my python application to store simple values like counters and
I have written an ASP.NET web service. It looks like this: WebServices.logic pLogic =
Problem fixed, see below What I'm doing is this: Generate random number -> store
Often, when I need to store system properties like admin info, versions, and so
I have a table that stores various clients I have done work for, separated

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.