[asio] Cloning a WSA socket (windows)

Hi, I am trying to clone a socket from process to another, mostly for having one process per client in a network server. Some legacy stuff makes this the only design possible, so the threaded approach is simply not feasible... Anyway, when I'm trying to create a boost socket from a cloned WSA socket I get an exception. class boost::exception_detail::clone_impl< struct boost::exception_detail::error_info_injector< class boost::system::system_error >
: The parameter is incorrect
This same happens for this example program, using boost 1.37 Does anyone have an idea why this does not work? #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0501 #endif #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include <windows.h> #include <winsock2.h> #pragma comment(lib, "ws2_32") #include <boost/asio.hpp> namespace ip = boost::asio::ip; int main_impl(int argc, char* argv[]) { boost::asio::io_service ios; ip::tcp::socket s(ios); ip::tcp::acceptor a(ios, ip::tcp::endpoint(ip::tcp::v4(), 1777)); a.accept(s); WSAPROTOCOL_INFO info; if (WSADuplicateSocket(s.native(), GetCurrentProcessId(), &info)) { std::cerr << "WSAError: " << WSAGetLastError() << std::endl; return EXIT_FAILURE; } // pass info over a pipe to the client... SOCKET copy(WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO , FROM_PROTOCOL_INFO, &info, NULL, NULL)); ip::tcp::socket scopy(ios, ip::tcp::v4(), copy); return EXIT_SUCCESS; } int main(int argc, char* argv[]) { try { return main_impl(argc, argv); } catch(std::exception const& ex) { std::cerr << typeid(ex).name() << ":" << ex.what() << std::endl; return EXIT_FAILURE; } } -- Eld på åren og sol på eng gjer mannen fegen og fjåg. [Jøtul] <demo> 2009 Tore Halvorsen || +052 0553034554

SOCKET copy(WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO , FROM_PROTOCOL_INFO, &info, NULL, NULL));
ip::tcp::socket scopy(ios, ip::tcp::v4(), copy);
For debug purposes, did you check what the value of "copy" is? Did you try to perform a simple winsock i/o operation on it? I.e., are you sure that you've got valid tcp socket descriptor?

On Tue, Feb 3, 2009 at 6:17 PM, Igor R <boost.lists@gmail.com> wrote:
For debug purposes, did you check what the value of "copy" is? Did you try to perform a simple winsock i/o operation on it? I.e., are you sure that you've got valid tcp socket descriptor?
Good call. While copy != INVALID_SOCKET, but I get a "The system detected an invalid pointer address in attempting to use a pointer argument in a call." when I try to do a WSARecv. I should probably try a windows forum instead. -- Eld på åren og sol på eng gjer mannen fegen og fjåg. [Jøtul] <demo> 2009 Tore Halvorsen || +052 0553034554

While copy != INVALID_SOCKET, but I get a "The system detected an invalid pointer address in attempting to use a pointer argument in a call." when I try to do a WSARecv. I should probably try a windows forum instead.
I didn't try to debug your code, but at a glance, it seems that when duplicating socket, you pass current process id instead of *target* process id.

On Tue, Feb 3, 2009 at 8:03 PM, Igor R <boost.lists@gmail.com> wrote:
I didn't try to debug your code, but at a glance, it seems that when duplicating socket, you pass current process id instead of *target* process id.
Right now I'm just trying to do it in a single program. -- Eld på åren og sol på eng gjer mannen fegen og fjåg. [Jøtul] <demo> 2009 Tore Halvorsen || +052 0553034554
participants (2)
-
Igor R
-
Tore Halvorsen