>g++ -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib -lwsock32 -lws2_32 -lboost_system-mgw81-mt-x64-1_69 -o print.dll

g++ -static -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib -lwsock32 -lws2_32 -lboost_system-mgw81-mt-x64-1_69

加一句免第三方dll

以下源码编译成功

#ifndef __MAIN_H__
#define __MAIN_H__ #ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif // Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0500 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif #ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target Windows 2000 or later.
#endif #ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif #ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0400 // Change this to the appropriate value to target IE 5.0 or later.
#endif #define WIN32_LEAN_AND_MEAN // 在#include<windows.h>前定义
#include <windows.h> /* To use this exported function of dll, include this header
* in your project.
*/ #ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif #ifdef __cplusplus
extern "C"
{
#endif DLL_EXPORT int Asio_init(int iport); DLL_EXPORT int Asio_SvrRun(); DLL_EXPORT int Asio_Uninit(int iport); DLL_EXPORT int Asio_SetCallback(int ikind,int ifun); DLL_EXPORT int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_closesocket(int* isocket); DLL_EXPORT int Asio_Client_init(int* ipobj); DLL_EXPORT int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata); DLL_EXPORT int Asio_Client_DisConn(int* isocket); DLL_EXPORT int Asio_Client_Free(int* isocket); DLL_EXPORT int Asio_Client_senddata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_readdata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_Asreaddata(int* isocket,char * ibuff,int ilen,int iuserdata); #ifdef __cplusplus
}
#endif #endif // __MAIN_H__
#define DLL _stdcall

