You probably need to shutdown the socket nicely.

 

See bottom of http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload2.html

 

BR,

Martin

 

From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of William Riancho
Sent: Wednesday, May 18, 2016 05:25
To: boost-users@lists.boost.org
Subject: [Boost-users] Blocking sockets on Windows

 

Hello all,

 

I try to implement a simple http server with blocking sockets on Windows. Basically, I have a simple server that just write data to a socket when a network connection occurs before exit. The problem is that the last "socket.send" as no effect if I don't delay the process exit. Writing to this socket is supposed to block until all the data as been written.

I have tried to use the completion condition of write, to use the non_blocking method of the socket. I still get the same problem.

Note that the problem doesn't occur on Linux.

 

Here is the code:

 

```

#include <boost/asio.hpp>

 

int main(int argc, char *argv[]) {

char *address = "0.0.0.0";

char *port = "8180";

 

boost::asio::io_service io_service;

boost::asio::ip::tcp::acceptor acceptor(io_service);

boost::asio::ip::tcp::resolver resolver(io_service);

boost::asio::ip::tcp::resolver::query query(address, port);

boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);

acceptor.open(endpoint.protocol());

acceptor.bind(endpoint);

acceptor.listen();

boost::asio::ip::tcp::socket sock(io_service);

acceptor.accept(sock);

 

std::string body("Hello, World!");

sock.send(boost::asio::buffer(std::string("HTTP/1.1 200 OK\r\n")));

sock.send(boost::asio::buffer(std::string("Content-Length: ") + std::to_string(body.size()) + "\r\n\r\n"));

sock.send(boost::asio::buffer(body));

Sleep(1000); // The body would not be sent without this

return 0;

}

```