Re: [Boost-bugs] [Boost C++ Libraries] #5157: Assign native socket problem

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #5157: Assign native socket problem
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-03-17 17:05:58

#5157: Assign native socket problem
  Reporter: a.pacek@… | Owner: chris_kohlhoff
      Type: Bugs | Status: new
 Milestone: | Component: asio
   Version: Boost 1.45.0 | Severity: Problem
Resolution: | Keywords: native socket assign

Comment (by Ole Jak (aka Blender)):

 Hello my friends.

 I loved this peace of code. I decided to help us all opensource
 programmers a bit and try to solve this problem.

 I fixed code a bit and made it compilable.

 I received exception described.

 I [
 accepted-tcp-connection-from-one-opened-socket-to-another addressed this
 question] to community of Grate [ StackOverflow]

 [ Ben Voigt] *(it seems a
 member of msmvps)[
 another/5329413#5329413 provided a solution] to this problem which was
 tested and that works.

 here is final code I compiled and tested:

 #include <iostream>

 #ifdef _WIN32

 #include <boost/asio.hpp>

 #ifdef _WIN32
 #include "Windows.h"

 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(),

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

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

 #ifdef _WIN32
         WSADuplicateSocket(socket.native(), GetCurrentProcessId(), &pi);
         SOCKET socketDup = WSASocket(pi.iAddressFamily/*AF_INET*/,
                 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);
         int socketDup = dup(socket.native()); // tested on Linux, works!

                 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"

Ticket URL: <>
Boost C++ Libraries <>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC