Advertisement
obernardovieira

Client Send Message to Server - boost

Sep 21st, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. //CLIENT
  2.  
  3. #pragma warning(disable : 4996)
  4.  
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <boost/asio.hpp>
  9.  
  10. using boost::asio::ip::udp;
  11.  
  12. enum { max_length = 1024 };
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     try
  17.     {
  18.         if (argc != 3)
  19.         {
  20.             std::cerr << "Usage: blocking_udp_echo_client <host> <port>\n";
  21.             return 1;
  22.         }
  23.  
  24.         boost::asio::io_service io_service;
  25.  
  26.         udp::socket s(io_service, udp::endpoint(udp::v4(), 0));
  27.  
  28.         udp::resolver resolver(io_service);
  29.         udp::resolver::query query(udp::v4(), argv[1], argv[2]);
  30.         udp::resolver::iterator iterator = resolver.resolve(query);
  31.  
  32.         using namespace std; // For strlen.
  33.         std::cout << "Enter message: ";
  34.         char request[max_length];
  35.         std::cin.getline(request, max_length);
  36.         size_t request_length = strlen(request);
  37.         s.send_to(boost::asio::buffer(request, request_length), *iterator);
  38.  
  39.         char reply[max_length];
  40.         udp::endpoint sender_endpoint;
  41.         size_t reply_length = s.receive_from(
  42.         boost::asio::buffer(reply, max_length), sender_endpoint);
  43.         std::cout << "Reply is: ";
  44.         std::cout.write(reply, reply_length);
  45.         std::cout << "\n";
  46.     }
  47.     catch (std::exception& e)
  48.     {
  49.         std::cerr << "Exception: " << e.what() << "\n";
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
  55. //SERVER
  56.  
  57. #pragma warning(disable : 4996)
  58.  
  59. #include <cstdlib>
  60. #include <iostream>
  61. #include <boost/asio.hpp>
  62.  
  63. using boost::asio::ip::udp;
  64.  
  65. enum { max_length = 1024 };
  66.  
  67. void server(boost::asio::io_service& io_service, short port)
  68. {
  69.     udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
  70.     for (;;)
  71.     {
  72.         char data[max_length];
  73.         udp::endpoint sender_endpoint;
  74.         size_t length = sock.receive_from(
  75.         boost::asio::buffer(data, max_length), sender_endpoint);
  76.  
  77.         std::cout << "received : ";
  78.         std::cout.write(data, length);
  79.         std::cout << "\n";
  80.  
  81.         sock.send_to(boost::asio::buffer(data, length), sender_endpoint);
  82.     }
  83. }
  84.  
  85. int main(int argc, char* argv[])
  86. {
  87.     try
  88.     {
  89.         if (argc != 2)
  90.         {
  91.             std::cerr << "Usage: blocking_udp_echo_server <port>\n";
  92.             return 1;
  93.         }
  94.  
  95.         boost::asio::io_service io_service;
  96.  
  97.         using namespace std; // For atoi.
  98.         server(io_service, atoi(argv[1]));
  99.     }
  100.     catch (std::exception& e)
  101.     {
  102.         std::cerr << "Exception: " << e.what() << "\n";
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement