1 /* Copyright (C) 2012 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 #include "TCPSocketServer.h"
25 TCPSocketServer::TCPSocketServer() {
29 int TCPSocketServer::bind(int port
) {
30 if (init_socket(SOCK_STREAM
) < 0)
33 struct sockaddr_in localHost
;
34 memset(&localHost
, 0, sizeof(localHost
));
36 localHost
.sin_family
= AF_INET
;
37 localHost
.sin_port
= htons(port
);
38 localHost
.sin_addr
.s_addr
= INADDR_ANY
;
40 if (lwip_bind(_sock_fd
, (const struct sockaddr
*) &localHost
, sizeof(localHost
)) < 0) {
48 int TCPSocketServer::listen(int max
) {
52 if (lwip_listen(_sock_fd
, max
) < 0) {
60 int TCPSocketServer::accept(TCPSocketConnection
& connection
) {
65 TimeInterval
timeout(_timeout
);
66 if (wait_readable(timeout
) != 0)
69 connection
.reset_address();
70 socklen_t newSockRemoteHostLen
= sizeof(connection
._remoteHost
);
71 int fd
= lwip_accept(_sock_fd
, (struct sockaddr
*) &connection
._remoteHost
, &newSockRemoteHostLen
);
73 return -1; //Accept failed
74 connection
._sock_fd
= fd
;
75 connection
._is_connected
= true;