日期:2014-05-16 浏览次数:21045 次
pid = fork();
if( pid < 0 )
{
perror( "fork error.\n" );
exit( EXIT_FAILURE );
}
if( pid > 0) //父进程实现动态刷新在线好友,从服务器端接收
{
while( 1 )
{
select(sockfd + 1, &r_set, NULL, NULL, NULL); //这里应该判断select的返回值,我没有写
if (FD_ISSET(sockfd, &r_set))
{
//可读
nbytes = recv( sockfd, online_buff, sizeof(online_buff), 0 );
if( nbytes < 0 )
{
fprintf( stderr, "接收数据错误..%s\n", strerror(errno) );
//continue;
}
else
{
printf( "%s\n", online_buff );
if( nbytes != nbytes_temp )
{
nbytes_temp = nbytes;
online_buff[nbytes] = '\0';
printf( "%s\n", online_buff );
}
}
}
//sleep( 5 );
}/*end while 1*/
waitpid( pid, NULL, 0 );
}
if( pid == 0 )
{
printf( "子进程:\n" );
scanf( "%d", &choice2 );
printf( "%d\n", choice2 );
}