Skip to content

Commit b78df13

Browse files
Don't destroy HTTP server in main!
1 parent b4df318 commit b78df13

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/http_server.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ class HttpServer {
157157
nsapi_error_t accept_result;
158158
TCPSocket* clt_sock = server.accept(&accept_result);
159159

160+
SocketAddress peer_addr;
161+
if(clt_sock->getpeername(&peer_addr) == NSAPI_ERROR_OK)
162+
{
163+
printf("Incoming connection from %s", peer_addr.get_ip_address());
164+
}
165+
else
166+
{
167+
printf("Incoming connection, failed to get peer IP");
168+
}
169+
160170
rtos::ScopedMutexLock lock(thread_pool_mutex);
161171

162172
// Create new thread

src/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ int main() {
6161
return 1;
6262
}
6363

64-
HttpServer server(network);
65-
nsapi_error_t res = server.start(8080, &request_handler);
64+
// Construct a server, allowing it to persist in memory permanently
65+
HttpServer * server = new HttpServer(network);
66+
nsapi_error_t res = server->start(8080, &request_handler);
6667

6768
if (res == NSAPI_ERROR_OK) {
6869
SocketAddress ourAddr;
@@ -72,4 +73,6 @@ int main() {
7273
else {
7374
printf("Server could not be started... %d\n", res);
7475
}
76+
77+
// Main thread will now exit but server threads keep running
7578
}

0 commit comments

Comments
 (0)