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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*********************************************************
00002 *file:TCPClient application
00003 *Creator:JacobShi
00004 *Time:2014/10/29
00005 *Description: make the mbed become a TCP client and 
00006 *connect the TCP Server 192.168.1.100
00007 * Send the data to Server and receive the data from the 
00008 * server ,sending the received data through the uart
00009 **********************************************************/
00010 #include "mbed.h"
00011 #include "EthernetInterface.h"
00012 EthernetInterface eth;
00013 char data_buffer[150];
00014 int main(void)
00015 {
00016     eth.init();
00017     eth.connect();
00018     printf("the ipaddr of the mbed is %s\n",eth.getIPAddress() );
00019     TCPSocketConnection socket;
00020     while( socket.connect("192.168.1.100",8080)<0)
00021     {
00022         printf("connect error\n");
00023         wait(1);
00024    }
00025         printf("the ip addr of the server is %s\n",socket.get_address() );
00026       socket.send_all("Hello I am the client",sizeof("Hello I am the client"));
00027    while(1)
00028    {
00029         
00030         int n=socket.receive(data_buffer,150);
00031         if(n<0)
00032         break;
00033         socket.send_all("Receive OK",sizeof("Receive OK"));
00034         data_buffer[n]='\0';
00035         printf("%s\n",data_buffer);
00036 
00037    }
00038 
00039    socket.close();
00040 
00041     return 0;
00042 }
00043