Visual Studio 实现TCp socket 异步通信,客户端和服务器端
Visual Studio 实现TCp socket 异步通信,分为客户端和服务器端两份资源
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 286720 2017-02-17 14:22 yibusyibusDebugyibus.exe
文件 1890944 2017-02-17 14:22 yibusyibusDebugyibus.ilk
文件 4066304 2017-02-17 14:22 yibusyibusDebugyibus.pdb
文件 1638532 2017-02-17 14:22 yibusyibusyibusDebugserver.obj
文件 2370560 2017-02-17 14:22 yibusyibusyibusDebugvc120.idb
文件 1159168 2017-02-17 14:22 yibusyibusyibusDebugvc120.pdb
文件 1610 2017-02-17 14:22 yibusyibusyibusDebugyibus.log
文件 542 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogcl.command.1.tlog
文件 89944 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogCL.read.1.tlog
文件 396 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogCL.write.1.tlog
文件 1098 2017-02-17 14:22 yibusyibusyibusDebugyibus.tloglink.command.1.tlog
文件 3202 2017-02-17 14:22 yibusyibusyibusDebugyibus.tloglink.read.1.tlog
文件 370 2017-02-17 14:22 yibusyibusyibusDebugyibus.tloglink.write.1.tlog
文件 160 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogyibus.lastbuildstate
文件 1959 2017-02-17 14:22 yibusyibusyibusserver.cpp
文件 3558 2017-02-15 10:57 yibusyibusyibusyibus.vcxproj
文件 947 2017-02-15 10:57 yibusyibusyibusyibus.vcxproj.filters
文件 109445120 2017-02-17 14:32 yibusyibusyibus.sdf
文件 961 2017-02-15 10:55 yibusyibusyibus.sln
..A..H. 17408 2017-02-17 14:32 yibusyibusyibus.v12.suo
文件 297472 2017-02-17 14:20 yibucyibucDebugyibuc.exe
文件 1460088 2017-02-17 14:20 yibucyibucDebugyibuc.ilk
文件 3812352 2017-02-17 14:20 yibucyibucDebugyibuc.pdb
文件 1683 2017-02-17 14:20 yibucyibucyibucclient.cpp
文件 1713222 2017-02-17 14:20 yibucyibucyibucDebugclient.obj
文件 2362368 2017-02-17 14:20 yibucyibucyibucDebugvc120.idb
文件 1093632 2017-02-17 14:20 yibucyibucyibucDebugvc120.pdb
文件 693 2017-02-17 11:26 yibucyibucyibucDebugyibuc.Build.CppClean.log
文件 1610 2017-02-17 14:20 yibucyibucyibucDebugyibuc.log
文件 542 2017-02-17 14:20 yibucyibucyibucDebugyibuc.tlogcl.command.1.tlog
............此处省略26个文件信息
#include
#include
#include
#include “boost/shared_ptr.hpp“
#include
#include
#include
using namespace boost::asio;
class Client
{
typedef ip::tcp::socket socket_type;
typedef std::shared_ptr sock_ptr;
public:
Client() :m_ep(ip::address::from_string(“127.0.0.1“) 6688)
{
start();
}
void run()
{
m_io.run();
}
private:
void start()
{
sock_ptr sock(new socket_type(m_io));
sock->async_connect(m_ep bind(&Client::connect_handler this placeholders::error sock));
}
void connect_handler(const boost::system::error_code& ec sock_ptr sock)
{
if (ec)
{
return;
}
std::cout << “receive from:“ << sock->remote_endpoint().address() << std::endl;
sock->async_read_some(buffer(m_buf)
bind(&Client::read_handler this placeholders::errorsock));
}
void read_handler(const boost::system::error_code& ec sock_ptr sock)
{
if (ec)
{
return;
}
std::ofstream outfile;
outfile.open(“D:\testdata_case012.sp“ std::ios::app);
outfile << m_buf << std::endl;
outfile.close();
//std::cout << m_buf << std::endl;
sock->async_read_some(buffer(m_buf)
bind(&Client::read_handler this placeholders::errorsock));
}
private:
io_service m_io;
ip::tcp::endpoint m_ep;
enum { max_length = 1000 };
char m_buf[max_length+16];
int count;
};
int main()
{
try
{
std::cout << “client start.“ << std::endl;
Client cl;
cl.run();
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
getchar();
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 286720 2017-02-17 14:22 yibusyibusDebugyibus.exe
文件 1890944 2017-02-17 14:22 yibusyibusDebugyibus.ilk
文件 4066304 2017-02-17 14:22 yibusyibusDebugyibus.pdb
文件 1638532 2017-02-17 14:22 yibusyibusyibusDebugserver.obj
文件 2370560 2017-02-17 14:22 yibusyibusyibusDebugvc120.idb
文件 1159168 2017-02-17 14:22 yibusyibusyibusDebugvc120.pdb
文件 1610 2017-02-17 14:22 yibusyibusyibusDebugyibus.log
文件 542 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogcl.command.1.tlog
文件 89944 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogCL.read.1.tlog
文件 396 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogCL.write.1.tlog
文件 1098 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogli
文件 3202 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogli
文件 370 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogli
文件 160 2017-02-17 14:22 yibusyibusyibusDebugyibus.tlogyibus.lastbuildstate
文件 1959 2017-02-17 14:22 yibusyibusyibusserver.cpp
文件 3558 2017-02-15 10:57 yibusyibusyibusyibus.vcxproj
文件 947 2017-02-15 10:57 yibusyibusyibusyibus.vcxproj.filters
文件 109445120 2017-02-17 14:32 yibusyibusyibus.sdf
文件 961 2017-02-15 10:55 yibusyibusyibus.sln
..A..H. 17408 2017-02-17 14:32 yibusyibusyibus.v12.suo
文件 297472 2017-02-17 14:20 yibucyibucDebugyibuc.exe
文件 1460088 2017-02-17 14:20 yibucyibucDebugyibuc.ilk
文件 3812352 2017-02-17 14:20 yibucyibucDebugyibuc.pdb
文件 1683 2017-02-17 14:20 yibucyibucyibucclient.cpp
文件 1713222 2017-02-17 14:20 yibucyibucyibucDebugclient.obj
文件 2362368 2017-02-17 14:20 yibucyibucyibucDebugvc120.idb
文件 1093632 2017-02-17 14:20 yibucyibucyibucDebugvc120.pdb
文件 693 2017-02-17 11:26 yibucyibucyibucDebugyibuc.Build.CppClean.log
文件 1610 2017-02-17 14:20 yibucyibucyibucDebugyibuc.log
文件 542 2017-02-17 14:20 yibucyibucyibucDebugyibuc.tlogcl.command.1.tlog
............此处省略26个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)