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

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of FRDM_K64F-Ethernet-SKM by Szymon eS

Revision:
1:aa0966de74f9
Parent:
0:bbc9cfdee3bc
Child:
2:47551c2af80b
--- a/main.cpp	Mon Sep 22 02:34:12 2014 +0000
+++ b/main.cpp	Wed Dec 06 10:14:24 2017 +0000
@@ -1,11 +1,68 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
  
-#define MBED_DEV_IP       "192.168.0.52"
+#define MBED_DEV_IP       "192.168.5.61"
 #define MBED_DEV_MASK     "255.255.255.0"
-#define MBED_DEV_GW       "0.0.0.0"
+#define MBED_DEV_GW       "192.168.5.1"
 #define ECHO_SERVER_PORT   5000
+#define SERVER_CLIENT_PORT 5001
+#define PORT_HTTP 80
 
+#define HTTP_GET_300POLITYKA ("GET /stan-gry/ HTTP/1.0\r\nHost: 300polityka.pl\r\n\r\n")
+//#define HTTP_GET_300POLITYKA ("GET / HTTP/1.0\r\nHost: 192.168.5.73\r\n\r\n")
+#define IP_300POLITYKA ("178.32.202.241")
+//#define IP_300POLITYKA ("192.168.5.73")
+
+//Serial pc(USBTX, USBRX);
+
+void eth_vTaskTimeReq(){
+    int socketFd;
+    int len;
+    struct sockaddr_in clientSockDescr, serverSockDescr;
+    char buf[1024];
+    char httpGetReq[]= HTTP_GET_300POLITYKA;
+
+    socketFd = lwip_socket(AF_INET, SOCK_STREAM, 0);
+    if (socketFd < 0)
+        return;
+
+    memset( (char *)&clientSockDescr, 0, sizeof(clientSockDescr) );
+    memset( (char *)&serverSockDescr, 0, sizeof(serverSockDescr) );
+
+    clientSockDescr.sin_family = AF_INET;
+    clientSockDescr.sin_len = sizeof(clientSockDescr);
+    clientSockDescr.sin_addr.s_addr = inet_addr(MBED_DEV_IP);
+    clientSockDescr.sin_port = htons(SERVER_CLIENT_PORT);
+
+    serverSockDescr.sin_family = AF_INET;
+    serverSockDescr.sin_len = sizeof(serverSockDescr);
+    serverSockDescr.sin_addr.s_addr = inet_addr(IP_300POLITYKA);
+    serverSockDescr.sin_port = htons(PORT_HTTP);
+
+    if ( lwip_bind(socketFd, (struct sockaddr *)&clientSockDescr, sizeof(struct sockaddr_in)) == -1 ){
+        printf("\nBind failed\n");  
+        return;
+    }
+    printf("\nBind succeed\n");
+    
+    while(1){
+        if( lwip_connect( socketFd, (struct sockaddr*)(&serverSockDescr), sizeof( struct sockaddr_in ) ) == 0 ){
+            if( lwip_send( socketFd, httpGetReq, strlen(httpGetReq), 0) >= 0 ){
+                do{
+                    len= lwip_recv( socketFd, buf, sizeof(buf), 0 );
+                    if ( len>0 ){
+                       printf(buf);// setDateTime( buf, *( (int*)pvParameters ) );
+                    }
+                }  while ( len>0 );
+                printf("\nSend succeed\n"); 
+            }
+              
+        }
+        printf("\nConnect failed\n"); 
+        break;
+    }
+        lwip_close(socketFd);
+}
  
 int main (void) {
     EthernetInterface eth;
@@ -13,6 +70,8 @@
     eth.connect();
     printf("IP Address is %s\n", eth.getIPAddress());
     
+    eth_vTaskTimeReq();
+    
     TCPSocketServer server;
     server.bind(ECHO_SERVER_PORT);
     server.listen();
@@ -21,14 +80,14 @@
         printf("\nWait for new connection...\n");
         TCPSocketConnection client;
         server.accept(client);
-        client.set_blocking(false, 1500); // Timeout after (1.5)s
+        client.set_blocking(false, 15000); // Timeout after (1.5)s
         
         printf("Connection from: %s\n", client.get_address());
         char buffer[256];
         while (true) {
             int n = client.receive(buffer, sizeof(buffer));
             if (n <= 0) break;
-            
+            printf("%s\n", buffer);
             client.send_all(buffer, n);
             if (n <= 0) break;
         }