我正在尝试创建一个连接到多个客户端的服务器。基本上,我不想在有人新连接到服务器时向所有客户端发送提示。但是, send() 命令不发送数据。:( 我想知道代码有什么问题。
int main(int argc, char **argv){
int listenfd,connfd;
struct sockaddr_in servaddr,cliaddr;
socklen_t len = sizeof(cliaddr);
char cli_ip[32];
char mesg[1024];
char broadcast[500];
int maxconn,counter = 0;
// struct addrinfo hints, *ai, *p;
socklen_t addrlen;
fd_set master;
fd_set read_fds;
int fdmax;
int yes = 1;
int i, j;
char remoteIP[INET6_ADDRSTRLEN];
FD_ZERO(&master);
FD_ZERO(&read_fds);
listenfd = socket(PF_INET,SOCK_STREAM,0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(54321);
setsockopt(listenfd, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int));
if(bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0 ){
perror(NULL);
exit(1);
}
maxconn = 2;
if((listen(listenfd,5)) == -1){
perror("listen");
exit(2);
}
FD_SET(listenfd, &master);
fdmax = listenfd;
while(1){
read_fds = master;
if((select(fdmax + 1,&read_fds, NULL, NULL, NULL )) == -1){
perror("select");
exit(3);
}
for(i = 0; i <= fdmax; i++){
if(FD_ISSET(i, &master)){
if(i = listenfd){
if(counter<= maxconn){
addrlen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &addrlen);
counter++;
inet_ntop(AF_INET,(struct in_addr *)&cliaddr.sin_addr, cli_ip, sizeof(cli_ip));
if(connfd == -1){
perror("accept");
exit(4);
}else{
FD_SET(connfd, &master);
if(connfd > fdmax)
fdmax = connfd;
sprintf(broadcast,"Player %d from %s has connected.\n",connfd, cli_ip);
for(j = i+ 1; j <= connfd; j++){
if(FD_ISSET(j, &master)){
/*somehow this doesn't work :(*/ if((send(j, broadcast, sizeof(broadcast), 0))){
perror("send");
exit(5);
}
}
}
printf("%s,%d\n",broadcast,counter);
printf("%d,%d\n",i,fdmax);
break;
}
}else{
printf("server full\n");
close(connfd);
break;
}
}
}
}
}
return 0;
}
如您所见,此代码与 Beej 指南中的代码有点相似。那么为什么客户端没有收到广播呢?