#include "main.h"

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <windows.h>
#include <List> using boost::asio::ip::tcp; bool iscreate=false;
bool isfinished=false;
int gport;
class session;
class server;
boost::asio::io_service io_service;
boost::asio::deadline_timer* lt;
server* s; std::list<session*> deadlist;
std::list<session*>::iterator deadlist_Iter,deadlist_Iter2;
CRITICAL_SECTION criCounter; //回调函数设置
typedef void (CALLBACK* Cb_OnNewConn)(int ipsocket,const char* ipeerip,int ipeerport,int* iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_OnDisConn)(int iuserdata);
typedef void (CALLBACK* Cb_Onreaddata)(char* ibuff,int ilen,int iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_Onwritedata)(int iuserdata,int iuser2); Cb_OnNewConn Callback_NewConn;
Cb_OnDisConn Callback_DisConn;
Cb_Onreaddata Callback_readdata;
Cb_Onwritedata Callback_writedata; class session
{
public:
int readlen;
int userdata;
int deadtime;
enum { max_length = };
char data_[max_length]; session(boost::asio::io_service& io_service)
: socket_(io_service)
{
//data_[0]='a';
//data_[1]='b';
//data_[2]='1';
readlen=;
userdata=;
}
~session()
{
socket_.close();
// delete socket_;
} tcp::socket& socket()
{
return socket_;
} void start()
{
freopen("start.txt", "w+", stdout);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
freopen("handle_read.txt", "w+", stdout);
// std::cout << "read some len:" << bytes_transferred << "\n" ;
if (!error)
{ try
{
if(Callback_readdata!=NULL)
{
Callback_readdata(data_,bytes_transferred, userdata,&readlen); }
}
catch(...)
{
} if(userdata!=)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
}
delete this;
}
}
else
{ socket_.close();
// cout << "socket error " << error << "\n" ;
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} void handle_write(const boost::system::error_code& error,int iuser,int iuser2)
{
freopen("handle_write.txt", "w+", stdout);
if(Callback_writedata!=NULL)
{
// std::cout << "write ok " << iuser << "\n" ;
Callback_writedata(iuser,iuser2);
}
if (!error)
{ }
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} private:
tcp::socket socket_; }; class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
freopen("server.txt", "w+", stdout);
//创建临界区
InitializeCriticalSection(&criCounter); session* new_session = new session(io_service_); acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error
));
}
~server()
{
//删除临界区
DeleteCriticalSection(&criCounter); }
void handle_accept(session* new_session,
const boost::system::error_code& error
)
{
int lbuf;
// cout << "some one conned ok \n" ;
if (!error)
{
if(Callback_NewConn!=NULL)
{ lbuf=(int &)(*new_session);
// std::cout <<"socket addr:"<<lbuf << "\n" ;
// cout <<"IP:"<<new_session->socket().remote_endpoint().address().to_string()<< " port:"<<new_session->socket().remote_endpoint().port()<<" \n" ;
/* std::cout <<"userdata:"<<&new_session->userdata<< "\n" ;
std::cout <<"readlen:"<<&new_session->readlen<< "\n" ; */ Callback_NewConn(lbuf,new_session->socket().remote_endpoint().address().to_string().c_str(),
new_session->socket().remote_endpoint().port(),
&new_session->userdata,
&new_session->readlen); new_session->start();
new_session = new session(io_service_);
// //删除超时的sesson
// int i;
// for (deadlist_Iter=deadlist.begin(); deadlist_Iter !=deadlist.end(); ++deadlist_Iter)
//{
// i= (*deadlist_Iter)->deadtime;
// i=GetTickCount() -i;
//
// if ( i > 3000)
// {
// //用pos2来保存pos指针
// deadlist_Iter2=deadlist_Iter;
// deadlist_Iter++;
// deadlist.erase(deadlist_Iter2);
// delete (*deadlist_Iter);
// }
//} }
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
/* session* lp=(session*)(lbuf);
boost::asio::async_write(lp->socket() ,
boost::asio::buffer(new_session->data_, 4),
boost::bind(&session::handle_write,
new_session,
boost::asio::placeholders::error,
1
));*/ }
else
{
delete new_session;
}
} private: boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; void ontimer(const boost::system::error_code& /*e*/)
{
freopen("ontimer.txt", "w+", stdout);
if(isfinished==false)
{
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
} //typedef int (CALLBACK* fPlayerlst)(char* ibuff,int i);
//typedef int (CALLBACK* frecordlst)(char* ibuff,int i); extern __declspec(dllexport) int Asio_init(int iport)
{
freopen("Asio_init.txt", "w+", stdout);
if(iscreate==false)
{
/* AllocConsole();
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
cout << "init \n" ;*/
gport=iport;
iscreate=true;
if(iport==)
//server s(io_service, iport);
//else
{
lt=new boost::asio::deadline_timer(io_service,boost::posix_time::seconds());
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
return ;
}
return ;
//
//if (access("plog", 0) == 0)
//{ //}
} extern __declspec(dllexport) int Asio_SvrRun()
{
freopen("Asio_SvrRun.txt", "w+", stdout);
if(gport!=)
{
s=new server(io_service,gport);
}
try
{
io_service.run();
}
catch(...)
{
}
delete s; if(gport==)
{
Sleep();
delete lt;
} return ;
} extern __declspec(dllexport) int Asio_Uninit(int iport)
{
freopen("Asio_Uninit.txt", "w+", stdout);
if(gport==)
{
lt->cancel();
}
io_service.stop();
isfinished=true;
return ;
} extern __declspec(dllexport) int Asio_SetCallback(int ikind,int ifun)
{
freopen("Asio_SetCallback.txt", "w+", stdout);
if(ikind==)
Callback_NewConn=(Cb_OnNewConn)ifun;
else if(ikind==)
Callback_DisConn=(Cb_OnDisConn)ifun;
else if(ikind==)
Callback_readdata=(Cb_Onreaddata)ifun;
else if(ikind==)
Callback_writedata=(Cb_Onwritedata)ifun;
return ;
} extern __declspec(dllexport) int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen)
{
freopen("Asio_senddata.txt", "w+", stdout);
session* ltmp=(session*)isocket; boost::asio::async_write(ltmp->socket(),
boost::asio::buffer(ibuff, ilen),
boost::bind(&session::handle_write,
ltmp,
boost::asio::placeholders::error,
ltmp->userdata,
ikind
));
return ;
} extern __declspec(dllexport) int Asio_closesocket(int* isocket)
{
freopen("Asio_closesocket.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close(); }
catch(...)
{}
//EnterCriticalSection(&criCounter);
//deadlist.push_back(isocket);
//LeaveCriticalSection(&criCounter); //delete isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_init(int* ipobj)
{
freopen("Asio_Client_init.txt", "w+", stdout);
session* new_session;
new_session=new session(io_service);
*ipobj=(int &)(*new_session);
return ;
} extern __declspec(dllexport) int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata)
{
freopen("Asio_Client_conntosvr.txt", "w+", stdout);
//域名解析,只将域名解析为ipv4地址
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string(iip), iport);
//只连接一次
boost::system::error_code ec;
session * lsock=(session *)isocket;
lsock->userdata=iuserdata;
lsock->socket().connect(ep,ec);
if (ec)
return ;
else
{
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} } extern __declspec(dllexport) int Asio_Client_DisConn(int * isocket)
{
freopen("Asio_Client_DisConn.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close();
}
catch(...)
{
}
return ;
} extern __declspec(dllexport) int Asio_Client_Free(int * isocket)
{
freopen("Asio_Client_Free.txt", "w+", stdout);
delete (session*)isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_senddata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_senddata.txt", "w+", stdout);
session* lsock=(session*)isocket;
boost::system::error_code ignored_error;
boost::asio::write(lsock->socket(), boost::asio::buffer(ibuff,ilen),
boost::asio::transfer_all(), ignored_error);
if(ignored_error)
return ;
else
return ;
} extern __declspec(dllexport) int Asio_Client_readdata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_readdata.txt", "w+", stdout);
boost::system::error_code ignored_error;
session* lsock=(session*)isocket;
ilen=lsock->socket().read_some(boost::asio::buffer(ibuff,ilen), ignored_error);
if(ignored_error)
return ;
else
return ilen;
} extern __declspec(dllexport) int Asio_Client_Asreaddata(int * isocket,char * ibuff,int ilen,int iuserdata)
{
freopen("Asio_Client_Asreaddata.txt", "w+", stdout); session * lsock=(session *)isocket; lsock->userdata=iuserdata;
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
}

MSYS2 MinGW 64-bit

./b2 install --prefix=/usr

./b2 -a cxxflags="-DVC_EXTRALEAN -DWINVER=0x0400 -D_WIN32_WINNT=0x0501 -D_WIN32_WINDOWS=0x0410 -D_WIN32_IE=0x0400"  toolset=gcc --这条不安装费事

./b2 -a cxxflags="-DVC_EXTRALEAN -DWINVER=0x0400 -D_WIN32_WINNT=0x0501 -D_WIN32_WINDOWS=0x0410 -D_WIN32_IE=0x0400"  toolset=gcc install --prefix=(D:\GCC\boost) (/usr)

./bootstrap.sh

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <list>
#ifdef WIN32
#include "windows.h"
#include <List>
#endif using boost::asio::ip::tcp; bool iscreate=false;
bool isfinished=false;
int gport;
class session;
class server;
boost::asio::io_service io_service;
boost::asio::deadline_timer* lt;
server* s; std::list<session*> deadlist;
std::list<session*>::iterator deadlist_Iter,deadlist_Iter2;
//CRITICAL_SECTION criCounter; //回调函数设置
typedef void (*Cb_OnNewConn)(int ipsocket,const char* ipeerip,int ipeerport,int* iuserdata,int* ireadlen);
typedef void (*Cb_OnDisConn)(int iuserdata);
typedef void (*Cb_Onreaddata)(char* ibuff,int ilen,int iuserdata,int* ireadlen);
typedef void (*Cb_Onwritedata)(int iuserdata,int iuser2); Cb_OnNewConn Callback_NewConn;
Cb_OnDisConn Callback_DisConn;
Cb_Onreaddata Callback_readdata;
Cb_Onwritedata Callback_writedata; class session
{
public:
int readlen;
int userdata;
int deadtime;
enum { max_length = };
char data_[max_length]; session(boost::asio::io_service& io_service)
: socket_(io_service)
{
//data_[0]='a';
//data_[1]='b';
//data_[2]='1';
readlen=;
userdata=;
}
~session()
{
socket_.close();
// delete socket_;
} tcp::socket& socket()
{
return socket_;
} void start()
{
freopen("start.txt", "w+", stdout);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
freopen("handle_read.txt", "w+", stdout);
// std::cout << "read some len:" << bytes_transferred << "\n" ;
if (!error)
{ try
{
if(Callback_readdata!=NULL)
{
Callback_readdata(data_,bytes_transferred, userdata,&readlen); }
}
catch(...)
{
} if(userdata!=)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
}
delete this;
}
}
else
{ socket_.close();
// cout << "socket error " << error << "\n" ;
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} void handle_write(const boost::system::error_code& error,int iuser,int iuser2)
{
freopen("handle_write.txt", "w+", stdout);
if(Callback_writedata!=NULL)
{
// std::cout << "write ok " << iuser << "\n" ;
Callback_writedata(iuser,iuser2);
}
if (!error)
{ }
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} private:
tcp::socket socket_; }; class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
freopen("server.txt", "w+", stdout);
//创建临界区
// InitializeCriticalSection(&criCounter); session* new_session = new session(io_service_); acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error
));
}
~server()
{
//删除临界区
// DeleteCriticalSection(&criCounter); }
void handle_accept(session* new_session,
const boost::system::error_code& error
)
{
int lbuf;
// cout << "some one conned ok \n" ;
if (!error)
{
if(Callback_NewConn!=NULL)
{ lbuf=(int &)(*new_session);
// std::cout <<"socket addr:"<<lbuf << "\n" ;
// cout <<"IP:"<<new_session->socket().remote_endpoint().address().to_string()<< " port:"<<new_session->socket().remote_endpoint().port()<<" \n" ;
/* std::cout <<"userdata:"<<&new_session->userdata<< "\n" ;
std::cout <<"readlen:"<<&new_session->readlen<< "\n" ; */ Callback_NewConn(lbuf,new_session->socket().remote_endpoint().address().to_string().c_str(),
new_session->socket().remote_endpoint().port(),
&new_session->userdata,
&new_session->readlen); new_session->start();
new_session = new session(io_service_);
// //删除超时的sesson
// int i;
// for (deadlist_Iter=deadlist.begin(); deadlist_Iter !=deadlist.end(); ++deadlist_Iter)
//{
// i= (*deadlist_Iter)->deadtime;
// i=GetTickCount() -i;
//
// if ( i > 3000)
// {
// //用pos2来保存pos指针
// deadlist_Iter2=deadlist_Iter;
// deadlist_Iter++;
// deadlist.erase(deadlist_Iter2);
// delete (*deadlist_Iter);
// }
//} }
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
/* session* lp=(session*)(lbuf);
boost::asio::async_write(lp->socket() ,
boost::asio::buffer(new_session->data_, 4),
boost::bind(&session::handle_write,
new_session,
boost::asio::placeholders::error,
));*/ }
else
{
delete new_session;
}
} private: boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; void ontimer(const boost::system::error_code& /*e*/)
{
freopen("ontimer.txt", "w+", stdout);
if(isfinished==false)
{
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
} unsigned long GetTickCount()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts);
return (ts.tv_sec * + ts.tv_nsec / );
} //typedef int (CALLBACK* fPlayerlst)(char* ibuff,int i);
//typedef int (CALLBACK* frecordlst)(char* ibuff,int i); int Asio_init(int iport)
{
freopen("Asio_init.txt", "w+", stdout);
if(iscreate==false)
{
/* AllocConsole();
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
cout << "init \n" ;*/
gport=iport;
iscreate=true;
if(iport==)
//server s(io_service, iport);
//else
{
lt=new boost::asio::deadline_timer(io_service,boost::posix_time::seconds());
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
return ;
}
return ;
//
//if (access("plog", 0) == 0)
//{ //}
} int Asio_SvrRun()
{
freopen("Asio_SvrRun.txt", "w+", stdout);
if(gport!=)
{
s=new server(io_service,gport);
}
try
{
io_service.run();
}
catch(...)
{
}
delete s; if(gport==)
{
sleep();
delete lt;
} return ;
} int Asio_Uninit(int iport)
{
freopen("Asio_Uninit.txt", "w+", stdout);
if(gport==)
{
lt->cancel();
}
io_service.stop();
isfinished=true;
return ;
} int Asio_SetCallback(int ikind,long long ifun)
{
freopen("Asio_SetCallback.txt", "w+", stdout);
if(ikind==)
Callback_NewConn=(Cb_OnNewConn)ifun;
else if(ikind==)
Callback_DisConn=(Cb_OnDisConn)ifun;
else if(ikind==)
Callback_readdata=(Cb_Onreaddata)ifun;
else if(ikind==)
Callback_writedata=(Cb_Onwritedata)ifun;
return ;
} int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen)
{
freopen("Asio_senddata.txt", "w+", stdout);
session* ltmp=(session*)isocket; boost::asio::async_write(ltmp->socket(),
boost::asio::buffer(ibuff, ilen),
boost::bind(&session::handle_write,
ltmp,
boost::asio::placeholders::error,
ltmp->userdata,
ikind
));
return ;
} int Asio_closesocket(int* isocket)
{
freopen("Asio_closesocket.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close(); }
catch(...)
{}
//EnterCriticalSection(&criCounter);
//deadlist.push_back(isocket);
//LeaveCriticalSection(&criCounter); //delete isocket;
return ;
} int Asio_Client_init(int* ipobj)
{
freopen("Asio_Client_init.txt", "w+", stdout);
session* new_session;
new_session=new session(io_service);
*ipobj=(int &)(*new_session);
return ;
} int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata)
{
freopen("Asio_Client_conntosvr.txt", "w+", stdout);
//域名解析,只将域名解析为ipv4地址
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string(iip), iport);
//只连接一次
boost::system::error_code ec;
session * lsock=(session *)isocket;
lsock->userdata=iuserdata;
lsock->socket().connect(ep,ec);
if (ec)
return ;
else
{
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} } int Asio_Client_DisConn(int * isocket)
{
freopen("Asio_Client_DisConn.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close();
}
catch(...)
{
}
return ;
} int Asio_Client_Free(int * isocket)
{
freopen("Asio_Client_Free.txt", "w+", stdout);
delete (session*)isocket;
return ;
} int Asio_Client_senddata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_senddata.txt", "w+", stdout);
session* lsock=(session*)isocket;
boost::system::error_code ignored_error;
boost::asio::write(lsock->socket(), boost::asio::buffer(ibuff,ilen),
boost::asio::transfer_all(), ignored_error);
if(ignored_error)
return ;
else
return ;
} int Asio_Client_readdata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_readdata.txt", "w+", stdout);
boost::system::error_code ignored_error;
session* lsock=(session*)isocket;
ilen=lsock->socket().read_some(boost::asio::buffer(ibuff,ilen), ignored_error);
if(ignored_error)
return ;
else
return ilen;
} int Asio_Client_Asreaddata(int * isocket,char * ibuff,int ilen,int iuserdata)
{
freopen("Asio_Client_Asreaddata.txt", "w+", stdout); session * lsock=(session *)isocket; lsock->userdata=iuserdata;
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
}

gcc main.cpp -Wall -g -DBUILD_DLL -DWIN32_LEAN_AND_MEAN -o main -user32 -l:ws2_32.dll -lboost_system -m32

gcc -LC:\Windows\System32 main.cpp ws2_32.dll

gcc -LC:\Windows\System32 -Wall -d'__USE_W32_SOCKETS' -g -DBUILD_DLL -DWIN32_LEAN_AND_MEAN main.cpp -D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -luser32 -user32 -l:ws2_32.dll -lboost_system -m32
-D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -std=c++11 -lboost_system -lws2_32

gcc -Wall -g -DBUILD_DLL -DWIN32_LEAN_AND_MEAN main.cpp -DVC_EXTRALEAN -DWINVER=0x0400 -D_WIN32_WINNT=0x0501 -D_WIN32_WINDOWS=0x0410 -D_WIN32_IE=0x0400 -D__USE_W32_SOCKETS -luser32 -user32 -lws2_32 -lboost_system -m32

4.2、完全安装boost库

执行下面命令即可

./bootstrap --buildtype=complete install
1
这样可以安装所有的测试版,发行版,静态库和动态库。

4.3、定制安装

用户可以根据自己的选择来编译需要使用的库

下面这条命令是查看所有必须编译才能使用的库

./b2 --show-libraries
1
在完全编译的基础上,使用–with或者–without选项可打开或者关闭某个库的编译,如

