189{
190
191 close(m_socket);
192
193
194 if (m_listener <= 0) {
195 B2RESULT("Started listening for new clients");
196 struct sockaddr_in sa;
197 bzero(&sa, sizeof(struct sockaddr_in));
198
199 sa.sin_family = AF_INET;
200 sa.sin_port = htons(port);
201
202 if ((m_listener = ::socket(AF_INET, SOCK_STREAM, 0)) < 0) {
203 B2ERROR("Socket initialization failed: " << strerror(errno));
204 return false;
205 }
206
207 int optval = 1;
208 setsockopt(m_listener, SOL_SOCKET, SO_REUSEADDR, &optval, 4);
209
210 int sizeval = D2_SOCKBUF_SIZE;
211 setsockopt(m_listener, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
212 setsockopt(m_listener, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
213
214 signal(SIGPIPE, SIG_IGN);
215
216 if ((bind(m_listener, (struct sockaddr*) &sa, sizeof(sa))) < 0) {
217 B2ERROR("Socket binding failed: " << strerror(errno));
218 return false;
219 }
220
221 listen(m_listener, 3);
222 }
223
224 struct sockaddr_in isa;
225 socklen_t i = sizeof(isa);
226 getsockname(m_listener, (struct sockaddr*)&isa, &i);
227
228 B2RESULT("Started accepting new clients");
229 if ((m_socket =::accept(m_listener, (struct sockaddr*)&isa, &i)) < 0) {
230 B2ERROR("Socket accepting failed: " << strerror(errno));
231 return false;
232 }
233
234 B2RESULT("Accepted connection with socket: " << m_socket);
235 m_initialized = true;
236 return true;
237}