Boost logo

Boost Users :

Subject: [Boost-users] Blocking sockets on Windows
From: William Riancho (wr.wllm_at_[hidden])
Date: 2016-05-18 05:25:06

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 = "";
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);;
boost::asio::ip::tcp::socket sock(io_service);

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"));
Sleep(1000); // The body would not be sent without this
return 0;

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at