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 [http://stackoverflow.com/questions/5326564/c-boost-asio-passing-
 accepted-tcp-connection-from-one-opened-socket-to-another addressed this
 question] to community of Grate [http://stackoverflow.com/ StackOverflow]
 programmers.

 [http://stackoverflow.com/users/103167/ben-voigt Ben Voigt] *(it seems a
 member of msmvps)[http://stackoverflow.com/questions/5326564/c-boost-asio-
 passing-accepted-tcp-connection-from-one-opened-socket-to-
 another/5329413#5329413 provided a solution] to this problem which was
 tested and that works.

 here is final code I compiled and tested:
 {{{
 #!cpp

 #include <iostream>

 #ifdef _WIN32
 #define BOOST_ASIO_DISABLE_IOCP 1
 #endif

 #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();
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5157#comment:1>
Boost C++ Libraries <http://www.boost.org/>
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