mbed socket API

Dependents:   EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more

Deprecated

This is an mbed 2 sockets library. For mbed 5, network sockets have been revised to better support additional network stacks and thread safety here.

Committer:
emilmont
Date:
Mon Aug 13 09:38:00 2012 +0000
Revision:
15:41e3af01f58e
Parent:
11:3d83c348fb8b
Child:
16:2d471deff212
Reset TCPSocketConnection address when accepting a new connection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 3:e6474399e057 1 /* Copyright (C) 2012 mbed.org, MIT License
emilmont 3:e6474399e057 2 *
emilmont 3:e6474399e057 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
emilmont 3:e6474399e057 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
emilmont 3:e6474399e057 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
emilmont 3:e6474399e057 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
emilmont 3:e6474399e057 7 * furnished to do so, subject to the following conditions:
emilmont 3:e6474399e057 8 *
emilmont 3:e6474399e057 9 * The above copyright notice and this permission notice shall be included in all copies or
emilmont 3:e6474399e057 10 * substantial portions of the Software.
emilmont 3:e6474399e057 11 *
emilmont 3:e6474399e057 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
emilmont 3:e6474399e057 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
emilmont 3:e6474399e057 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
emilmont 3:e6474399e057 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 3:e6474399e057 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
emilmont 3:e6474399e057 17 */
emilmont 3:e6474399e057 18 #include "TCPSocketServer.h"
emilmont 3:e6474399e057 19
emilmont 3:e6474399e057 20 #include <cstring>
emilmont 3:e6474399e057 21
emilmont 3:e6474399e057 22 using std::memset;
emilmont 3:e6474399e057 23 using std::memcpy;
emilmont 3:e6474399e057 24
emilmont 3:e6474399e057 25 TCPSocketServer::TCPSocketServer() {
emilmont 3:e6474399e057 26
emilmont 3:e6474399e057 27 }
emilmont 3:e6474399e057 28
emilmont 3:e6474399e057 29 int TCPSocketServer::bind(int port) {
emilmont 5:300e7ad2dc1d 30 if (init_socket(SOCK_STREAM) < 0)
emilmont 3:e6474399e057 31 return -1;
emilmont 3:e6474399e057 32
emilmont 3:e6474399e057 33 struct sockaddr_in localHost;
emilmont 3:e6474399e057 34 memset(&localHost, 0, sizeof(localHost));
emilmont 3:e6474399e057 35
emilmont 3:e6474399e057 36 localHost.sin_family = AF_INET;
emilmont 3:e6474399e057 37 localHost.sin_port = htons(port);
emilmont 3:e6474399e057 38 localHost.sin_addr.s_addr = INADDR_ANY;
emilmont 3:e6474399e057 39
emilmont 3:e6474399e057 40 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
emilmont 3:e6474399e057 41 close();
emilmont 3:e6474399e057 42 return -1;
emilmont 3:e6474399e057 43 }
emilmont 3:e6474399e057 44
emilmont 3:e6474399e057 45 return 0;
emilmont 3:e6474399e057 46 }
emilmont 3:e6474399e057 47
emilmont 3:e6474399e057 48 int TCPSocketServer::listen(int max) {
emilmont 3:e6474399e057 49 if (_sock_fd < 0)
emilmont 3:e6474399e057 50 return -1;
emilmont 3:e6474399e057 51
emilmont 3:e6474399e057 52 if (lwip_listen(_sock_fd, max) < 0) {
emilmont 3:e6474399e057 53 close();
emilmont 3:e6474399e057 54 return -1;
emilmont 3:e6474399e057 55 }
emilmont 3:e6474399e057 56
emilmont 3:e6474399e057 57 return 0;
emilmont 3:e6474399e057 58 }
emilmont 3:e6474399e057 59
emilmont 10:d24738f4ef99 60 int TCPSocketServer::accept(TCPSocketConnection& connection) {
emilmont 3:e6474399e057 61 if (_sock_fd < 0)
emilmont 3:e6474399e057 62 return -1;
emilmont 3:e6474399e057 63
emilmont 10:d24738f4ef99 64 if (!_blocking) {
emilmont 10:d24738f4ef99 65 TimeInterval timeout(_timeout);
emilmont 10:d24738f4ef99 66 if (wait_readable(timeout) != 0)
emilmont 10:d24738f4ef99 67 return -1;
emilmont 10:d24738f4ef99 68 }
emilmont 15:41e3af01f58e 69 connection.reset_address();
emilmont 3:e6474399e057 70 socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost);
emilmont 3:e6474399e057 71 int fd = lwip_accept(_sock_fd, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen);
emilmont 3:e6474399e057 72 if (fd < 0)
emilmont 3:e6474399e057 73 return -1; //Accept failed
emilmont 3:e6474399e057 74 connection._sock_fd = fd;
emilmont 11:3d83c348fb8b 75 connection._is_connected = true;
emilmont 3:e6474399e057 76
emilmont 3:e6474399e057 77 return 0;
emilmont 3:e6474399e057 78 }