forked from MaJerle/lwesp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add single_thread_single_client example
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef __NETCONN_SERVER_1THREAD_H | ||
#define __NETCONN_SERVER_1THREAD_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
void netconn_server_1thread_thread(void* arg); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Netconn server example is based on single thread | ||
* and it listens for single client only on port 23 | ||
*/ | ||
#include "netconn_server_1thread.h" | ||
#include "esp/esp.h" | ||
|
||
/** | ||
* \brief Basic thread for netconn server to test connections | ||
* \param[in] arg: User argument | ||
*/ | ||
void | ||
netconn_server_1thread_thread(void* arg) { | ||
espr_t res; | ||
esp_netconn_p server, client; | ||
esp_pbuf_p p; | ||
|
||
/* Create netconn for server */ | ||
server = esp_netconn_new(ESP_NETCONN_TYPE_TCP); | ||
if (server == NULL) { | ||
printf("Cannot create server netconn!\r\n"); | ||
} | ||
|
||
/* Bind it to port 23 */ | ||
res = esp_netconn_bind(server, 23); | ||
if (res != espOK) { | ||
printf("Cannot bind server\r\n"); | ||
goto out; | ||
} | ||
|
||
/* Start listening for incoming connections with maximal 1 client */ | ||
res = esp_netconn_listen_with_max_conn(server, 1); | ||
if (res != espOK) { | ||
goto out; | ||
} | ||
|
||
/* Unlimited loop */ | ||
while (1) { | ||
/* Accept new client */ | ||
res = esp_netconn_accept(server, &client); | ||
if (res != espOK) { | ||
break; | ||
} | ||
printf("New client accepted!\r\n"); | ||
while (1) { | ||
/* Receive data */ | ||
res = esp_netconn_receive(client, &p); | ||
if (res == espOK) { | ||
printf("Data received!\r\n"); | ||
esp_pbuf_free(p); | ||
} else { | ||
printf("Netconn receive returned: %d\r\n", (int)res); | ||
if (res == espCLOSED) { | ||
printf("Connection closed by client\r\n"); | ||
break; | ||
} | ||
} | ||
} | ||
/* Delete client */ | ||
if (client != NULL) { | ||
esp_netconn_delete(client); | ||
client = NULL; | ||
} | ||
} | ||
/* Delete client */ | ||
if (client != NULL) { | ||
esp_netconn_delete(client); | ||
client = NULL; | ||
} | ||
|
||
out: | ||
printf("Terminating netconn thread!\r\n"); | ||
if (server != NULL) { | ||
esp_netconn_delete(server); | ||
} | ||
esp_sys_thread_terminate(NULL); | ||
} |