linux 我的accept函数为什么不阻塞啊,请教
intmain(){intsock_main,sock_sub;structsockaddr_incli_addr,ser_addr;intaddr_size;socke...
int main()
{
int sock_main , sock_sub ;
struct sockaddr_in cli_addr, ser_addr;
int addr_size ;
socket(AF_INET, SOCK_STREAM, 0);
ser_addr.sin_family = AF_INET;
// ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ser_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ser_addr.sin_port = htons(atoi("6789"));
bind(sock_main, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr));
listen(sock_main, 10);
int fork_num ;
int subpid ;
signal(SIGCHLD, handle_zombie);
while(1)
{
addr_size = sizeof(struct sockaddr_in);
sock_sub = accept(sock_main, (struct sockaddr *)(&cli_addr), &addr_size);
fork_num = fork();
if(fork_num < 0)
{
printf("fork failure !!!");
exit(0);
}
else if(fork_num == 0)
{
close(sock_main);
child_process_handdle(sock_sub);
exit(0);
}
close(sock_sub);
}
sleep(100);
return 0;
}
void child_process_handdle(int sock_fd)
{
char recv[60] ;
memset(recv, 0X00, 60);
read(sock_fd, recv, 50);
printf("server is : %s\n", recv);
}
void handle_zombie(int signo)
{
int pid = 0 ;
int state = 0;
while( (pid = waitpid(-1, &state, WNOHANG)) > 0)
printf("clean zombie pid : %d\n", pid);
return ;
} 展开
{
int sock_main , sock_sub ;
struct sockaddr_in cli_addr, ser_addr;
int addr_size ;
socket(AF_INET, SOCK_STREAM, 0);
ser_addr.sin_family = AF_INET;
// ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ser_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ser_addr.sin_port = htons(atoi("6789"));
bind(sock_main, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr));
listen(sock_main, 10);
int fork_num ;
int subpid ;
signal(SIGCHLD, handle_zombie);
while(1)
{
addr_size = sizeof(struct sockaddr_in);
sock_sub = accept(sock_main, (struct sockaddr *)(&cli_addr), &addr_size);
fork_num = fork();
if(fork_num < 0)
{
printf("fork failure !!!");
exit(0);
}
else if(fork_num == 0)
{
close(sock_main);
child_process_handdle(sock_sub);
exit(0);
}
close(sock_sub);
}
sleep(100);
return 0;
}
void child_process_handdle(int sock_fd)
{
char recv[60] ;
memset(recv, 0X00, 60);
read(sock_fd, recv, 50);
printf("server is : %s\n", recv);
}
void handle_zombie(int signo)
{
int pid = 0 ;
int state = 0;
while( (pid = waitpid(-1, &state, WNOHANG)) > 0)
printf("clean zombie pid : %d\n", pid);
return ;
} 展开
1个回答
TableDI
2024-07-18 广告
2024-07-18 广告
VLOOKUP是Excel中用于垂直查找的函数,其基本用法包括四个参数:1. 查找值:即在数据表首列中需要搜索的值。2. 数据表:包含查找值的单元格区域或数组。3. 返回值所在列数:指定返回查询区域中第几列的值。4. 查找方式:选择精确匹配...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询