Ethernet and TCP

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoServer by Mbed

Committer:
lachu
Date:
Tue Jan 24 12:34:59 2017 +0000
Revision:
9:9224b02b6192
Parent:
8:475898cdb7d4
SISK

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:38cbb854d85f 1 #include "mbed.h"
emilmont 1:5cebe0e38cd2 2 #include "EthernetInterface.h"
lachu 8:475898cdb7d4 3 #include <string>
emilmont 1:5cebe0e38cd2 4
emilmont 3:36fd3cfad85a 5 #define ECHO_SERVER_PORT 7
emilmont 3:36fd3cfad85a 6
emilmont 1:5cebe0e38cd2 7 int main (void) {
lachu 8:475898cdb7d4 8
emilmont 1:5cebe0e38cd2 9 EthernetInterface eth;
lachu 9:9224b02b6192 10 //1)Inicjalizacja interfejsu (DHCP)
lachu 9:9224b02b6192 11
emilmont 1:5cebe0e38cd2 12 eth.connect();
lachu 9:9224b02b6192 13 //2)
lachu 9:9224b02b6192 14 printf("\nServer IP Address is %s\n", *** );
emilmont 1:5cebe0e38cd2 15
emilmont 1:5cebe0e38cd2 16 TCPSocketServer server;
emilmont 3:36fd3cfad85a 17 server.bind(ECHO_SERVER_PORT);
emilmont 3:36fd3cfad85a 18 server.listen();
emilmont 1:5cebe0e38cd2 19
emilmont 1:5cebe0e38cd2 20 while (true) {
emilmont 1:5cebe0e38cd2 21 printf("\nWait for new connection...\n");
emilmont 1:5cebe0e38cd2 22 TCPSocketConnection client;
emilmont 1:5cebe0e38cd2 23 server.accept(client);
lachu 9:9224b02b6192 24 client.set_blocking(false, 20000); // Timeout after 20s
emilmont 1:5cebe0e38cd2 25
emilmont 1:5cebe0e38cd2 26 printf("Connection from: %s\n", client.get_address());
emilmont 1:5cebe0e38cd2 27 char buffer[256];
lachu 9:9224b02b6192 28
emilmont 1:5cebe0e38cd2 29 while (true) {
lachu 9:9224b02b6192 30
emilmont 3:36fd3cfad85a 31 int n = client.receive(buffer, sizeof(buffer));
emilmont 1:5cebe0e38cd2 32
mbedAustin 7:a5ead1402704 33 printf("Received message from Client :'%s'\n",buffer);
mbedAustin 7:a5ead1402704 34
lachu 8:475898cdb7d4 35 client.send_all(buffer, n);
lachu 8:475898cdb7d4 36 buffer[n] = '\0';
lachu 8:475898cdb7d4 37
lachu 8:475898cdb7d4 38 printf("Variable n is: %d\n", n);
mbedAustin 7:a5ead1402704 39
lachu 9:9224b02b6192 40 //3
lachu 8:475898cdb7d4 41 if(n>0){
lachu 8:475898cdb7d4 42 if(strcmp(buffer, "red") == 0){
lachu 8:475898cdb7d4 43 printf("REDled\n");
lachu 9:9224b02b6192 44 //TODO
lachu 8:475898cdb7d4 45 }else if(strcmp(buffer, "green") == 0){
lachu 8:475898cdb7d4 46 printf("GREENled\n");
lachu 9:9224b02b6192 47 //TODO
lachu 8:475898cdb7d4 48 }else if(strcmp(buffer, "blue") == 0){
lachu 8:475898cdb7d4 49 printf("BLUEled\n");
lachu 9:9224b02b6192 50 //TODO
lachu 8:475898cdb7d4 51 }else if(strcmp(buffer, "clear") == 0){
lachu 8:475898cdb7d4 52 printf("CLEAR\n");
lachu 9:9224b02b6192 53 //TODO
lachu 9:9224b02b6192 54 }else if(strcmp(buffer, "close") == 0){
lachu 8:475898cdb7d4 55 printf("CLOSE\n");
lachu 8:475898cdb7d4 56 break;
lachu 8:475898cdb7d4 57 }
lachu 8:475898cdb7d4 58 }
mbedAustin 7:a5ead1402704 59
lachu 8:475898cdb7d4 60 printf("Received message from Client :'%s'\n",buffer);
lachu 8:475898cdb7d4 61
lachu 8:475898cdb7d4 62 n=0;
lachu 9:9224b02b6192 63
emilmont 1:5cebe0e38cd2 64 }
emilmont 1:5cebe0e38cd2 65 client.close();
emilmont 1:5cebe0e38cd2 66 }
emilmont 1:5cebe0e38cd2 67 }