将套接字从父进程传递给子进程 [英] Pass socket from parent to child process

查看:56
本文介绍了将套接字从父进程传递给子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有两个程序:父项和子项。



父项:从客户端有一个套接字等待连接。当Parent接受连接时,它会生成Child进程并将套接字传递给Child。



My project has two programs: Parent and Child.

In Parent: has one socket waiting connection from client. When Parent accepts connection, it generates Child process and passes socket to Child.

SOCKET newSock = accept(listenSock, 0, 0);
char cmd[1024];
sprintf(cmd, "%s %d", "Child.exe", newSock);
result = CreateProcess( NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInformation);



客户端和子进程传输数据成功。



但是当我搜索谷歌时,有些人写道:必须调用函数WSADuplicateSocket(),然后将套接字传递给子进程。 />


请求帮我展示两种方式之间的差异?

如果我不调用WSADuplicateSocket()我的程序是否有任何错误?



谢谢!


Client and Child process transfer data successfully.

But when I searches Google and some people wrote that : must call function WSADuplicateSocket(), after that pass socket to child process.

Pleas help me show the different between the two ways?
If i don't call WSADuplicateSocket() whether my program has any error or not?

Thanks!

推荐答案

问题/错误是您的2个进程共享同一个套接字。如果其中任何一个正在关闭套接字,则其他进程存在巨大问题。



我的解决方案是通过仅在子进程中打开套接字来清除所有权,或者按照建议使用API​​。
the problem/error is that your 2 processes are sharing the same socket. If any of them is closing the socket the other process has a huge problem.

My solution wold be to "clear" the ownership by opening the socket ONLY in the child process or use the API as recommended.


这篇关于将套接字从父进程传递给子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