34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
63 panic(
"Too late to disable all listeners, already have a listener");
77 panic(
"Too late to bind to loopback, already have a listener");
86 : listening(false),
fd(-1)
100 panic(
"Socket already listening!");
104 fd = ::socket(PF_INET, SOCK_STREAM, 0);
106 panic(
"Can't create socket:%s !", strerror(errno));
111 if (::setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, (
char *)&
i,
113 panic(
"ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
116 struct sockaddr_in sockaddr;
117 sockaddr.sin_family = PF_INET;
118 sockaddr.sin_addr.s_addr =
120 sockaddr.sin_port = htons(port);
122 std::memset(&sockaddr.sin_zero, 0,
sizeof(sockaddr.sin_zero));
123 int ret = ::bind(
fd, (
struct sockaddr *)&sockaddr,
sizeof (sockaddr));
125 if (ret == -1 && errno != EADDRINUSE)
126 panic(
"ListenSocket(listen): bind() failed!");
131 if (errno != EADDRINUSE)
132 panic(
"ListenSocket(listen): listen() failed!");
148 struct sockaddr_in sockaddr;
149 socklen_t slen =
sizeof (sockaddr);
150 int sfd =
::accept(
fd, (
struct sockaddr *)&sockaddr, &slen);
151 if (sfd != -1 && nodelay) {
153 if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (
char *)&
i,
155 warn(
"ListenSocket(accept): setsockopt() TCP_NODELAY failed!");