34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
66 panic(
"Too late to disable all listeners, already have a listener");
80 panic(
"Too late to bind to loopback, already have a listener");
99 #if defined(_GNU_SOURCE) && defined(SOCK_CLOEXEC)
100 return ::accept4(sockfd,
addr, addrlen, SOCK_CLOEXEC);
102 return ::accept(sockfd,
addr, addrlen);
111 : listening(false),
fd(-1)
125 panic(
"Socket already listening!");
131 panic(
"Can't create socket:%s !", strerror(errno));
136 if (::setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, (
char *)&
i,
138 panic(
"ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
141 struct sockaddr_in sockaddr;
142 sockaddr.sin_family = PF_INET;
143 sockaddr.sin_addr.s_addr =
145 sockaddr.sin_port = htons(port);
147 std::memset(&sockaddr.sin_zero, 0,
sizeof(sockaddr.sin_zero));
148 int ret = ::bind(
fd, (
struct sockaddr *)&sockaddr,
sizeof (sockaddr));
150 if (ret == -1 && errno != EADDRINUSE)
151 panic(
"ListenSocket(listen): bind() failed!");
156 if (errno != EADDRINUSE)
157 panic(
"ListenSocket(listen): listen() failed!");
173 struct sockaddr_in sockaddr;
174 socklen_t slen =
sizeof (sockaddr);
176 if (sfd != -1 && nodelay) {
178 if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (
char *)&
i,
180 warn(
"ListenSocket(accept): setsockopt() TCP_NODELAY failed!");