How to build an echo server and client using asio local datagram?

Clash Royale CLAN TAG#URR8PPP
How to build an echo server and client using asio local datagram?
// 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
2 Answers
2
Find the answer here.
client,server.
The reason the server aborting is because the senderEndpoint is not being populated by the call to receive_from - the client socket is not bound to anything so it does not send a file descriptor on which to receive responses. If we refactor the client like this:
senderEndpoint
receive_from
#include <iostream>
#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 server_endpoint("local_socket");
// Create client endpoint on which to receive response
std::string client_addr = "local_socket.";
client_addr += std::to_string(::getpid());
::unlink(client_addr.c_str());
boost::asio::local::datagram_protocol::endpoint client_endpoint(client_addr.c_str());
boost::asio::local::datagram_protocol::socket socket(io_service, client_endpoint);
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), server_endpoint);
char reply[max_length];
size_t reply_length = socket.receive_from(
boost::asio::buffer(reply, max_length), client_endpoint);
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "n";
return 0;
then it works as expected:
Enter message: 123
***local_socket.29635###
Reply is: 123
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.