Skip to content

Instantly share code, notes, and snippets.

@bechu
Created April 19, 2012 19:18
Show Gist options
  • Save bechu/2423333 to your computer and use it in GitHub Desktop.
Save bechu/2423333 to your computer and use it in GitHub Desktop.
simple boost tcp client example
/*
g++ main.cpp -lboost_system -lboost_thread -lpthread -o main
*/
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <iostream>
void send_something(std::string host, int port, std::string message)
{
boost::asio::io_service ios;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(host), port);
boost::asio::ip::tcp::socket socket(ios);
socket.connect(endpoint);
boost::array<char, 128> buf;
std::copy(message.begin(),message.end(),buf.begin());
boost::system::error_code error;
socket.write_some(boost::asio::buffer(buf, message.size()), error);
socket.close();
}
int main()
{
send_something("127.0.0.1", 1990, "hello flowers team");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment