Make server and client
This commit is contained in:
parent
6b2bf30b05
commit
08281c918d
|
@ -8,7 +8,49 @@ namespace net = boost::asio;
|
|||
namespace sys = boost::system;
|
||||
using namespace std;
|
||||
|
||||
void ProcessRequest(net::ip::tcp::socket& sock)
|
||||
{
|
||||
sys::error_code ec;
|
||||
net::streambuf request_buf;
|
||||
|
||||
net::read(sock, request_buf, ec);
|
||||
|
||||
if (ec != net::error::eof)
|
||||
{
|
||||
throw sys::system_error(ec);
|
||||
}
|
||||
|
||||
const char response_buf[] = {0x48, 0x69, 0x21};
|
||||
|
||||
net::write(sock, net::buffer(response_buf));
|
||||
|
||||
sock.shutdown(net::socket_base::shutdown_send);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const uint16_t port_num = 3333;
|
||||
|
||||
try
|
||||
{
|
||||
net::ip::tcp::endpoint ep(net::ip::address_v4::any(), port_num);
|
||||
|
||||
net::io_context ios;
|
||||
|
||||
net::ip::tcp::acceptor acceptor(ios, ep);
|
||||
|
||||
net::ip::tcp::socket sock(ios);
|
||||
|
||||
acceptor.accept(sock);
|
||||
|
||||
ProcessRequest(sock);
|
||||
}
|
||||
catch (const sys::system_error& err)
|
||||
{
|
||||
cout << "Error!" << err.what() << endl;
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue