简体   繁体   English

如何在C套接字实现中创建fork?

[英]how can i create a fork in c socket implementation?

My mini project is on implementing ac socket program where multiple clients send files to two or three servers. 我的小型项目是实现ac套接字程序,其中多个客户端将文件发送到两个或三个服务器。 i have implemented these. 我已经实现了这些。 but for handling the client request i need to create a child process is it? 但是为了处理客户请求,我需要创建一个子进程吗? how can i do that . 我怎样才能做到这一点 。 like have to handle the request separately.please anybody guide me to do it. 就像必须单独处理该请求。请任何人指导我去做。

The usual multi-process server would typically look something like this (pseudo-code): 通常的多进程服务器通常看起来像这样(伪代码):

passive_socket = create_listening_socket()

for (;;)
{
    new_socket = accept(passive_socket)
    if (fork() == 0)
    {
        /* In child */
        read_from_socket(new_socket)
        write_to_socket(new_socket)
        /* Or do any other processing needed */

        exit(0);
    }
    else
    {
        /* In parent */
        close(new_socket)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM