gpt4 book ai didi

c - UDP 多客户端聊天服务器

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:19 25 4
gpt4 key购买 nike

我有一个多客户端聊天服务器,出于某种原因,只添加了第一个客户端。我使用了一个教程来帮助我入门。我在下面包含了我的代码。当我尝试添加另一个客户端时,它似乎没有被添加。如果我添加一个客户端,我会像我想要的那样从服务器得到响应,但只有我输入的第一条消息然后它停止正确发送。

服务器代码:

 int main(void)  
{
struct sockaddr_in my_addr, cli_addr[10],cli_temp;
int sockfd;
socklen_t slen[10],slen_temp;

slen_temp = sizeof(cli_temp);
char buf[BUFLEN];
int clients = 0;
int client_port[10];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
{
printf("test\n");
err("socket");
}else{
printf("Server : Socket() successful\n");
}
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sockfd, (struct sockaddr* ) &my_addr, sizeof(my_addr))==-1)
{
err("bind");
}else{
printf("Server : bind() successful\n");
}

int num_clients = 0;
while(1)
{
//receive
printf("Receiving...\n");
if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_temp, &slen_temp)==-1)
err("recvfrom()");

if (clients <= 10) {
cli_addr[clients] = cli_temp;
client_port[clients] = ntohs(cli_addr[clients].sin_port);
clients++;
printf("Client added\n");
//printf("%d",clients);
int i;
for(i=0;sizeof(clients);i++) {
sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_addr[i], sizeof(cli_addr[i]));

}
}
}

close(sockfd);
return 0;

我也包含了客户端代码以防有帮助。

void err(char *s)  
{
perror(s);
exit(1);
}
sig_atomic_t child_exit_status;

void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}

int main(int argc, char** argv)
{
struct sockaddr_in serv_addr;
int sockfd, slen=sizeof(serv_addr);
char buf[BUFLEN];
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
int pid,ppid;

if(argc != 2)
{
printf("Usage : %s <Server-IP>\n",argv[0]);
exit(0);
}

if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
err("socket");

bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_aton(argv[1], &serv_addr.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
pid = fork();
if (pid<0) {
err("Fork Error");
}else if (pid==0) {
//child process will receive from server
while (1) {
bzero(buf,BUFLEN);
//printf("Attempting to READ to socket %d: ",sockfd);
fflush(stdout);
//recvfrom here
if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, &slen)==-1)
err("recvfrom()");

printf("The message from the server is: %s \n",buf);
if (strcmp(buf,"bye\n") == 0) {
ppid = getppid();
kill(ppid, SIGUSR2);

break;
}
}
}else {
//parent will send to server
while(1){
printf("Please enter the message to send: ");
bzero(buf,BUFLEN);
fgets(buf,BUFLEN,stdin);
printf("Attempting to write to socket %d: ",sockfd);
fflush(stdout);
//send to here
if (sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, slen)==-1)
{
err("sendto()");
}
}
}
close(sockfd);
return 0;

最佳答案

我突然想到了几个问题。首先,每次您收到消息时,它都会将其视为新客户。您需要扫描整个数组以查看源地址是否已经存在,而不是仅仅增加消息的客户端变量。二、sizeof(clients)将返回一个静态值(可能是 4),具体取决于 int 在您的机器上占用的字节数。该循环应该是 for( int i = 0; i < clients; i++ ) .

您还有一个名为 num_clients 的变量未使用。它应该是为了什么而存在的,可能会引起一些困惑吗?

最后,不要到处使用魔法值 10,而是使用 #define MAX_CONNECTIONS 10然后用 MAX_CONNECTIONS 替换所有这些数字。以后阅读和更改要容易得多。

关于c - UDP 多客户端聊天服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16472157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com