* @brief Accept a connection and return a socket connected to the client. * * Waits for a client to connect and returns a pointer to a inet_stream object * which can be used to communicate with the client. * * @param numeric Specifies if the client's parameter (IP address, port) should * be delivered numerically in the src_host/src_port parameters. * @param accept_flags Flags specified in `
| 175 | * @returns A pointer to a connected TCP/IP client socket object. |
| 176 | */ |
| 177 | inet_stream* inet_stream_server::accept(int numeric, int accept_flags) { |
| 178 | if (sfd < 0) |
| 179 | throw socket_exception( |
| 180 | __FILE__, __LINE__, |
| 181 | "inet_stream_server::accept() - stream server socket is not in " |
| 182 | "listening state -- please call first setup()!"); |
| 183 | |
| 184 | using std::unique_ptr; |
| 185 | unique_ptr<char[]> src_host(new char[1024]); |
| 186 | unique_ptr<char[]> src_port(new char[32]); |
| 187 | |
| 188 | memset(src_host.get(), 0, 1024); |
| 189 | memset(src_port.get(), 0, 32); |
| 190 | |
| 191 | int client_sfd; |
| 192 | inet_stream* client = new inet_stream; |
| 193 | |
| 194 | if (-1 == (client_sfd = accept_inet_stream_socket(sfd, src_host.get(), 1023, |
| 195 | src_port.get(), 31, |
| 196 | numeric, accept_flags))) { |
| 197 | if (!is_nonblocking && errno != EWOULDBLOCK) { |
| 198 | throw socket_exception( |
| 199 | __FILE__, __LINE__, |
| 200 | "inet_stream_server::accept() - could not accept new " |
| 201 | "connection on stream server socket!"); |
| 202 | } else { |
| 203 | return nullptr; // Only return NULL but don't throw an exception if |
| 204 | // the socket is nonblocking |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | client->sfd = client_sfd; |
| 209 | client->host = |
| 210 | string(src_host.get()); // these strings are destructed automatically |
| 211 | // when the returned object is deleted. |
| 212 | // (http://stackoverflow.com/a/6256543) |
| 213 | client->port = string(src_port.get()); // |
| 214 | client->proto = proto; |
| 215 | |
| 216 | return client; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @brief Accept a connection and return a socket connected to the client. |
no test coverage detected