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

Committer:
shiyilei
Date:
Thu Oct 30 16:57:40 2014 +0000
Revision:
7:da224eeb7f59
Parent:
3:3fbf0efec25a
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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shiyilei 7:da224eeb7f59 1 /*********************************************************
shiyilei 7:da224eeb7f59 2 *file:TCPClient application
shiyilei 7:da224eeb7f59 3 *Creator:JacobShi
shiyilei 7:da224eeb7f59 4 *Time:2014/10/29
shiyilei 7:da224eeb7f59 5 *Description: make the mbed become a TCP client and
shiyilei 7:da224eeb7f59 6 *connect the TCP Server 192.168.1.100
shiyilei 7:da224eeb7f59 7 * Send the data to Server and receive the data from the
shiyilei 7:da224eeb7f59 8 * server ,sending the received data through the uart
shiyilei 7:da224eeb7f59 9 **********************************************************/
mbed_official 0:e8f884c94549 10 #include "mbed.h"
emilmont 1:a51d8ed156e6 11 #include "EthernetInterface.h"
shiyilei 7:da224eeb7f59 12 EthernetInterface eth;
shiyilei 7:da224eeb7f59 13 char data_buffer[150];
shiyilei 7:da224eeb7f59 14 int main(void)
shiyilei 7:da224eeb7f59 15 {
shiyilei 7:da224eeb7f59 16 eth.init();
emilmont 1:a51d8ed156e6 17 eth.connect();
shiyilei 7:da224eeb7f59 18 printf("the ipaddr of the mbed is %s\n",eth.getIPAddress() );
emilmont 3:3fbf0efec25a 19 TCPSocketConnection socket;
shiyilei 7:da224eeb7f59 20 while( socket.connect("192.168.1.100",8080)<0)
shiyilei 7:da224eeb7f59 21 {
shiyilei 7:da224eeb7f59 22 printf("connect error\n");
emilmont 1:a51d8ed156e6 23 wait(1);
shiyilei 7:da224eeb7f59 24 }
shiyilei 7:da224eeb7f59 25 printf("the ip addr of the server is %s\n",socket.get_address() );
shiyilei 7:da224eeb7f59 26 socket.send_all("Hello I am the client",sizeof("Hello I am the client"));
shiyilei 7:da224eeb7f59 27 while(1)
shiyilei 7:da224eeb7f59 28 {
shiyilei 7:da224eeb7f59 29
shiyilei 7:da224eeb7f59 30 int n=socket.receive(data_buffer,150);
shiyilei 7:da224eeb7f59 31 if(n<0)
shiyilei 7:da224eeb7f59 32 break;
shiyilei 7:da224eeb7f59 33 socket.send_all("Receive OK",sizeof("Receive OK"));
shiyilei 7:da224eeb7f59 34 data_buffer[n]='\0';
shiyilei 7:da224eeb7f59 35 printf("%s\n",data_buffer);
shiyilei 7:da224eeb7f59 36
shiyilei 7:da224eeb7f59 37 }
shiyilei 7:da224eeb7f59 38
shiyilei 7:da224eeb7f59 39 socket.close();
shiyilei 7:da224eeb7f59 40
shiyilei 7:da224eeb7f59 41 return 0;
emilmont 1:a51d8ed156e6 42 }
shiyilei 7:da224eeb7f59 43