Created
September 21, 2012 14:35
-
-
Save yifu/3761822 to your computer and use it in GitHub Desktop.
Chat room using asio.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } | |
| } |
Author
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
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