// server
#include <boost/asio.hpp>
int main()
{
::unlink("local_socket");
boost::asio::io_service io_service;
boost::asio::local::datagram_protocol::endpoint endpoint("local_socket");
boost::asio::local::datagram_protocol::socket socket(io_service, endpoint);
char recv_buf[1024];
while (1)
{
boost::asio::local::datagram_protocol::endpoint senderEndpoint;
size_t len = socket.receive_from(
boost::asio::buffer(recv_buf, 1024), senderEndpoint);
printf("***%s###\n", senderEndpoint.path().c_str());
socket.send_to(
boost::asio::buffer(recv_buf, len), senderEndpoint);
}
return 0;
}
// client
#include <boost/asio.hpp>
enum { max_length = 1024 };
int main(int argc, char* argv[])
{
boost::asio::io_service io_service;
boost::asio::local::datagram_protocol::endpoint endpoint("local_socket");
boost::asio::local::datagram_protocol::socket socket(io_service);
socket.open();
std::cout << "Enter message: ";
char request[max_length];
std::cin.getline(request, max_length);
size_t request_length = strlen(request);
socket.send_to(
boost::asio::buffer(request, request_length), endpoint);
char reply[max_length];
size_t reply_length = socket.receive_from(
boost::asio::buffer(reply, max_length), endpoint);
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
return 0;
}
When I use client to send "123", and server is coredump with:
***### terminate called after throwing an instance of 'boost::exception_detail::clone_impl
' what(): send_to: Invalid argument Aborted