![](/media/cache/group/logo_IcAX7cw.png.50x50_q85.png)
tcp example
main.cpp@0:4e0f31b64b24, 2022-01-19 (annotated)
- Committer:
- minamikawa
- Date:
- Wed Jan 19 08:31:23 2022 +0000
- Revision:
- 0:4e0f31b64b24
first_commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
minamikawa | 0:4e0f31b64b24 | 1 | |
minamikawa | 0:4e0f31b64b24 | 2 | |
minamikawa | 0:4e0f31b64b24 | 3 | #include "mbed.h" //MbedOS 5.15 |
minamikawa | 0:4e0f31b64b24 | 4 | #include "EthernetInterface.h" |
minamikawa | 0:4e0f31b64b24 | 5 | |
minamikawa | 0:4e0f31b64b24 | 6 | #define BUFFSIZE 50 |
minamikawa | 0:4e0f31b64b24 | 7 | |
minamikawa | 0:4e0f31b64b24 | 8 | #define PORT 80 |
minamikawa | 0:4e0f31b64b24 | 9 | |
minamikawa | 0:4e0f31b64b24 | 10 | DigitalOut led1(LED1); |
minamikawa | 0:4e0f31b64b24 | 11 | |
minamikawa | 0:4e0f31b64b24 | 12 | EthernetInterface net; |
minamikawa | 0:4e0f31b64b24 | 13 | TCPSocket server; |
minamikawa | 0:4e0f31b64b24 | 14 | TCPSocket* client; |
minamikawa | 0:4e0f31b64b24 | 15 | SocketAddress clientAddress; |
minamikawa | 0:4e0f31b64b24 | 16 | |
minamikawa | 0:4e0f31b64b24 | 17 | char *in_buffer; |
minamikawa | 0:4e0f31b64b24 | 18 | char *out_buffer; |
minamikawa | 0:4e0f31b64b24 | 19 | |
minamikawa | 0:4e0f31b64b24 | 20 | static const char* mbedIP = "192.168.0.32"; //IP |
minamikawa | 0:4e0f31b64b24 | 21 | static const char* mbedMask = "255.255.255.0"; // Mask |
minamikawa | 0:4e0f31b64b24 | 22 | static const char* mbedGateway = "192.168.0.0"; //Gateway |
minamikawa | 0:4e0f31b64b24 | 23 | |
minamikawa | 0:4e0f31b64b24 | 24 | |
minamikawa | 0:4e0f31b64b24 | 25 | int main (void){ |
minamikawa | 0:4e0f31b64b24 | 26 | printf("TCP Echo starting...\n"); |
minamikawa | 0:4e0f31b64b24 | 27 | int net_stat; |
minamikawa | 0:4e0f31b64b24 | 28 | //#ifndef ROUTER |
minamikawa | 0:4e0f31b64b24 | 29 | net.disconnect(); |
minamikawa | 0:4e0f31b64b24 | 30 | net_stat = net.set_network(mbedIP,mbedMask,mbedGateway); //set ip, mask, gateway to net |
minamikawa | 0:4e0f31b64b24 | 31 | printf("set IP status: %i\n",net_stat); |
minamikawa | 0:4e0f31b64b24 | 32 | //#endif |
minamikawa | 0:4e0f31b64b24 | 33 | net_stat = net.connect(); //connect to network |
minamikawa | 0:4e0f31b64b24 | 34 | printf("connect status: %i\n",net_stat); |
minamikawa | 0:4e0f31b64b24 | 35 | |
minamikawa | 0:4e0f31b64b24 | 36 | SocketAddress ip; //make socket for local ip address |
minamikawa | 0:4e0f31b64b24 | 37 | net.get_ip_address(&ip); //get a info from net |
minamikawa | 0:4e0f31b64b24 | 38 | const char *p_ip = ip.get_ip_address(); |
minamikawa | 0:4e0f31b64b24 | 39 | printf("IP address: %s and Port: %d\n", p_ip ? p_ip : "None" , PORT ); |
minamikawa | 0:4e0f31b64b24 | 40 | SocketAddress mask; |
minamikawa | 0:4e0f31b64b24 | 41 | net.get_netmask(&mask); |
minamikawa | 0:4e0f31b64b24 | 42 | const char *p_mask = mask.get_ip_address(); |
minamikawa | 0:4e0f31b64b24 | 43 | printf("Netmask: %s\n", p_mask ? p_mask : "None"); |
minamikawa | 0:4e0f31b64b24 | 44 | SocketAddress gateway; |
minamikawa | 0:4e0f31b64b24 | 45 | net.get_gateway(&gateway); |
minamikawa | 0:4e0f31b64b24 | 46 | const char *p_gateway = gateway.get_ip_address(); |
minamikawa | 0:4e0f31b64b24 | 47 | printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); |
minamikawa | 0:4e0f31b64b24 | 48 | |
minamikawa | 0:4e0f31b64b24 | 49 | if(ip){ |
minamikawa | 0:4e0f31b64b24 | 50 | server.open(&net); //open a network socket using info of net |
minamikawa | 0:4e0f31b64b24 | 51 | server.bind(PORT); //bind the socket to a port which receive data |
minamikawa | 0:4e0f31b64b24 | 52 | server.listen(1); //prepare for incoming connections |
minamikawa | 0:4e0f31b64b24 | 53 | while (1) { |
minamikawa | 0:4e0f31b64b24 | 54 | |
minamikawa | 0:4e0f31b64b24 | 55 | printf("Server bound and listening\n"); |
minamikawa | 0:4e0f31b64b24 | 56 | client = server.accept(); //wait until connection has done |
minamikawa | 0:4e0f31b64b24 | 57 | client->set_blocking(false); |
minamikawa | 0:4e0f31b64b24 | 58 | client->getpeername(&clientAddress); |
minamikawa | 0:4e0f31b64b24 | 59 | const char *p_clientAddress = clientAddress.get_ip_address(); |
minamikawa | 0:4e0f31b64b24 | 60 | printf("Client connected from IP address: %s\n", p_clientAddress ? p_clientAddress : "None"); |
minamikawa | 0:4e0f31b64b24 | 61 | |
minamikawa | 0:4e0f31b64b24 | 62 | bool b = true; |
minamikawa | 0:4e0f31b64b24 | 63 | while (b) { |
minamikawa | 0:4e0f31b64b24 | 64 | in_buffer = new char[BUFFSIZE]; |
minamikawa | 0:4e0f31b64b24 | 65 | int n = client->recv(in_buffer, BUFFSIZE); |
minamikawa | 0:4e0f31b64b24 | 66 | |
minamikawa | 0:4e0f31b64b24 | 67 | printf("%d\n", n); |
minamikawa | 0:4e0f31b64b24 | 68 | |
minamikawa | 0:4e0f31b64b24 | 69 | if (n == 0) { |
minamikawa | 0:4e0f31b64b24 | 70 | printf("Client disconnected\n"); |
minamikawa | 0:4e0f31b64b24 | 71 | b = false; |
minamikawa | 0:4e0f31b64b24 | 72 | }else if(n == NSAPI_ERROR_WOULD_BLOCK){ |
minamikawa | 0:4e0f31b64b24 | 73 | //printf("data is not here\n"); |
minamikawa | 0:4e0f31b64b24 | 74 | }else{ |
minamikawa | 0:4e0f31b64b24 | 75 | in_buffer[n] = '\0'; |
minamikawa | 0:4e0f31b64b24 | 76 | printf("Received message from Client :'%s'\n", in_buffer); |
minamikawa | 0:4e0f31b64b24 | 77 | out_buffer = new char[BUFFSIZE]; |
minamikawa | 0:4e0f31b64b24 | 78 | n = sprintf(out_buffer,"Echo - %s", in_buffer); |
minamikawa | 0:4e0f31b64b24 | 79 | |
minamikawa | 0:4e0f31b64b24 | 80 | printf("Sending echo to client\n"); |
minamikawa | 0:4e0f31b64b24 | 81 | //client->send(out_buffer, n); |
minamikawa | 0:4e0f31b64b24 | 82 | led1 =! led1; |
minamikawa | 0:4e0f31b64b24 | 83 | delete []out_buffer; |
minamikawa | 0:4e0f31b64b24 | 84 | } |
minamikawa | 0:4e0f31b64b24 | 85 | led1 =! led1; |
minamikawa | 0:4e0f31b64b24 | 86 | delete []in_buffer; |
minamikawa | 0:4e0f31b64b24 | 87 | wait_us(10000); |
minamikawa | 0:4e0f31b64b24 | 88 | } |
minamikawa | 0:4e0f31b64b24 | 89 | client->close(); |
minamikawa | 0:4e0f31b64b24 | 90 | } |
minamikawa | 0:4e0f31b64b24 | 91 | }else{ |
minamikawa | 0:4e0f31b64b24 | 92 | printf("No IP\n"); |
minamikawa | 0:4e0f31b64b24 | 93 | net.disconnect(); |
minamikawa | 0:4e0f31b64b24 | 94 | printf("Program end\n"); |
minamikawa | 0:4e0f31b64b24 | 95 | } |
minamikawa | 0:4e0f31b64b24 | 96 | } |