Jan Kamidra
/
TCP_EchoServer
Example for TCPSocket as EchoServer
main.cpp@0:633c10a3bb5d, 2020-05-08 (annotated)
- Committer:
- JohnnyK
- Date:
- Fri May 08 18:47:52 2020 +0000
- Revision:
- 0:633c10a3bb5d
- Child:
- 1:8c10d8f4e3aa
First
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 0:633c10a3bb5d | 1 | #include "mbed.h" //MbedOS 5.15 |
JohnnyK | 0:633c10a3bb5d | 2 | #include "EthernetInterface.h" |
JohnnyK | 0:633c10a3bb5d | 3 | |
JohnnyK | 0:633c10a3bb5d | 4 | #define ROUTER // commented = Static IP uncomented = address assigned by router |
JohnnyK | 0:633c10a3bb5d | 5 | #ifndef ROUTER |
JohnnyK | 0:633c10a3bb5d | 6 | #define IP "192.168.1.1" //Here place your static IP of Mbed |
JohnnyK | 0:633c10a3bb5d | 7 | #define GATEWAY "0.0.0.0" |
JohnnyK | 0:633c10a3bb5d | 8 | #define MASK "255.255.255.0" |
JohnnyK | 0:633c10a3bb5d | 9 | #endif |
JohnnyK | 0:633c10a3bb5d | 10 | #define PORT 20 |
JohnnyK | 0:633c10a3bb5d | 11 | |
JohnnyK | 0:633c10a3bb5d | 12 | DigitalOut led1(LED1); |
JohnnyK | 0:633c10a3bb5d | 13 | |
JohnnyK | 0:633c10a3bb5d | 14 | EthernetInterface eth; |
JohnnyK | 0:633c10a3bb5d | 15 | TCPSocket server; |
JohnnyK | 0:633c10a3bb5d | 16 | TCPSocket* client; |
JohnnyK | 0:633c10a3bb5d | 17 | SocketAddress clientAddress; |
JohnnyK | 0:633c10a3bb5d | 18 | |
JohnnyK | 0:633c10a3bb5d | 19 | int main (void){ |
JohnnyK | 0:633c10a3bb5d | 20 | printf("TCP Echo starting...\n"); |
JohnnyK | 0:633c10a3bb5d | 21 | int eth_stat; |
JohnnyK | 0:633c10a3bb5d | 22 | #ifndef ROUTER |
JohnnyK | 0:633c10a3bb5d | 23 | eth.disconnect(); |
JohnnyK | 0:633c10a3bb5d | 24 | eth_stat = eth.set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY); |
JohnnyK | 0:633c10a3bb5d | 25 | printf("set IP status: %i\n",eth_stat); |
JohnnyK | 0:633c10a3bb5d | 26 | #endif |
JohnnyK | 0:633c10a3bb5d | 27 | eth_stat = eth.connect(); |
JohnnyK | 0:633c10a3bb5d | 28 | printf("connect status: %i\n",eth_stat); |
JohnnyK | 0:633c10a3bb5d | 29 | |
JohnnyK | 0:633c10a3bb5d | 30 | SocketAddress ip; |
JohnnyK | 0:633c10a3bb5d | 31 | eth.get_ip_address(&ip); |
JohnnyK | 0:633c10a3bb5d | 32 | const char *p_ip = ip.get_ip_address(); |
JohnnyK | 0:633c10a3bb5d | 33 | printf("IP address: %s and Port: %d\n", p_ip ? p_ip : "None" , PORT ); |
JohnnyK | 0:633c10a3bb5d | 34 | SocketAddress mask; |
JohnnyK | 0:633c10a3bb5d | 35 | eth.get_netmask(&mask); |
JohnnyK | 0:633c10a3bb5d | 36 | const char *p_mask = mask.get_ip_address(); |
JohnnyK | 0:633c10a3bb5d | 37 | printf("Netmask: %s\n", p_mask ? p_mask : "None"); |
JohnnyK | 0:633c10a3bb5d | 38 | SocketAddress gateway; |
JohnnyK | 0:633c10a3bb5d | 39 | eth.get_gateway(&gateway); |
JohnnyK | 0:633c10a3bb5d | 40 | const char *p_gateway = gateway.get_ip_address(); |
JohnnyK | 0:633c10a3bb5d | 41 | printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); |
JohnnyK | 0:633c10a3bb5d | 42 | |
JohnnyK | 0:633c10a3bb5d | 43 | if(ip){ |
JohnnyK | 0:633c10a3bb5d | 44 | server.open(ð); |
JohnnyK | 0:633c10a3bb5d | 45 | server.bind(PORT); |
JohnnyK | 0:633c10a3bb5d | 46 | server.listen(1); |
JohnnyK | 0:633c10a3bb5d | 47 | while (1) { |
JohnnyK | 0:633c10a3bb5d | 48 | printf("Server bound and listening\n"); |
JohnnyK | 0:633c10a3bb5d | 49 | client = server.accept(); |
JohnnyK | 0:633c10a3bb5d | 50 | |
JohnnyK | 0:633c10a3bb5d | 51 | client->getpeername(&clientAddress); |
JohnnyK | 0:633c10a3bb5d | 52 | const char *p_clientAddress = clientAddress.get_ip_address(); |
JohnnyK | 0:633c10a3bb5d | 53 | printf("Client connected from IP address: %s\n", p_clientAddress ? p_clientAddress : "None"); |
JohnnyK | 0:633c10a3bb5d | 54 | |
JohnnyK | 0:633c10a3bb5d | 55 | bool b = true; |
JohnnyK | 0:633c10a3bb5d | 56 | while (b) { |
JohnnyK | 0:633c10a3bb5d | 57 | char in_buffer[100]; |
JohnnyK | 0:633c10a3bb5d | 58 | int n = client->recv(in_buffer, sizeof(in_buffer)); |
JohnnyK | 0:633c10a3bb5d | 59 | if (n == 0) { |
JohnnyK | 0:633c10a3bb5d | 60 | printf("Client disconnected\n"); |
JohnnyK | 0:633c10a3bb5d | 61 | b = false; |
JohnnyK | 0:633c10a3bb5d | 62 | }else{ |
JohnnyK | 0:633c10a3bb5d | 63 | printf("Received message from Client :'%s'\n", in_buffer); |
JohnnyK | 0:633c10a3bb5d | 64 | char out_buffer[100]; |
JohnnyK | 0:633c10a3bb5d | 65 | n = sprintf(out_buffer,"Echo - %s", in_buffer); |
JohnnyK | 0:633c10a3bb5d | 66 | |
JohnnyK | 0:633c10a3bb5d | 67 | printf("Sending echo to client\n"); |
JohnnyK | 0:633c10a3bb5d | 68 | client->send(out_buffer, n); |
JohnnyK | 0:633c10a3bb5d | 69 | led1 =! led1; |
JohnnyK | 0:633c10a3bb5d | 70 | } |
JohnnyK | 0:633c10a3bb5d | 71 | led1 =! led1; |
JohnnyK | 0:633c10a3bb5d | 72 | } |
JohnnyK | 0:633c10a3bb5d | 73 | client->close(); |
JohnnyK | 0:633c10a3bb5d | 74 | } |
JohnnyK | 0:633c10a3bb5d | 75 | }else{ |
JohnnyK | 0:633c10a3bb5d | 76 | printf("No IP\n"); |
JohnnyK | 0:633c10a3bb5d | 77 | eth.disconnect(); |
JohnnyK | 0:633c10a3bb5d | 78 | printf("Program end\n"); |
JohnnyK | 0:633c10a3bb5d | 79 | } |
JohnnyK | 0:633c10a3bb5d | 80 | } |