This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.
Dependents: Embedded_web EmailButton EmailButton HTTPClient_Weather ... more
TCPSocketServer.cpp
00001 /* Copyright (C) 2012 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 00021 TCPSocketServer::TCPSocketServer() {} 00022 00023 // Server initialization 00024 int TCPSocketServer::bind(int port) 00025 { 00026 00027 if (_sock_fd < 0) { 00028 _sock_fd = eth->new_socket(); 00029 if (_sock_fd < 0) { 00030 return -1; 00031 } 00032 } 00033 // set the listen_port for next connection. 00034 listen_port = port; 00035 // set TCP protocol 00036 eth->setProtocol(_sock_fd, TCP); 00037 // set local port 00038 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); 00039 // connect the network 00040 eth->scmd(_sock_fd, OPEN); 00041 return 0; 00042 } 00043 00044 int TCPSocketServer::listen(int backlog) 00045 { 00046 if (_sock_fd < 0) { 00047 return -1; 00048 } 00049 if (backlog != 1) { 00050 return -1; 00051 } 00052 eth->scmd(_sock_fd, LISTEN); 00053 return 0; 00054 } 00055 00056 00057 int TCPSocketServer::accept(TCPSocketConnection& connection) 00058 { 00059 if (_sock_fd < 0) { 00060 return -1; 00061 } 00062 Timer t; 00063 t.reset(); 00064 t.start(); 00065 while(1) { 00066 if (t.read_ms() > _timeout && _blocking == false) { 00067 return -1; 00068 } 00069 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) { 00070 break; 00071 } 00072 } 00073 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR); 00074 char host[16]; 00075 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00076 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT); 00077 // change this server socket to connection socket. 00078 connection._sock_fd = _sock_fd; 00079 connection._is_connected = true; 00080 connection.set_address(host, port); 00081 00082 // and then, for the next connection, server socket should be assigned new one. 00083 _sock_fd = -1; // want to assign new available _sock_fd. 00084 if(bind(listen_port) < 0) { 00085 // modified by Patrick Pollet 00086 error("No more socket for listening, bind error"); 00087 return -1; 00088 } else { 00089 //return -1; 00090 if(listen(1) < 0) { 00091 // modified by Patrick Pollet 00092 error("No more socket for listening, listen error"); 00093 return -1; 00094 } 00095 } 00096 00097 return 0; 00098 } 00099
Generated on Wed Jul 13 2022 02:51:31 by 1.7.2