使用fork的str_cli函数

void str_cli(FILE *fp, int sockfd)
{
    pid_t    pid;
    char     sendline[MAXLINE], recvline[MAXLINE];

    if ( (pid = fork()) == 0) {      /* child: server -> stdout */
        while (read(sockfd, recvline, MAXLINE) > 0)
            fputs(recvline, stdout);

        kill(getppid(), SIGTERM);    /* in case parent still running */
        exit(0);
    }

    /* parent: stdin -> server */
    while (fgets(sendline, MAXLINE, fp) != NULL)
        writen(sockfd, sendline, strlen(sendline));

    shutdown(sockfd, SHUT_WR);       /* EOF on stdin, send FIN */
    pause();
    return;
}
原文地址:https://www.cnblogs.com/soldierback/p/10776530.html