Skip to content

Instantly share code, notes, and snippets.

@yifu
Created September 21, 2012 14:35
Show Gist options
  • Select an option

  • Save yifu/3761822 to your computer and use it in GitHub Desktop.

Select an option

Save yifu/3761822 to your computer and use it in GitHub Desktop.
Chat room using asio.
#include <iostream>
#include <boost/asio.hpp>
// g++ -I /Soft/mag/dev/tools/boost-1.48.0/include/ -L /Soft/mag/dev/tools/boost-1.48.0/lib/ -l boost_system -lpthread client.cpp -o client
using namespace std;
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
using namespace boost::system;
int main(int argc, char *argv[])
{
try
{
if (argc != 3)
{
cerr << "Usage: client host port" << std::endl;
return 1;
}
io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], "55555"/*"daytime"*/);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
connect(socket, endpoint_iterator);
string line;
while(getline(cin, line))
{
error_code error;
write(socket, buffer(line), error);
// array<char, 128> buf;
// const size_t len = socket.read_some(buffer(buf), error);
// if (error == boost::asio::error::eof)
// break; // Connection closed cleanly by peer.
// else if (error)
// throw boost::system::system_error(error); // Some other error.
// cout.write(buf.data(), len);
}
}
catch(std::exception& e)
{
cerr << e.what() << endl;
}
}
@yifu

yifu commented Sep 21, 2012

Copy link
Copy Markdown
Author

g++ -I /Soft/mag/dev/tools/boost-1.48.0/include/ -L /Soft/mag/dev/tools/boost-1.48.0/lib/ -l boost_system -lpthread client.cpp -o client

@yifu

yifu commented Sep 21, 2012

Copy link
Copy Markdown
Author

g++ -I /Soft/mag/dev/tools/boost-1.48.0/include/ -L /Soft/mag/dev/tools/boost-1.48.0/lib/ -l boost_system -lpthread client.cpp -o client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment