Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 00004 #define ECHO_SERVER_PORT 7 00005 static const char* mbedIp = "192.168.137.2"; //IP 00006 static const char* mbedMask = "255.255.255.0"; // Mask 00007 static const char* mbedGateway = "192.168.137.1"; //Gateway 00008 int main (void) { 00009 EthernetInterface eth; 00010 eth.init(mbedIp,mbedMask,mbedGateway); 00011 eth.connect(); 00012 printf("\nServer IP Address is %s\n", eth.getIPAddress()); 00013 00014 TCPSocketServer server; 00015 server.bind(ECHO_SERVER_PORT); 00016 server.listen(); 00017 00018 while (true) { 00019 printf("\nWait for new connection...\n"); 00020 TCPSocketConnection client; 00021 server.accept(client); 00022 client.set_blocking(false, 1500); // Timeout after (1.5)s 00023 00024 printf("Connection from: %s\n", client.get_address()); 00025 char buffer[256]; 00026 while (true) { 00027 int n = client.receive(buffer, sizeof(buffer)); 00028 if (n <= 0) break; 00029 00030 // print received message to terminal 00031 buffer[n] = '\0'; 00032 printf("Received message from Client :'%s'\n",buffer); 00033 00034 // reverse the message 00035 char temp; 00036 for(int f = 0, l = n-1; f<l; f++,l--){ 00037 temp = buffer[f]; 00038 buffer[f] = buffer[l]; 00039 buffer[l] = temp; 00040 } 00041 00042 // print reversed message to terminal 00043 printf("Sending message to Client: '%s'\n",buffer); 00044 00045 // Echo received message back to client 00046 client.send_all(buffer, n); 00047 if (n <= 0) break; 00048 } 00049 00050 client.close(); 00051 } 00052 } 00053
Generated on Mon Jul 18 2022 12:56:44 by
1.7.2
