Skip to content

Commit be73e26

Browse files
committed
[mbed::net] Not shutting down the socket, before closing it, is not robust in lwIP
1 parent 3f96b40 commit be73e26

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

libraries/net/lwip/Socket/Socket.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ int Socket::wait_writable(TimeInterval& timeout) {
6969
return select(&timeout._time, false, true);
7070
}
7171

72-
int Socket::close() {
72+
int Socket::close(bool shutdown) {
7373
if (_sock_fd < 0)
7474
return -1;
7575

76+
if (shutdown)
77+
lwip_shutdown(_sock_fd, SHUT_RDWR);
7678
lwip_close(_sock_fd);
7779
_sock_fd = -1;
7880

libraries/net/lwip/Socket/Socket.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ class Socket {
6565
*/
6666
int get_option(int level, int optname, void *optval, socklen_t *optlen);
6767

68-
/** Close the socket file descriptor
68+
/** Close the socket
69+
\param shutdown free the left-over data in message queues
6970
*/
70-
int close();
71+
int close(bool shutdown=true);
7172

7273
~Socket();
7374

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "mbed.h"
2+
#include "EthernetInterface.h"
3+
4+
const char* ECHO_SERVER_ADDRESS = "10.2.200.57";
5+
const int ECHO_PORT = 7;
6+
7+
int main() {
8+
EthernetInterface eth;
9+
eth.init(); //Use DHCP
10+
eth.connect();
11+
printf("IP Address is %s\n", eth.getIPAddress());
12+
13+
TCPSocketConnection socket;
14+
15+
while (true) {
16+
while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) {
17+
printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT);
18+
wait(1);
19+
}
20+
21+
char hello[] = "Hello World\n";
22+
socket.send_all(hello, sizeof(hello) - 1);
23+
24+
char buf[256];
25+
int n = socket.receive(buf, 256);
26+
buf[n] = '\0';
27+
printf("%s", buf);
28+
29+
socket.close();
30+
wait(1);
31+
}
32+
33+
eth.disconnect();
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Be sure that the tools directory is in the search path
2+
import sys
3+
from os.path import join, abspath, dirname
4+
ROOT = abspath(join(dirname(__file__), "..", ".."))
5+
sys.path.append(ROOT)
6+
7+
from workspace_tools.private_settings import LOCALHOST
8+
from SocketServer import BaseRequestHandler, TCPServer
9+
10+
11+
class TCP_EchoHandler(BaseRequestHandler):
12+
def handle(self):
13+
print "\nHandle connection from:", self.client_address
14+
while True:
15+
data = self.request.recv(1024)
16+
if not data: break
17+
self.request.sendall(data)
18+
self.request.close()
19+
print "socket closed"
20+
21+
if __name__ == '__main__':
22+
server = TCPServer((LOCALHOST, 7), TCP_EchoHandler)
23+
print "listening for connections on:", (LOCALHOST, 7)
24+
server.serve_forever()

workspace_tools/tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@
500500
"dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY],
501501
"supported": CORTEX_ARM_SUPPORT,
502502
},
503+
{
504+
"id": "NET_13", "description": "NET: TCP client echo loop",
505+
"source_dir": join(TEST_DIR, "net", "echo", "tcp_client_loop"),
506+
"dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY],
507+
"supported": CORTEX_ARM_SUPPORT,
508+
},
503509

504510
# Vodafone tests
505511
{

0 commit comments

Comments
 (0)