[mbed::net] Not shutting down the socket, before closing it, is not robust in lwIP

pull/4/head
Emilio Monti 2013-05-14 10:41:47 +01:00
parent 3f96b401b0
commit be73e26366
5 changed files with 70 additions and 3 deletions

View File

@ -69,10 +69,12 @@ int Socket::wait_writable(TimeInterval& timeout) {
return select(&timeout._time, false, true);
}
int Socket::close() {
int Socket::close(bool shutdown) {
if (_sock_fd < 0)
return -1;
if (shutdown)
lwip_shutdown(_sock_fd, SHUT_RDWR);
lwip_close(_sock_fd);
_sock_fd = -1;

View File

@ -65,9 +65,10 @@ public:
*/
int get_option(int level, int optname, void *optval, socklen_t *optlen);
/** Close the socket file descriptor
/** Close the socket
\param shutdown free the left-over data in message queues
*/
int close();
int close(bool shutdown=true);
~Socket();

View File

@ -0,0 +1,34 @@
#include "mbed.h"
#include "EthernetInterface.h"
const char* ECHO_SERVER_ADDRESS = "10.2.200.57";
const int ECHO_PORT = 7;
int main() {
EthernetInterface eth;
eth.init(); //Use DHCP
eth.connect();
printf("IP Address is %s\n", eth.getIPAddress());
TCPSocketConnection socket;
while (true) {
while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) {
printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT);
wait(1);
}
char hello[] = "Hello World\n";
socket.send_all(hello, sizeof(hello) - 1);
char buf[256];
int n = socket.receive(buf, 256);
buf[n] = '\0';
printf("%s", buf);
socket.close();
wait(1);
}
eth.disconnect();
}

View File

@ -0,0 +1,24 @@
# Be sure that the tools directory is in the search path
import sys
from os.path import join, abspath, dirname
ROOT = abspath(join(dirname(__file__), "..", ".."))
sys.path.append(ROOT)
from workspace_tools.private_settings import LOCALHOST
from SocketServer import BaseRequestHandler, TCPServer
class TCP_EchoHandler(BaseRequestHandler):
def handle(self):
print "\nHandle connection from:", self.client_address
while True:
data = self.request.recv(1024)
if not data: break
self.request.sendall(data)
self.request.close()
print "socket closed"
if __name__ == '__main__':
server = TCPServer((LOCALHOST, 7), TCP_EchoHandler)
print "listening for connections on:", (LOCALHOST, 7)
server.serve_forever()

View File

@ -500,6 +500,12 @@ TESTS = [
"dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY],
"supported": CORTEX_ARM_SUPPORT,
},
{
"id": "NET_13", "description": "NET: TCP client echo loop",
"source_dir": join(TEST_DIR, "net", "echo", "tcp_client_loop"),
"dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY],
"supported": CORTEX_ARM_SUPPORT,
},
# Vodafone tests
{