This example uses the mbed libraries to check Ethernet TCP Echo Server Example.

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of FRDM_K64F-Ethernet by Rangel Alvarado

Committer:
szymones
Date:
Wed Dec 06 10:14:24 2017 +0000
Revision:
1:aa0966de74f9
Parent:
0:bbc9cfdee3bc
initial commit, working client server and echo server

Who changed what in which revision?

UserRevisionLine numberNew contents of line
issaiass 0:bbc9cfdee3bc 1 #include "mbed.h"
issaiass 0:bbc9cfdee3bc 2 #include "EthernetInterface.h"
issaiass 0:bbc9cfdee3bc 3
szymones 1:aa0966de74f9 4 #define MBED_DEV_IP "192.168.5.61"
issaiass 0:bbc9cfdee3bc 5 #define MBED_DEV_MASK "255.255.255.0"
szymones 1:aa0966de74f9 6 #define MBED_DEV_GW "192.168.5.1"
issaiass 0:bbc9cfdee3bc 7 #define ECHO_SERVER_PORT 5000
szymones 1:aa0966de74f9 8 #define SERVER_CLIENT_PORT 5001
szymones 1:aa0966de74f9 9 #define PORT_HTTP 80
issaiass 0:bbc9cfdee3bc 10
szymones 1:aa0966de74f9 11 #define HTTP_GET_300POLITYKA ("GET /stan-gry/ HTTP/1.0\r\nHost: 300polityka.pl\r\n\r\n")
szymones 1:aa0966de74f9 12 //#define HTTP_GET_300POLITYKA ("GET / HTTP/1.0\r\nHost: 192.168.5.73\r\n\r\n")
szymones 1:aa0966de74f9 13 #define IP_300POLITYKA ("178.32.202.241")
szymones 1:aa0966de74f9 14 //#define IP_300POLITYKA ("192.168.5.73")
szymones 1:aa0966de74f9 15
szymones 1:aa0966de74f9 16 //Serial pc(USBTX, USBRX);
szymones 1:aa0966de74f9 17
szymones 1:aa0966de74f9 18 void eth_vTaskTimeReq(){
szymones 1:aa0966de74f9 19 int socketFd;
szymones 1:aa0966de74f9 20 int len;
szymones 1:aa0966de74f9 21 struct sockaddr_in clientSockDescr, serverSockDescr;
szymones 1:aa0966de74f9 22 char buf[1024];
szymones 1:aa0966de74f9 23 char httpGetReq[]= HTTP_GET_300POLITYKA;
szymones 1:aa0966de74f9 24
szymones 1:aa0966de74f9 25 socketFd = lwip_socket(AF_INET, SOCK_STREAM, 0);
szymones 1:aa0966de74f9 26 if (socketFd < 0)
szymones 1:aa0966de74f9 27 return;
szymones 1:aa0966de74f9 28
szymones 1:aa0966de74f9 29 memset( (char *)&clientSockDescr, 0, sizeof(clientSockDescr) );
szymones 1:aa0966de74f9 30 memset( (char *)&serverSockDescr, 0, sizeof(serverSockDescr) );
szymones 1:aa0966de74f9 31
szymones 1:aa0966de74f9 32 clientSockDescr.sin_family = AF_INET;
szymones 1:aa0966de74f9 33 clientSockDescr.sin_len = sizeof(clientSockDescr);
szymones 1:aa0966de74f9 34 clientSockDescr.sin_addr.s_addr = inet_addr(MBED_DEV_IP);
szymones 1:aa0966de74f9 35 clientSockDescr.sin_port = htons(SERVER_CLIENT_PORT);
szymones 1:aa0966de74f9 36
szymones 1:aa0966de74f9 37 serverSockDescr.sin_family = AF_INET;
szymones 1:aa0966de74f9 38 serverSockDescr.sin_len = sizeof(serverSockDescr);
szymones 1:aa0966de74f9 39 serverSockDescr.sin_addr.s_addr = inet_addr(IP_300POLITYKA);
szymones 1:aa0966de74f9 40 serverSockDescr.sin_port = htons(PORT_HTTP);
szymones 1:aa0966de74f9 41
szymones 1:aa0966de74f9 42 if ( lwip_bind(socketFd, (struct sockaddr *)&clientSockDescr, sizeof(struct sockaddr_in)) == -1 ){
szymones 1:aa0966de74f9 43 printf("\nBind failed\n");
szymones 1:aa0966de74f9 44 return;
szymones 1:aa0966de74f9 45 }
szymones 1:aa0966de74f9 46 printf("\nBind succeed\n");
szymones 1:aa0966de74f9 47
szymones 1:aa0966de74f9 48 while(1){
szymones 1:aa0966de74f9 49 if( lwip_connect( socketFd, (struct sockaddr*)(&serverSockDescr), sizeof( struct sockaddr_in ) ) == 0 ){
szymones 1:aa0966de74f9 50 if( lwip_send( socketFd, httpGetReq, strlen(httpGetReq), 0) >= 0 ){
szymones 1:aa0966de74f9 51 do{
szymones 1:aa0966de74f9 52 len= lwip_recv( socketFd, buf, sizeof(buf), 0 );
szymones 1:aa0966de74f9 53 if ( len>0 ){
szymones 1:aa0966de74f9 54 printf(buf);// setDateTime( buf, *( (int*)pvParameters ) );
szymones 1:aa0966de74f9 55 }
szymones 1:aa0966de74f9 56 } while ( len>0 );
szymones 1:aa0966de74f9 57 printf("\nSend succeed\n");
szymones 1:aa0966de74f9 58 }
szymones 1:aa0966de74f9 59
szymones 1:aa0966de74f9 60 }
szymones 1:aa0966de74f9 61 printf("\nConnect failed\n");
szymones 1:aa0966de74f9 62 break;
szymones 1:aa0966de74f9 63 }
szymones 1:aa0966de74f9 64 lwip_close(socketFd);
szymones 1:aa0966de74f9 65 }
issaiass 0:bbc9cfdee3bc 66
issaiass 0:bbc9cfdee3bc 67 int main (void) {
issaiass 0:bbc9cfdee3bc 68 EthernetInterface eth;
issaiass 0:bbc9cfdee3bc 69 eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway
issaiass 0:bbc9cfdee3bc 70 eth.connect();
issaiass 0:bbc9cfdee3bc 71 printf("IP Address is %s\n", eth.getIPAddress());
issaiass 0:bbc9cfdee3bc 72
szymones 1:aa0966de74f9 73 eth_vTaskTimeReq();
szymones 1:aa0966de74f9 74
issaiass 0:bbc9cfdee3bc 75 TCPSocketServer server;
issaiass 0:bbc9cfdee3bc 76 server.bind(ECHO_SERVER_PORT);
issaiass 0:bbc9cfdee3bc 77 server.listen();
issaiass 0:bbc9cfdee3bc 78
issaiass 0:bbc9cfdee3bc 79 while (true) {
issaiass 0:bbc9cfdee3bc 80 printf("\nWait for new connection...\n");
issaiass 0:bbc9cfdee3bc 81 TCPSocketConnection client;
issaiass 0:bbc9cfdee3bc 82 server.accept(client);
szymones 1:aa0966de74f9 83 client.set_blocking(false, 15000); // Timeout after (1.5)s
issaiass 0:bbc9cfdee3bc 84
issaiass 0:bbc9cfdee3bc 85 printf("Connection from: %s\n", client.get_address());
issaiass 0:bbc9cfdee3bc 86 char buffer[256];
issaiass 0:bbc9cfdee3bc 87 while (true) {
issaiass 0:bbc9cfdee3bc 88 int n = client.receive(buffer, sizeof(buffer));
issaiass 0:bbc9cfdee3bc 89 if (n <= 0) break;
szymones 1:aa0966de74f9 90 printf("%s\n", buffer);
issaiass 0:bbc9cfdee3bc 91 client.send_all(buffer, n);
issaiass 0:bbc9cfdee3bc 92 if (n <= 0) break;
issaiass 0:bbc9cfdee3bc 93 }
issaiass 0:bbc9cfdee3bc 94
issaiass 0:bbc9cfdee3bc 95 client.close();
issaiass 0:bbc9cfdee3bc 96 }
issaiass 0:bbc9cfdee3bc 97 }