Advertisement
Guest User

Untitled

a guest
Apr 27th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <boost/asio.hpp>
  2. #include <unistd.h>
  3.  
  4. int main() {
  5.   try {
  6.     const char* host = "192.168.1.100";
  7.     const unsigned short port = 8080;
  8.  
  9.     std::ostringstream s;
  10.     s << "pid is " << getpid() << std::endl;
  11.     std::string message = s.str();
  12.  
  13.     namespace asio = boost::asio;
  14.     using Address = asio::ip::address_v4;
  15.     using Tcp = asio::ip::tcp;
  16.  
  17.     asio::io_service io_service;
  18.     asio::ip::tcp::resolver resolver(io_service);
  19.  
  20.     Address address = Address::from_string(host);
  21.     Tcp::endpoint ep(address, port);
  22.     Tcp::resolver::iterator endpoint_iterator = resolver.resolve(ep);
  23.  
  24.     Tcp::socket socket(io_service);
  25.     asio::connect(socket, endpoint_iterator);
  26.  
  27.     socket.send(asio::buffer(message));
  28.  
  29.     return EXIT_FAILURE;
  30.   }
  31.   catch (std::exception& e)
  32.   {
  33.     std::cout << "Exception: " << e.what() << std::endl;
  34.     return EXIT_FAILURE;
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement