Menu

#55 Assign native socket problem

open
nobody
None
5
2011-02-15
2011-02-15
Anonymous
No

Hi

I'm trying to duplicate accepted/opened socket and pass them to other process. It works fine under linux. But under Windows I can't assign this native socket to boost::asio::ip::tcp::socket. When I try to assign opened native socket to asio socket object s.assign(boost::asio::ip::tcp::v4(), socketDup) throws me exception: "The parameter is incorrect". Even if I try to duplicate socket and use them in the same process. I still got this exception.

The line inside boost throws this exception: boost_1_45_0\boost\asio\detail\impl\win_iocp_io_service.ipp:
boost::system::error_code win_iocp_io_service::register_handle(
HANDLE handle, boost::system::error_code& ec)
{
if (::CreateIoCompletionPort(handle, iocp_.handle, 0, 0) == 0) //this function failed
{
DWORD last_error = ::GetLastError();
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
}
else
{
ec = boost::system::error_code();
}
return ec;
}

I'm using: VC++ 2008 / Win7

What am I doing wrong? How can I resolve this problem? Thanks for help.

Example code:
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));

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

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

#ifdef _WIN32
WSAPROTOCOL_INFO pi;
WSADuplicateSocket(socket.native(), get_current_process_id(), &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());
#endif

try
{
tcp::socket s(io_service);
s.assign(boost::asio::ip::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"));
}
catch(std::exception &e)
{
std::cerr << e.what() << std::endl; //"The parameter is incorrect" exception
}

Discussion


Log in to post a comment.