1.服务端建立链接
int accept_new_connection() {
//服务端
const int BACKLOG_SIZE = 30;
unsigned short port_num = 3333;
//服务器先生成端点
asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(), port_num);
asio::io_context ios;
try
{
//创建接收器
asio::ip::tcp::acceptor acceptor(ios, ep.protocol());
//绑定端点
acceptor.bind(ep);
//监听
acceptor.listen(BACKLOG_SIZE);
asio::ip::tcp::socket sock(ios);
//accept返回新的链接
acceptor.accept(sock);
}
catch (system::system_error& e)
{
std::cout << "Failed to accept_new_connection. Error code =" << e.code() << ".Message is" << e.what();
return e.code().value();
}
return 0;
}
2.客户端建立链接
int connect_to_end() {
//客户端链接
std::string raw_ip_address = "192.168.1.124";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num);
asio::io_context ios;
asio::ip::tcp::socket sock(ios, ep.protocol());
sock.connect(ep);
}
catch (system::system_error& e)
{
std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
return e.code().value();
}
return 0;
}
3.读写数据
void write_to_socket(asio::ip::tcp::socket& sock) {
std::string buf = "Hello World";
std::size_t total_bytes_writter = 0;
while (total_bytes_writter!=buf.length())
{
total_bytes_writter += sock.write_some(asio::buffer(buf.c_str() + total_bytes_writter,
buf.length() - total_bytes_writter));
}
}
int send_data_by_write_some() {
std::string raw_ip_address = "192.168.1.124";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num);
asio::io_context ios;
asio::ip::tcp::socket sock(ios, ep.protocol());
sock.connect(ep);
//write_to_socket(sock);
/*std::string buf = "Hello World";
int send_lenth = sock.send(asio::buffer(buf.c_str(), buf.length()));
if (send_lenth<=0)
{
return 0;
}*/
std::string buf = "Hello World";
int send_lenth = asio::write(sock,asio::buffer(buf.c_str(),buf.length()));
if (send_lenth <= 0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
return e.code().value();
}
return 0;
}
std::string read_from_socket(asio::ip::tcp::socket& sock) {
const unsigned char MESSAGE_SIZE = 7;
char buf[MESSAGE_SIZE];
std::size_t total_bytes_read = 0;
while (total_bytes_read!= MESSAGE_SIZE)
{
total_bytes_read += sock.read_some(asio::buffer(buf + total_bytes_read, MESSAGE_SIZE - total_bytes_read));
}
return std::string(buf, total_bytes_read);
}
int read_data_by_read_some() {
std::string raw_ip_address = "192.168.1.124";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num);
asio::io_context ios;
asio::ip::tcp::socket sock(ios, ep.protocol());
sock.connect(ep);
//read_from_socket(sock);
const unsigned char MESSAGE_SIZE = 7;
char buf_receive[MESSAGE_SIZE];
/*int send_lenth = sock.receive(asio::buffer(buf_receive, MESSAGE_SIZE));
if (send_lenth<=0)
{
return -1;
}*/
int send_lenth = asio::read(sock, asio::buffer(buf_receive, MESSAGE_SIZE));
if (send_lenth <= 0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
return e.code().value();
}
}