Example for TCPSocket as EchoServer

Committer:
JohnnyK
Date:
Sat May 09 09:16:53 2020 +0000
Revision:
1:8c10d8f4e3aa
Parent:
0:633c10a3bb5d
Correction of buffers and rework to Network Interface instead of Ethernet Interface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 0:633c10a3bb5d 1 #include "mbed.h" //MbedOS 5.15
JohnnyK 1:8c10d8f4e3aa 2 #include "NetworkInterface.h"
JohnnyK 1:8c10d8f4e3aa 3
JohnnyK 1:8c10d8f4e3aa 4 #define BUFFSIZE 50
JohnnyK 0:633c10a3bb5d 5
JohnnyK 0:633c10a3bb5d 6 #define ROUTER // commented = Static IP uncomented = address assigned by router
JohnnyK 0:633c10a3bb5d 7 #ifndef ROUTER
JohnnyK 0:633c10a3bb5d 8 #define IP "192.168.1.1" //Here place your static IP of Mbed
JohnnyK 0:633c10a3bb5d 9 #define GATEWAY "0.0.0.0"
JohnnyK 0:633c10a3bb5d 10 #define MASK "255.255.255.0"
JohnnyK 0:633c10a3bb5d 11 #endif
JohnnyK 0:633c10a3bb5d 12 #define PORT 20
JohnnyK 1:8c10d8f4e3aa 13
JohnnyK 0:633c10a3bb5d 14 DigitalOut led1(LED1);
JohnnyK 1:8c10d8f4e3aa 15
JohnnyK 1:8c10d8f4e3aa 16 NetworkInterface *net = NetworkInterface::get_default_instance();
JohnnyK 0:633c10a3bb5d 17 TCPSocket server;
JohnnyK 0:633c10a3bb5d 18 TCPSocket* client;
JohnnyK 0:633c10a3bb5d 19 SocketAddress clientAddress;
JohnnyK 1:8c10d8f4e3aa 20
JohnnyK 1:8c10d8f4e3aa 21 char *in_buffer;
JohnnyK 1:8c10d8f4e3aa 22 char *out_buffer;
JohnnyK 0:633c10a3bb5d 23
JohnnyK 0:633c10a3bb5d 24 int main (void){
JohnnyK 0:633c10a3bb5d 25 printf("TCP Echo starting...\n");
JohnnyK 1:8c10d8f4e3aa 26 int net_stat;
JohnnyK 0:633c10a3bb5d 27 #ifndef ROUTER
JohnnyK 1:8c10d8f4e3aa 28 net->disconnect();
JohnnyK 1:8c10d8f4e3aa 29 net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
JohnnyK 1:8c10d8f4e3aa 30 printf("set IP status: %i\n",net_stat);
JohnnyK 0:633c10a3bb5d 31 #endif
JohnnyK 1:8c10d8f4e3aa 32 net_stat = net->connect();
JohnnyK 1:8c10d8f4e3aa 33 printf("connect status: %i\n",net_stat);
JohnnyK 1:8c10d8f4e3aa 34
JohnnyK 0:633c10a3bb5d 35 SocketAddress ip;
JohnnyK 1:8c10d8f4e3aa 36 net->get_ip_address(&ip);
JohnnyK 0:633c10a3bb5d 37 const char *p_ip = ip.get_ip_address();
JohnnyK 0:633c10a3bb5d 38 printf("IP address: %s and Port: %d\n", p_ip ? p_ip : "None" , PORT );
JohnnyK 0:633c10a3bb5d 39 SocketAddress mask;
JohnnyK 1:8c10d8f4e3aa 40 net->get_netmask(&mask);
JohnnyK 0:633c10a3bb5d 41 const char *p_mask = mask.get_ip_address();
JohnnyK 0:633c10a3bb5d 42 printf("Netmask: %s\n", p_mask ? p_mask : "None");
JohnnyK 0:633c10a3bb5d 43 SocketAddress gateway;
JohnnyK 1:8c10d8f4e3aa 44 net->get_gateway(&gateway);
JohnnyK 0:633c10a3bb5d 45 const char *p_gateway = gateway.get_ip_address();
JohnnyK 0:633c10a3bb5d 46 printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
JohnnyK 0:633c10a3bb5d 47
JohnnyK 0:633c10a3bb5d 48 if(ip){
JohnnyK 1:8c10d8f4e3aa 49 server.open(net);
JohnnyK 0:633c10a3bb5d 50 server.bind(PORT);
JohnnyK 0:633c10a3bb5d 51 server.listen(1);
JohnnyK 0:633c10a3bb5d 52 while (1) {
JohnnyK 0:633c10a3bb5d 53 printf("Server bound and listening\n");
JohnnyK 0:633c10a3bb5d 54 client = server.accept();
JohnnyK 1:8c10d8f4e3aa 55
JohnnyK 0:633c10a3bb5d 56 client->getpeername(&clientAddress);
JohnnyK 0:633c10a3bb5d 57 const char *p_clientAddress = clientAddress.get_ip_address();
JohnnyK 0:633c10a3bb5d 58 printf("Client connected from IP address: %s\n", p_clientAddress ? p_clientAddress : "None");
JohnnyK 0:633c10a3bb5d 59
JohnnyK 0:633c10a3bb5d 60 bool b = true;
JohnnyK 0:633c10a3bb5d 61 while (b) {
JohnnyK 1:8c10d8f4e3aa 62 in_buffer = new char[BUFFSIZE];
JohnnyK 1:8c10d8f4e3aa 63 int n = client->recv(in_buffer, BUFFSIZE);
JohnnyK 0:633c10a3bb5d 64 if (n == 0) {
JohnnyK 0:633c10a3bb5d 65 printf("Client disconnected\n");
JohnnyK 0:633c10a3bb5d 66 b = false;
JohnnyK 0:633c10a3bb5d 67 }else{
JohnnyK 1:8c10d8f4e3aa 68 in_buffer[n] = '\0';
JohnnyK 0:633c10a3bb5d 69 printf("Received message from Client :'%s'\n", in_buffer);
JohnnyK 1:8c10d8f4e3aa 70 out_buffer = new char[BUFFSIZE];
JohnnyK 0:633c10a3bb5d 71 n = sprintf(out_buffer,"Echo - %s", in_buffer);
JohnnyK 0:633c10a3bb5d 72
JohnnyK 0:633c10a3bb5d 73 printf("Sending echo to client\n");
JohnnyK 0:633c10a3bb5d 74 client->send(out_buffer, n);
JohnnyK 0:633c10a3bb5d 75 led1 =! led1;
JohnnyK 1:8c10d8f4e3aa 76 delete []out_buffer;
JohnnyK 0:633c10a3bb5d 77 }
JohnnyK 0:633c10a3bb5d 78 led1 =! led1;
JohnnyK 1:8c10d8f4e3aa 79 delete []in_buffer;
JohnnyK 0:633c10a3bb5d 80 }
JohnnyK 0:633c10a3bb5d 81 client->close();
JohnnyK 0:633c10a3bb5d 82 }
JohnnyK 0:633c10a3bb5d 83 }else{
JohnnyK 0:633c10a3bb5d 84 printf("No IP\n");
JohnnyK 1:8c10d8f4e3aa 85 net->disconnect();
JohnnyK 0:633c10a3bb5d 86 printf("Program end\n");
JohnnyK 0:633c10a3bb5d 87 }
JohnnyK 0:633c10a3bb5d 88 }