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");
89 : listening(false),
fd(-1)
103 panic(
"Socket already listening!");
107 fd = ::socket(PF_INET, SOCK_STREAM, 0);
109 panic(
"Can't create socket:%s !", strerror(errno));
114 if (::setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, (
char *)&
i,
116 panic(
"ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
119 struct sockaddr_in sockaddr;
120 sockaddr.sin_family = PF_INET;
121 sockaddr.sin_addr.s_addr =
123 sockaddr.sin_port = htons(port);
125 std::memset(&sockaddr.sin_zero, 0,
sizeof(sockaddr.sin_zero));
126 int ret = ::bind(
fd, (
struct sockaddr *)&sockaddr,
sizeof (sockaddr));
128 if (ret == -1 && errno != EADDRINUSE)
129 panic(
"ListenSocket(listen): bind() failed!");
134 if (errno != EADDRINUSE)
135 panic(
"ListenSocket(listen): listen() failed!");
151 struct sockaddr_in sockaddr;
152 socklen_t slen =
sizeof (sockaddr);
153 int sfd =
::accept(
fd, (
struct sockaddr *)&sockaddr, &slen);
154 if (sfd != -1 && nodelay) {
156 if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (
char *)&
i,
158 warn(
"ListenSocket(accept): setsockopt() TCP_NODELAY failed!");