I tried to learn how to reassign a received connection using Boost.ASIO and the Windows API. found this code example , added to it, includes and uses namespaces, so now it compiles - just copy and paste, and here you go ... "The parameter is incorrect" The exception is in the same place as the poster with the code, if it = (So, here is the 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(); }
In general, the code follows this post , and I really don’t see what is wrong and how to fix it.
And this follows how we will transfer the received TCP connection from one process to another ( here)
Maybe this example “Inheritance of stickers on different Windows platforms” may help , but I don’t see how to do it.
Can someone help me find a possible workaround for this problem?
Update: Just proven Linux code - works great, no errors.
So what is this with the version of Windows?