网络编程3
同步续2
发送数据函数
循环发送
void write_to_socket(asio::ip::tcp::socket& sock)
{
std::string buf = "hello world";
std::size_t total_bytes_written = 0;
//循环发送
while (total_bytes_written!=buf.length())
{
total_bytes_written += sock.write_some(asio::buffer(buf.c_str()+total_bytes_written,
buf.length() - total_bytes_written));//循环的发送,一次发送最大的数据,并累加到total_bytes_written
}
}
int send_data_by_write_some()
{
std::string raw_ip_address = "192.168.0.60";//地址
unsigned short port_num =3333;//端口
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);//这段代码是使用 Boost.Asio(或独立 Asio 库)创建一个 TCP 端点(endpoint),用于表示一个网络通信的目标地址和端口。
//asio::io_service ios;
asio::io_context ioc;//TCP上下文
asio::ip::tcp::socket sock(ioc, ep.protocol());//创建socket
sock.connect(ep);//socket连接
write_to_socket(sock);//发送数据
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
return e.code().value();
}
return 0;
}
使用send发送
int send_data_by_write_some()
{
std::string raw_ip_address = "192.168.0.60";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);
//asio::io_service ios;
asio::io_context ioc;
asio::ip::tcp::socket sock(ioc, ep.protocol());
sock.connect(ep);
std::string buf = "hello world!";
int send_length = sock.send(asio::buffer(buf.c_str(), buf.length()));//length<0是系统的错误,length==0是发送失败,length>0是发送成功
if (send_length <= 0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
return e.code().value();
}
return 0;
}
写数据
int send_data_by_send()
{
std::string raw_ip_address = "192.168.0.60";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);
//asio::io_service ios;
asio::io_context ioc;
asio::ip::tcp::socket sock(ioc, ep.protocol());
sock.connect(ep);
std::string buf = "hello world!";
int send_length = asio::write(sock, asio::buffer(buf.c_str(), buf.length());
if (send_length <= 0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
return e.code().value();
}
return 0;
}
//两函数行为是等效的
int send_data_by_write()
{
std::string raw_ip_address = "192.168.0.60";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);
//asio::io_service ios;
asio::io_context ioc;
asio::ip::tcp::socket sock(ioc, ep.protocol());
sock.connect(ep);
std::string buf = "hello world!";
int send_length = sock.send(asio::buffer(buf.c_str(), buf.length()));
if (send_length <= 0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
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 = "127.0.0.0";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);
asio::io_context ioc;
asio::ip::tcp::socket sock(ioc, ep);
sock.connect(ep);
read_from_socket(sock);
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
return e.code().value();
}
return 0;
}
使用receive
int read_data_by_receive()
{
std::string raw_ip_address = "127.0.0.0";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);
asio::io_context ioc;
asio::ip::tcp::socket sock(ioc, ep);
sock.connect(ep);
const unsigned char BUFF_SIZE = 7;
char buffer_receive[BUFF_SIZE];
int receive_length = sock.receive(asio::buffer(buffer_receive,BUFF_SIZE));//recevice读取返回的数值与send一致
if (receive_length<=0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
return e.code().value();
}
return 0;
}
使用read
int read_data_by_read()
{
std::string raw_ip_address = "127.0.0.0";
unsigned short port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::make_address(raw_ip_address), port_num);
asio::io_context ioc;
asio::ip::tcp::socket sock(ioc, ep);
sock.connect(ep);
const unsigned char BUFF_SIZE = 7;
char buffer_receive[BUFF_SIZE];
int receive_length = asio::read(sock,asio::buffer(buffer_receive, BUFF_SIZE));
if (receive_length <= 0)
{
return 0;
}
}
catch (system::system_error& e)
{
std::cout << e.what() << std::endl;
return e.code().value();
}
return 0;
}
