changeset 293:9b3270513f40

Socket: accept() can also block
author David Demelier <markand@malikania.fr>
date Thu, 13 Nov 2014 21:03:12 +0100
parents f4fc723429fe
children 434a6a289206
files C++/Socket.cpp C++/Tests/Sockets/main.cpp
diffstat 2 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Socket.cpp	Thu Nov 13 13:21:53 2014 +0100
+++ b/C++/Socket.cpp	Thu Nov 13 21:03:12 2014 +0100
@@ -196,8 +196,19 @@
 	addrlen = sizeof (sockaddr_storage);
 	handle = ::accept(s.handle(), (sockaddr *)&address, &addrlen);
 
-	if (handle == INVALID_SOCKET)
+	if (handle == INVALID_SOCKET) {
+#if defined(_WIN32)
+		if (WSAGetLastError() == WSAEWOULDBLOCK)
+			throw error::WouldBlock("accept");
+
 		throw error::Failure("accept", Socket::syserror());
+#else
+		if (errno == EAGAIN || errno == EWOULDBLOCK)
+			throw error::WouldBlock("accept");
+
+		throw error::Failure("accept", Socket::syserror());
+#endif
+	}
 
 	// Usually accept works only with SOCK_STREAM
 	info = SocketAddress(address, addrlen);
--- a/C++/Tests/Sockets/main.cpp	Thu Nov 13 13:21:53 2014 +0100
+++ b/C++/Tests/Sockets/main.cpp	Thu Nov 13 21:03:12 2014 +0100
@@ -298,7 +298,6 @@
 					if (tries >= 10)
 						running = false;
 				} catch (const Timeout &) {
-					puts("DEBUG: TIMEOUT");
 				}
 			}
 		} catch (const std::exception &ex) {