1Boost之TCP,Client and Server
2014-09-06 23:14
316 查看
客户端代码
#include
<boost/asio.hpp>
#include
<iostream>
#include
<stdlib.h>
using
namespace
std;
using
namespace
boost::asio;
void
main()
{
io_service
iosev;
ip::tcp::socket
mysorket(iosev);
ip::tcp::endpoint
ep(ip::address_v4::from_string("127.0.0.1"),
1100);
boost::system::error_code
ec;
mysorket.connect(ep,
ec);//链接
while (1)
{
char
str[1024] = { 0 };
cout <<
"请输入";
cin >>
str;
cout <<
endl;
mysorket.write_some(buffer(str),
ec);
memset(str,
0, 1024);//清空字符串
mysorket.read_some(buffer(str),
ec);
cout <<
"收到"
<< str <<
endl;
}
cin.get();
}
客户端代码:
服务器端代码:
#include
<boost/asio.hpp>
#include
<iostream>
#include
<stdlib.h>
using
namespace
std;
using
namespace
boost::asio;
void
main()
{
io_service
iosev;
//这里使用ip v4协议,端口号是1100
ip::tcp::acceptor
myacceptor(iosev,
ip::tcp::endpoint(ip::tcp::v4(),
1100));
while (1)
{
//构建TCP
ip::tcp::socket
mysocket(iosev);
//接受套接字
myacceptor.accept(mysocket);
cout <<
"客户端"
<< mysocket.remote_endpoint().address()
<< ":"
<< mysocket.remote_endpoint().port()
<< "链接上"
<< endl;
char
recestr[1024] = { 0 };
boost::system::error_code
ec;
//处理网络异常
int
length =
mysocket.read_some(buffer(recestr),
ec);
cout <<
"收到"
<< recestr <<
"长度"
<< length <<
endl;
system(recestr);
length =
mysocket.write_some(buffer(recestr,
length),
ec);
cout <<
"发送报文长度"
<< length <<
endl;
}
cin.get();
}
运行结果:
客户端代码
#include
<boost/asio.hpp>
#include
<iostream>
#include
<stdlib.h>
using
namespace
std;
using
namespace
boost::asio;
void
main()
{
io_service
iosev;
ip::tcp::socket
mysorket(iosev);
ip::tcp::endpoint
ep(ip::address_v4::from_string("127.0.0.1"),
1100);
boost::system::error_code
ec;
mysorket.connect(ep,
ec);//链接
while (1)
{
char
str[1024] = { 0 };
cout <<
"请输入";
cin >>
str;
cout <<
endl;
mysorket.write_some(buffer(str),
ec);
memset(str,
0, 1024);//清空字符串
mysorket.read_some(buffer(str),
ec);
cout <<
"收到"
<< str <<
endl;
}
cin.get();
}
客户端代码:
服务器端代码:
#include
<boost/asio.hpp>
#include
<iostream>
#include
<stdlib.h>
using
namespace
std;
using
namespace
boost::asio;
void
main()
{
io_service
iosev;
//这里使用ip v4协议,端口号是1100
ip::tcp::acceptor
myacceptor(iosev,
ip::tcp::endpoint(ip::tcp::v4(),
1100));
while (1)
{
//构建TCP
ip::tcp::socket
mysocket(iosev);
//接受套接字
myacceptor.accept(mysocket);
cout <<
"客户端"
<< mysocket.remote_endpoint().address()
<< ":"
<< mysocket.remote_endpoint().port()
<< "链接上"
<< endl;
char
recestr[1024] = { 0 };
boost::system::error_code
ec;
//处理网络异常
int
length =
mysocket.read_some(buffer(recestr),
ec);
cout <<
"收到"
<< recestr <<
"长度"
<< length <<
endl;
system(recestr);
length =
mysocket.write_some(buffer(recestr,
length),
ec);
cout <<
"发送报文长度"
<< length <<
endl;
}
cin.get();
}
运行结果:
相关文章推荐
- 2Boost之UPD,Client and Server
- TCP_client and server in py
- C#:TcpClient(客户端) and TcpServer(服务端)
- 深入Boost.asio的异步Tcp通信的server、client,可用来当程序骨架。
- tcp client and server
- TCP/IP Client and Server¶
- tcp sock kernel server and client
- Simple TcpClient and TcpServer in Java
- Boost TCP server&client 有回调无发送
- tcp_sync_server and tcp_sync_client
- Simple TCP Socket Client and Server Communication in C Under Linux
- ServerSocket and ClientSocket
- [转载]异步Socket服务器与客户端(An Asynchronous Socket Server and Client)
- Jax-ws server side and client side
- 基于C++的纯面向对象的通用高性能大并发TCP-SERVER/CLIENT开发框架实践系列之基础篇
- TCP Server《——》TCP Client
- Sock application with two classes:CServer and CClient
- Tomcat5SSL_ServerAndClient 在 Tomcat 5.5 上的配置实例
- TCPserver Tcpclient 怎么实现聊天室一起聊天的功能呢?