Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/algorithm/string/compare.hpp>
- #include <boost/lexical_cast.hpp>
- int main(int argc, const char* argv[])
- {
- using boost::asio::ip::tcp;
- enum { PORT = 12580 };
- if ( argc == 2 && boost::is_iequal()(argv[1], (const char*)"server") == 0 )
- {
- boost::asio::io_service io_service;
- tcp::endpoint endpoint( tcp::v4(), PORT );
- tcp::acceptor acceptor( io_service, endpoint );
- tcp::iostream stream;
- boost::system::error_code ec;
- for (;;)
- {
- acceptor.accept( *stream.rdbuf(), ec );
- if (!ec)
- std::cout << stream.rdbuf();
- else
- {
- std::cerr << "链接已中断:" << stream.error().message() << std::endl;
- return 0;
- }
- }
- }
- else if ( argc == 3 && boost::is_iequal()(argv[1], (const char*)"client") == 0 )
- {
- tcp::iostream stream( argv[2], boost::lexical_cast<std::string>(PORT) );
- if (!stream)
- {
- std::cerr << "链接失败:" << stream.error().message() << std::endl;
- return 1;
- }
- std::string line;
- for (;;)
- {
- std::getline(std::cin, line);
- stream << line << std::endl;
- }
- }
- else
- {
- std::cerr << "用法:\n\t"<< argv[0] << " server / client <host>" << std::endl;
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement