Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TCPSocketServer.cpp
00001 /* Copyright (C) 2013 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "TCPSocketServer.h" 00020 #include <string> 00021 00022 TCPSocketServer::TCPSocketServer() { 00023 00024 } 00025 00026 int TCPSocketServer::bind(int port) { 00027 if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) { 00028 return -1; 00029 } 00030 00031 sockaddr_in localHost; 00032 memset(&localHost, 0, sizeof(localHost)); 00033 00034 localHost.sin_family = AF_INET; 00035 localHost.sin_port = htons(port); 00036 localHost.sin_addr.s_addr = 0; 00037 00038 if (_cc3000_module->_socket.bind(_sock_fd, (const sockaddr *)&localHost, sizeof(localHost)) < 0) { 00039 close(); 00040 return -1; 00041 } 00042 00043 return 0; 00044 } 00045 00046 int TCPSocketServer::listen(int max) { 00047 if (_sock_fd < 0) { 00048 return -1; 00049 } 00050 00051 if (_cc3000_module->_socket.listen(_sock_fd, max) < 0) { 00052 close(); 00053 return -1; 00054 } 00055 00056 return 0; 00057 } 00058 00059 00060 int TCPSocketServer::accept(TCPSocketConnection& connection) { 00061 if (_sock_fd < 0) { 00062 return -1; 00063 } 00064 00065 if (!_blocking) { 00066 TimeInterval timeout(_timeout); 00067 if (wait_readable(timeout) != 0) { 00068 return -1; 00069 } 00070 } 00071 00072 connection.reset_address(); 00073 socklen_t newSockRemoteHostLen = sizeof(connection._remote_host); 00074 int fd = _cc3000_module->_socket.accept(_sock_fd, (sockaddr *) &connection._remote_host, &newSockRemoteHostLen); 00075 if (fd < 0) { 00076 return -1; 00077 } 00078 /* s_addr is returned in the little endian */ 00079 connection._remote_host.sin_addr.s_addr = htonl(connection._remote_host.sin_addr.s_addr); 00080 connection._sock_fd = fd; 00081 connection._is_connected = true; 00082 00083 return 0; 00084 } 00085
Generated on Tue Jul 12 2022 18:55:02 by
1.7.2