./b2 --with-date_time --buildtype=complete install
1
将仅仅编译安装date_time库

---------------------
作者:郭世江
来源:CSDN
原文:https://blog.csdn.net/jiang_xinxing/article/details/60150346
版权声明:本文为博主原创文章,转载请附上博文链接!

下面是测试boost是否安装成功的代码

#include <boost/version.hpp>
#include <boost/config.hpp>
int main()

{
cout << BOOST_VERSION <<endl;
cout << BOOST_LIB_VERSION<<endl;
cout << BOOST_STDLIB <<endl;
cout <<BOOST_PLATFORM <<endl;
cout << BOOST_COMPILER<<endl;
}

---------------------
作者:郭世江
来源:CSDN
原文:https://blog.csdn.net/jiang_xinxing/article/details/60150346
版权声明:本文为博主原创文章,转载请附上博文链接!

#define DLL _stdcall

#include "main.h"

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <windows.h>
#include <List> using boost::asio::ip::tcp; bool iscreate=false;
bool isfinished=false;
int gport;
class session;
class server;
boost::asio::io_service io_service;
boost::asio::deadline_timer* lt;
server* s; std::list<session*> deadlist;
std::list<session*>::iterator deadlist_Iter,deadlist_Iter2;
CRITICAL_SECTION criCounter; //回调函数设置
typedef void (CALLBACK* Cb_OnNewConn)(int ipsocket,const char* ipeerip,int ipeerport,int* iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_OnDisConn)(int iuserdata);
typedef void (CALLBACK* Cb_Onreaddata)(char* ibuff,int ilen,int iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_Onwritedata)(int iuserdata,int iuser2); Cb_OnNewConn Callback_NewConn;
Cb_OnDisConn Callback_DisConn;
Cb_Onreaddata Callback_readdata;
Cb_Onwritedata Callback_writedata; class session
{
public:
int readlen;
int userdata;
int deadtime;
enum { max_length = };
char data_[max_length]; session(boost::asio::io_service& io_service)
: socket_(io_service)
{
//data_[0]='a';
//data_[1]='b';
//data_[2]='1';
readlen=;
userdata=;
}
~session()
{
socket_.close();
// delete socket_;
} tcp::socket& socket()
{
return socket_;
} void start()
{
freopen("start.txt", "w+", stdout);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
freopen("handle_read.txt", "w+", stdout);
// std::cout << "read some len:" << bytes_transferred << "\n" ;
if (!error)
{ try
{
if(Callback_readdata!=NULL)
{
Callback_readdata(data_,bytes_transferred, userdata,&readlen); }
}
catch(...)
{
} if(userdata!=)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
}
delete this;
}
}
else
{ socket_.close();
// cout << "socket error " << error << "\n" ;
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} void handle_write(const boost::system::error_code& error,int iuser,int iuser2)
{
freopen("handle_write.txt", "w+", stdout);
if(Callback_writedata!=NULL)
{
// std::cout << "write ok " << iuser << "\n" ;
Callback_writedata(iuser,iuser2);
}
if (!error)
{ }
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} private:
tcp::socket socket_; }; class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
freopen("server.txt", "w+", stdout);
//创建临界区
InitializeCriticalSection(&criCounter); session* new_session = new session(io_service_); acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error
));
}
~server()
{
//删除临界区
DeleteCriticalSection(&criCounter); }
void handle_accept(session* new_session,
const boost::system::error_code& error
)
{
int lbuf;
// cout << "some one conned ok \n" ;
if (!error)
{
if(Callback_NewConn!=NULL)
{ lbuf=(int &)(*new_session);
// std::cout <<"socket addr:"<<lbuf << "\n" ;
// cout <<"IP:"<<new_session->socket().remote_endpoint().address().to_string()<< " port:"<<new_session->socket().remote_endpoint().port()<<" \n" ;
/* std::cout <<"userdata:"<<&new_session->userdata<< "\n" ;
std::cout <<"readlen:"<<&new_session->readlen<< "\n" ; */ Callback_NewConn(lbuf,new_session->socket().remote_endpoint().address().to_string().c_str(),
new_session->socket().remote_endpoint().port(),
&new_session->userdata,
&new_session->readlen); new_session->start();
new_session = new session(io_service_);
// //删除超时的sesson
// int i;
// for (deadlist_Iter=deadlist.begin(); deadlist_Iter !=deadlist.end(); ++deadlist_Iter)
//{
// i= (*deadlist_Iter)->deadtime;
// i=GetTickCount() -i;
//
// if ( i > 3000)
// {
// //用pos2来保存pos指针
// deadlist_Iter2=deadlist_Iter;
// deadlist_Iter++;
// deadlist.erase(deadlist_Iter2);
// delete (*deadlist_Iter);
// }
//} }
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
/* session* lp=(session*)(lbuf);
boost::asio::async_write(lp->socket() ,
boost::asio::buffer(new_session->data_, 4),
boost::bind(&session::handle_write,
new_session,
boost::asio::placeholders::error,
1
));*/ }
else
{
delete new_session;
}
} private: boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; void ontimer(const boost::system::error_code& /*e*/)
{
freopen("ontimer.txt", "w+", stdout);
if(isfinished==false)
{
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
} //typedef int (CALLBACK* fPlayerlst)(char* ibuff,int i);
//typedef int (CALLBACK* frecordlst)(char* ibuff,int i); extern __declspec(dllexport) int Asio_init(int iport)
{
freopen("Asio_init.txt", "w+", stdout);
if(iscreate==false)
{
/* AllocConsole();
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
cout << "init \n" ;*/
gport=iport;
iscreate=true;
if(iport==)
//server s(io_service, iport);
//else
{
lt=new boost::asio::deadline_timer(io_service,boost::posix_time::seconds());
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
return ;
}
return ;
//
//if (access("plog", 0) == 0)
//{ //}
} extern __declspec(dllexport) int Asio_SvrRun()
{
freopen("Asio_SvrRun.txt", "w+", stdout);
if(gport!=)
{
s=new server(io_service,gport);
}
try
{
io_service.run();
}
catch(...)
{
}
delete s; if(gport==)
{
Sleep();
delete lt;
} return ;
} extern __declspec(dllexport) int Asio_Uninit(int iport)
{
freopen("Asio_Uninit.txt", "w+", stdout);
if(gport==)
{
lt->cancel();
}
io_service.stop();
isfinished=true;
return ;
} extern __declspec(dllexport) int Asio_SetCallback(int ikind,int ifun)
{
freopen("Asio_SetCallback.txt", "w+", stdout);
if(ikind==)
Callback_NewConn=(Cb_OnNewConn)ifun;
else if(ikind==)
Callback_DisConn=(Cb_OnDisConn)ifun;
else if(ikind==)
Callback_readdata=(Cb_Onreaddata)ifun;
else if(ikind==)
Callback_writedata=(Cb_Onwritedata)ifun;
return ;
} extern __declspec(dllexport) int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen)
{
freopen("Asio_senddata.txt", "w+", stdout);
session* ltmp=(session*)isocket; boost::asio::async_write(ltmp->socket(),
boost::asio::buffer(ibuff, ilen),
boost::bind(&session::handle_write,
ltmp,
boost::asio::placeholders::error,
ltmp->userdata,
ikind
));
return ;
} extern __declspec(dllexport) int Asio_closesocket(int* isocket)
{
freopen("Asio_closesocket.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close(); }
catch(...)
{}
//EnterCriticalSection(&criCounter);
//deadlist.push_back(isocket);
//LeaveCriticalSection(&criCounter); //delete isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_init(int* ipobj)
{
freopen("Asio_Client_init.txt", "w+", stdout);
session* new_session;
new_session=new session(io_service);
*ipobj=(int &)(*new_session);
return ;
} extern __declspec(dllexport) int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata)
{
freopen("Asio_Client_conntosvr.txt", "w+", stdout);
//域名解析,只将域名解析为ipv4地址
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string(iip), iport);
//只连接一次
boost::system::error_code ec;
session * lsock=(session *)isocket;
lsock->userdata=iuserdata;
lsock->socket().connect(ep,ec);
if (ec)
return ;
else
{
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} } extern __declspec(dllexport) int Asio_Client_DisConn(int * isocket)
{
freopen("Asio_Client_DisConn.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close();
}
catch(...)
{
}
return ;
} extern __declspec(dllexport) int Asio_Client_Free(int * isocket)
{
freopen("Asio_Client_Free.txt", "w+", stdout);
delete (session*)isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_senddata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_senddata.txt", "w+", stdout);
session* lsock=(session*)isocket;
boost::system::error_code ignored_error;
boost::asio::write(lsock->socket(), boost::asio::buffer(ibuff,ilen),
boost::asio::transfer_all(), ignored_error);
if(ignored_error)
return ;
else
return ;
} extern __declspec(dllexport) int Asio_Client_readdata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_readdata.txt", "w+", stdout);
boost::system::error_code ignored_error;
session* lsock=(session*)isocket;
ilen=lsock->socket().read_some(boost::asio::buffer(ibuff,ilen), ignored_error);
if(ignored_error)
return ;
else
return ilen;
} extern __declspec(dllexport) int Asio_Client_Asreaddata(int * isocket,char * ibuff,int ilen,int iuserdata)
{
freopen("Asio_Client_Asreaddata.txt", "w+", stdout); session * lsock=(session *)isocket; lsock->userdata=iuserdata;
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
freopen("DllMain.txt", "w+", stdout);
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break; case DLL_PROCESS_DETACH:
// detach from process
break; case DLL_THREAD_ATTACH:
// attach to thread
break; case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
#ifndef __MAIN_H__
#define __MAIN_H__ #include <windows.h> /* To use this exported function of dll, include this header
* in your project.
*/ #ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif #ifdef __cplusplus
extern "C"
{
#endif DLL_EXPORT int Asio_init(int iport); DLL_EXPORT int Asio_SvrRun(); DLL_EXPORT int Asio_Uninit(int iport); DLL_EXPORT int Asio_SetCallback(int ikind,int ifun); DLL_EXPORT int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_closesocket(int* isocket); DLL_EXPORT int Asio_Client_init(int* ipobj); DLL_EXPORT int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata); DLL_EXPORT int Asio_Client_DisConn(int* isocket); DLL_EXPORT int Asio_Client_Free(int* isocket); DLL_EXPORT int Asio_Client_senddata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_readdata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_Asreaddata(int* isocket,char * ibuff,int ilen,int iuserdata); #ifdef __cplusplus
}
#endif #endif // __MAIN_H__

Windows MinGW 64-bit boost 踩坑的更多相关文章

  1. Faster_Rcnn在windows下运行踩坑总结

    Faster_Rcnn在windows下运行踩坑总结  20190524 今天又是元气满满的一天! 1.代码下载 2.编译 3.下载数据集 4.下载pre-train Model 5.运行train ...

  2. Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64

    Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64 Sun, 01/01/2012 - 15:43 ...

  3. 使用Windows下的git工具往github上传代码 踩坑记录

    使用Windows下的git工具往github上传代码 踩坑记录 背景 由于以前接触的项目都是通过svn进行版本控制,现在公司项目使用git,加上自己平时有一个练手小项目,趁着周末试着把项目上传到自己 ...

  4. windows container 踩坑记

    windows container 踩坑记 Intro 我们有一些服务是 dotnet framework 的,不能直接跑在 docker linux container 下面,最近一直在折腾把它部署 ...

  5. 在CentOS/Windows下配置Nginx(以及踩坑)

    在CentOS/Windows下配置Nginx(以及踩坑) 1. 序言 因为这类文章网上比较多,实际操作起来也大同小异,所以我并不会着重于详细配置方面,而是将我配置时踩的坑写出来. 2. CentOS ...

  6. Windows Server 2012搭建SQL Server Always On踩坑全记录

    Windows Server 2012搭建SQL Server Always On踩坑全记录 环境信息: Windows Server 2012 R2 Sql Server 2012 整个搭建集群的过 ...

  7. windows生成github密钥并推送文件踩坑

    强调官方文档最可靠,百度踩坑很浪费时间,建议去寻找一手数据源头 github官方文档提供了帮助 第一步 查看密钥 如果您还没有 SSH 密钥,则必须生成新 SSH 密钥. 如果您不确定是否已有 SSH ...

  8. OpenCV+Qt+CMake安装+十种踩坑

    平台:win10 x64+opencv-3.4.1 + qt-x86-5.9.0 + cmake3.13.4 x64 OpenCV+Qt+CMake安装,及目前安装完后打包:mingw32-make时 ...

  9. TrinityCore3.3.5编译过程-官方指导-踩坑总结

    官方指导:主页->how to compile -> windows 指导文档写得很详细,但有不少细节点没提到,这里把过程简化总结,说明重点,及易坑点 1,安装需求 编译工具:cmake, ...

随机推荐

  1. 阿里云的opensearch

    官方文档:https://help.aliyun.com/document_detail/29104.html?spm=a2c4g.11186623.6.544.5215187be2TnnD

  2. 【OpenJ_Bailian - 4137】最小新整数 (贪心)

    最小新整数 Descriptions: 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < ...

  3. 全球首款iOS模拟器出炉!在违法的边缘疯狂试探

    对于许多智能手机用户,特别是手游玩家来说,在手机屏幕的方寸之间进行操作显然并非特别方便,而且在多年之前,由于手机配置不足,也导致了用PC来玩手游的需求不断涌现.彼时,BlueStacks及夜神等一众A ...

  4. A. Office Keys ( Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) )

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  5. SOA思想

    参考:https://www.cnblogs.com/renzhitian/p/6853289.html 是什么 SOA service-oriented architecture 面向服务的体系结构 ...

  6. Hive_Hive的数据模型_内部表

    Hive的数据模型_内部表 - 与数据库中的Table在概念上是类似.- 每一个Table在Hive中都有一个相应的目录存储数据.- 所有的Table数据(不包括External Table)都保存在 ...

  7. html文本框和按钮这些点击时不显示边框的光晕

    直接在样式加:focus{outline:0;}这样子就可以了

  8. SpringBoot项目取消数据库配置

    1. 错误springboot项目启动时,如果没有配置数据库配置,启动时会抛出如下异常. Description: Cannot determine embedded database driver ...

  9. ECSHOP商品属性调用到任意页面方法

    看到标题有的人觉得这个很复杂,其实这个没那么复杂,直接用下面的方法,就可以在ECSHOP的任意页面调用商品属性. 一)打开includes\lib_insert.php文件,在最后面增加一个函数: f ...

  10. Java环境安装与Eclipse安装

    1.jdk下载安装 2.Eclipse下载安装 遇到的问题: 出现问题原因可能有两个:1)没有配置环境变量 2)jdk和eclipse安装的版本不一致,都是64位或者都是32位. 本人出现错误的原因: ...