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