因此,每个客户端连接都应在新的子进程中提供。
现在,我有一个函数generate_client(),该函数创建一个客户端并为其提供一个随机ID号(返回给客户端)。
client_t generate_client()
{
client_t *client = malloc(sizeof(client_t));
client->clientID = randomClientIdGenerator(); < ----
client->entryIndexConstant = 0;
client->messageQueueIndex = 0;
client->readMsg = 0;
client->totalMessageSent = 0;
client->unReadMsg = 0;
client->status = CLIENT_INACTIVE;
return *client;
}
int randomClientIdGenerator()
{
int num = rand() % MAX_CLIENTS;
return num;
}
问题:对于使用fork()的每个连接,将从父级复制子进程,如您在实现中看到的,复制具有相同客户端ID的 client对象下方到子进程(至少我认为这是正在发生的事情)。
例如:使用终端1连接到服务器会生成客户端ID 83,而终端2的连接也会发送ID 83。
/* bind the socket to the end point */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
/* start listnening */
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}
while (1)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
perror("accept.");
printf("\n...error: accept new_fd failed\n");
// continue;
}
printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));
if (!fork())
{ /* this is the child process */
printf("\n-----------------------CHILD START ----------\n");
printf("\n child process id is %d. parent id is: %d\n", getpid(), getppid());
/* ***Server-Client Connected*** */
client_t client = generate_client();
printf("\n =>client id %d STATUS: %d\n", client.clientID, client.status);
if (client.clientID < -1)
{
perror("SERVER: failed to create client object (Max. 100 clients allowed)");
printf("SERVER: failed to create client object (Max. 100 clients allowed) \n");
exit(1);
// send response to client Cant accept connection
}
// Send: Welcome Message. ------------> SAME id of 83 is given to child process!!!
if (send(new_fd, &client.clientID, sizeof(int), 0) == -1)
{
perror("send");
printf("Error: Welcome message not sent to client \n");
}
}
}
我认为问题是因为fork()中的client_t client = generate_client();
..生成了从父进程复制过来的客户端,我该如何在每个进程中重新调用它?
答案 0 :(得分:0)
这似乎与几个小时前发布的问题相同: Trying to fork() after new client connection to server [Socket Programming C]
简短答案:
“ rand”函数使用隐藏的“ state”来生成下一个随机数。由于父母从未使用过兰德,所以每个分叉的孩子将获得相同的状态,并将生成相同的随机数序列。
一些可能的解决方法: