C ++ Boost.Asio的:从一个打开的套接字通过接受的TCP连接到另一台使用Windows的API(而在Linux API的作品)? [英] C++ Boost.ASIO: passing accepted TCP connection from one opened socket to another using Windows APIs ( while works with Linux APIs)?

查看:396
本文介绍了C ++ Boost.Asio的:从一个打开的套接字通过接受的TCP连接到另一台使用Windows的API(而在Linux API的作品)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想了解如何重新使用Boost.Asio的和Windows API的分配接受的连接。发现这个code样品的加入,它包括和使用命名空间,所以现在这是编译 - 只需复制并粘贴在这里你去...的参数不正确异常在同一地点code海报了吧=(因此,这里是code:

I was trying to learn how to re assign accepted connection using Boost.ASIO and Windows API's. found this code sample added to it includes and use of namespaces so now it is compilable - just copy and paste and here you go... "The parameter is incorrect" exception at the same place code poster had it=( So here is code:

#include <iostream>
#include <boost/asio.hpp>

#ifdef _WIN32
#include "Windows.h"
#endif

using namespace boost::asio::ip;
using namespace std;

int main(){
int m_nPort = 12345;
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));

cout << "Waiting for connection..." << endl;

tcp::socket socket(io_service);
acceptor.accept(socket);
cout << "connection accepted" << endl;

#ifdef _WIN32
WSAPROTOCOL_INFO pi;
WSADuplicateSocket(socket.native(), GetCurrentProcessId(), &pi);
SOCKET socketDup = WSASocket(pi.iAddressFamily/*AF_INET*/, pi.iSocketType/*SOCK_STREAM*/,
                             pi.iProtocol/*IPPROTO_TCP*/, &pi, 0, 0);
char sText[] = "I can use my duplicated socket via WinApi!\r\n";
int nRet = send(socketDup, sText, strlen(sText), 0);
#else
//linux
 int socketDup = dup(socket.native()); // tested on Linux, works!
#endif

try
{
    tcp::socket s(io_service);
    s.assign(tcp::v4(), socketDup); //this throws exception under Windows
    //I can't use my socket via boost lib
    s.send(boost::asio::buffer("Not work\r\n"));
    cout << "We do not get here!=(" << endl;
}
catch(exception &e)
{
    cerr << e.what() << endl; //"The parameter is incorrect" exception
}
cin.get();
}

在一般<一href=\"http://stackoverflow.com/questions/2966323/boostasio-and-socket-ownership/2968774#2968774\">$c$c遵循这个帖子,我居然看不出有什么不对既不如何解决它。

In general code follows this post and I actually do not see what is wrong neither how to fix it.

和它遵循的方式,我们怎么会通过接受从一个进程到另一个TCP连接(<一href=\"http://stackoverflow.com/questions/5312773/c-application-is-it-possible-to-pass-accepted-tcp-connection-from-one-process/5312969#5312969\">described这里)

And it follows way how we would pass accepted TCP connection from one process to another (described here)

可能这个例子中在不同的Windows平台上插座继承可以帮助,但我看不出如何。

May be this "Socket inheritance on different Windows platforms" example could help but I do not see how.

任何一个可以请你帮我找到任何可能的解决方法这个问题?

Can any one please help me to find any possible workaround that problem?

更新:
在Linux上刚刚测试code - 作品完美,没有任何错误

Update: Just tested code on Linux - works perfectly, no errors.

那么,与Windows的版本吗?

So what is it with windows version?

推荐答案

<击>
尝试使用连接到 WSASocket 文档code片断:

socketDup = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, &pi, 0, WSA_FLAG_OVERLAPPED);

好吧,我通过升压code追根溯源,它失败尝试与I / O完成端口插座关联。这是因为套接字已被关联到完成端口。

Ok, I traced through the Boost code, and it fails trying to associate the socket with the I/O completion port. That's because the socket is already associated to a completion port.

MSDN文档说:

最好是不要通过使用处理继承或将DuplicateHandle函数的调用共享与I / O完成端口相关联的文件句柄。这种重复的手柄执行的操作生成完成通知。仔细考虑建议。

It is best not to share a file handle associated with an I/O completion port by using either handle inheritance or a call to the DuplicateHandle function. Operations performed with such duplicate handles generate completion notifications. Careful consideration is advised.

明知IOCP涉及到这个问题,我设置(包括任何boost头文件之前)

Knowing that IOCP is related to the problem, I set (before including any boost headers)

#define BOOST_ASIO_DISABLE_IOCP 1

和一切工作正常。

本地输出:

Waiting for connection...
connection accepted
We do not get here!=(

远程输出:

I can use my duplicated socket via WinApi!
Not work

这篇关于C ++ Boost.Asio的:从一个打开的套接字通过接受的TCP连接到另一台使用Windows的API(而在Linux API的作品)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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