EthernetInterface, TCP Echo Server, support multi session
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
mbed RTOS により、マルチセッションに対応した TCP Echo Server です。
複数のTCP接続を受付け、処理することができます。
accept の後に fork (スレッドを生成)しています。
main.cpp
- Committer:
- okini3939
- Date:
- 2013-11-01
- Revision:
- 7:0a3cf89d5b93
- Parent:
- 3:36fd3cfad85a
- Child:
- 8:46437b951a2f
File content as of revision 7:0a3cf89d5b93:
#include "mbed.h" #include "rtos.h" #include "EthernetInterface.h" #define ECHO_SERVER_PORT 10000 #define THREAD_MAX 10 Thread *threads[THREAD_MAX]; void child (void const *arg) { int i; char buf[256]; TCPSocketConnection *client = (TCPSocketConnection*)arg; printf("Connection from %s\r\n", client->get_address()); // client->set_blocking(false, 1500); // Timeout after (1.5)s for (;;) { i = client->receive(buf, sizeof(buf)); if (i < 0) break; if (i == 0) continue; buf[i] = 0; printf("Recv '%s'\r\n", buf); if (client->send(buf, i) < 0) break; } client->close(); printf("Close %s\r\n", client->get_address()); } void parent () { int i, t = 0; TCPSocketConnection *client; for (i = 0; i < THREAD_MAX; i ++) { threads[i] = NULL; } TCPSocketServer server; server.bind(ECHO_SERVER_PORT); server.listen(); printf("Wait for new connection...\r\n"); for (;;) { if (t >= 0) { client = new TCPSocketConnection; server.accept(*client); // fork child process threads[t] = new Thread(child, (void*)client); printf("Forked %d\r\n", t); } t = -1; for (i = 0; i < THREAD_MAX; i ++) { if (threads[i]) { if (threads[i]->get_state() == Thread::Inactive) { // terminate process // delete threads[i]; // threads[i] = NULL; printf("Terminated %d\r\n", i); } } if (threads[i] == NULL) { if (t < 0) t = i; // empty thread } } } } int main () { EthernetInterface eth; printf("TCP Echo Server with Fork...\r\n"); eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\r\n", eth.getIPAddress()); parent(); }