make the mbed become a TCP client and *connect the TCP Server 192.168.1.100 * Send the data to Server and receive the data from the * server ,sending the received data through the uart

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoClient by mbed official

Files at this revision

API Documentation at this revision

Comitter:
shiyilei
Date:
Thu Oct 30 16:57:40 2014 +0000
Parent:
6:36dfad519099
Commit message:
make the mbed become a TCP client and ; *connect the TCP Server 192.168.1.100; * Send the data to Server and receive the data from the ; * server ,sending the received data through the uart

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 36dfad519099 -r da224eeb7f59 main.cpp
--- a/main.cpp	Wed May 14 15:24:47 2014 +0000
+++ b/main.cpp	Thu Oct 30 16:57:40 2014 +0000
@@ -1,31 +1,43 @@
+/*********************************************************
+*file:TCPClient application
+*Creator:JacobShi
+*Time:2014/10/29
+*Description: make the mbed become a TCP client and 
+*connect the TCP Server 192.168.1.100
+* Send the data to Server and receive the data from the 
+* server ,sending the received data through the uart
+**********************************************************/
 #include "mbed.h"
 #include "EthernetInterface.h"
-
-const char* ECHO_SERVER_ADDRESS = "192.168.0.51";
-const int ECHO_SERVER_PORT = 7;
-
-int main() {
-    EthernetInterface eth;
-    eth.init(); //Use DHCP
+EthernetInterface eth;
+char data_buffer[150];
+int main(void)
+{
+    eth.init();
     eth.connect();
-    printf("IP Address is %s\n", eth.getIPAddress());
-    
+    printf("the ipaddr of the mbed is %s\n",eth.getIPAddress() );
     TCPSocketConnection socket;
-    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+    while( socket.connect("192.168.1.100",8080)<0)
+    {
+        printf("connect error\n");
         wait(1);
-    }
-    
-    char hello[] = "Hello World\n";
-    socket.send_all(hello, sizeof(hello) - 1);
-    
-    char buf[256];
-    int n = socket.receive(buf, 256);
-    buf[n] = '\0';
-    printf("%s", buf);
-    
-    socket.close();
-    eth.disconnect();
-    
-    while(true) {}
+   }
+        printf("the ip addr of the server is %s\n",socket.get_address() );
+      socket.send_all("Hello I am the client",sizeof("Hello I am the client"));
+   while(1)
+   {
+        
+        int n=socket.receive(data_buffer,150);
+        if(n<0)
+        break;
+        socket.send_all("Receive OK",sizeof("Receive OK"));
+        data_buffer[n]='\0';
+        printf("%s\n",data_buffer);
+
+   }
+
+   socket.close();
+
+    return 0;
 }
+ 
\ No newline at end of file