esemi00
Dependencies: mbed mbed-rtos EthernetInterface
Revision 0:449ed9227d4a, committed 2021-01-20
- Comitter:
- esemi00
- Date:
- Wed Jan 20 05:22:54 2021 +0000
- Commit message:
- esemi00
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Wed Jan 20 05:22:54 2021 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mainETH_TCP_SERVER.cpp Wed Jan 20 05:22:54 2021 +0000 @@ -0,0 +1,91 @@ +//eth_tcp_server.cpp +#include "mbed.h" +#include "EthernetInterface.h" + +#define HTTP_TCP_PORT 80 + +Serial pc(USBTX,USBRX); +EthernetInterface eth; + +void http(); + +int main() +{ + eth.init(); //Use DHCP + eth.connect(); + pc.printf("\nServer IP Address is %s\n", eth.getIPAddress()); + http(); + eth.disconnect(); +} + +void http() +{ + char buf[1024]; + char buf_s[2048]; + char meth_name[16]; + char uri_addr[256]; + char http_ver[64]; + int n; + + //ステップ1 ソケットの生成 + TCPSocketServer server; + TCPSocketConnection client; + + //ステップ2 ポートの登録 + server.bind(HTTP_TCP_PORT); + + //ステップ3 ソケットの待機準備 + server.listen(5); + + // ステップB HTTPレスポンス作成 + memset(buf_s, 0, sizeof(buf_s)); //HTTPレスポンス用バッファメモリを初期化 + snprintf(buf_s, sizeof(buf_s), //200のHTTPレスポンスの作成 + "HTTP/1.0 200 Document follows\r\n" //ステータス行 + "Content-Type: text/html\r\n" + "\r\n" //空白行(これ以降、送信したデータはボディ) + "HELLO\r\n"); + + while(true){ + printf("\n\nWait for new connection...\n"); + + //ステップ4 ソケットの接続受付 + server.accept(client); + client.set_blocking(false, 1500); + printf("Connection from: %s\n", client.get_address()); + + while (true) { + // ステップ5 データの受信 + n = client.receive(buf, sizeof(buf)); + pc.printf("%d byte\r\n", n); + if (n <= 0){ + // データがない場合 + break; + } else { + // データがある場合 + buf[n] = '\0'; + printf("Received message from Client :%s",buf); + + // ステップA HTTPリクエストのリクエスト行の分解 + sscanf(buf, "%s %s %s", meth_name, uri_addr, http_ver); + printf("meth_name:'%s'\n",meth_name);//メソッド(GETのみ) + printf("uri_addr:'%s'\n",uri_addr);//リクエスト対象(ファイル) + printf("http_ver:'%s'\n",http_ver);//HTTPのバージョン + + //ステップC HTTPレスポンスの送信 + if (strcmp(meth_name, "GET") != 0) { + //GET以外のとき + pc.printf("HTTP/1.0 501 Not Implemented\r\n"); + client.send("HTTP/1.0 501 Not Implemented", 28); + } else { + //GETのとき + pc.printf("\r\n%s", buf_s); + client.send(buf_s, (int)strlen(buf_s)); + } + } + } + + // ステップ7 ソケット間の通信接続を切断 + client.close(); + pc.printf("client close\r\n"); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Jan 20 05:22:54 2021 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jan 20 05:22:54 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/176b8275d35d \ No newline at end of file