简体   繁体   English

存储客户结构并在以后使用

[英]Store a clients struct and use it afterwards

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. 我知道我可以存储我的客户文件描述符,但是我如何存储我的客户结构并在以后使用它,我想向它发送消息?..说我想向IP 192.168.5.10的客户发送消息。

thanks. 谢谢。

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 ). 但是accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)将填充客户端addres结构,它是第二个参数( struct sockaddr *addr )。 You can then easily add this address to your CLIENTS_FD structure. 然后,您可以轻松地将此地址添加到CLIENTS_FD结构中。

Clients[loop].sock = NewSFD; 

Clients[loop].cli_adr = cli_addr;

assuming that Clients is cliuse (or struct CLIENTS_FD ). 假设客户端是客户端(或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). 您需要与客户进行交流的只是它的sockfd (由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}; 您可以简单地编写int FreeSpot[MAXCLIENTS] = {0}; You should then somewhere (probably inside if(FreeSpot[loop]<0) statement) add something like this FreeSpot[loop] = 1; 然后,您应该在某个地方(可能在if(FreeSpot[loop]<0)语句内)添加类似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. 从C99开始,可以使用常量以外的其他东西声明表。 This is called VLA (variable length array). 这称为VLA(可变长度数组)。 Nevertheless in your case I can see no point to use VLA. 但是,就您而言,我看不到使用VLA的意义。 Try #define MAXCLIENTS 4 instead (as suggested in some comment above). 尝试改为#define MAXCLIENTS 4 (如上面的某些注释所建议)。

To write to the clients, after returning from your do-while loop you can simply use something like below: 要写给客户,从do-while循环返回后,您可以简单地使用如下代码:

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 ). 其中i是你的客户(范围0-3)的数量,再假设客户是cliuse (或struct CLIENTS_FD )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM