make the mbed become a server * and receive the data from the client * send the data to the screen. * confirm the server ip is 192.168.1.101

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoServer by mbed official

Committer:
shiyilei
Date:
Thu Oct 30 16:55:14 2014 +0000
Revision:
7:93f5f670deb9
Parent:
3:36fd3cfad85a
make the mbed become a server; * and receive the data from the client; * send the data to the screen.; * confirm the server ip is 192.168.1.101

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shiyilei 7:93f5f670deb9 1 /**************************************
shiyilei 7:93f5f670deb9 2 *file :TcpServer test
shiyilei 7:93f5f670deb9 3 *Creator:JacobShi
shiyilei 7:93f5f670deb9 4 *Time:2014/10/29
shiyilei 7:93f5f670deb9 5 * Description:make the mbed become a server
shiyilei 7:93f5f670deb9 6 * and receive the data from the client
shiyilei 7:93f5f670deb9 7 * send the data to the screen.
shiyilei 7:93f5f670deb9 8 * confirm the server ip is 192.168.1.101
shiyilei 7:93f5f670deb9 9 ***************************************/
mbed_official 0:38cbb854d85f 10 #include "mbed.h"
emilmont 1:5cebe0e38cd2 11 #include "EthernetInterface.h"
shiyilei 7:93f5f670deb9 12 EthernetInterface eth;
shiyilei 7:93f5f670deb9 13 #define PORT 8080
shiyilei 7:93f5f670deb9 14 char databuffer[100];
shiyilei 7:93f5f670deb9 15 int main(void)
shiyilei 7:93f5f670deb9 16 {
shiyilei 7:93f5f670deb9 17 eth.init("192.168.1.101","225.225.225.0","192.168.1.1");
emilmont 1:5cebe0e38cd2 18 eth.connect();
shiyilei 7:93f5f670deb9 19 printf("the ip address of the mbed is :%s\n",eth.getIPAddress());
emilmont 1:5cebe0e38cd2 20
emilmont 1:5cebe0e38cd2 21 TCPSocketServer server;
shiyilei 7:93f5f670deb9 22 TCPSocketConnection client;
shiyilei 7:93f5f670deb9 23 server.bind(PORT);
emilmont 3:36fd3cfad85a 24 server.listen();
shiyilei 7:93f5f670deb9 25 server.accept(client);
shiyilei 7:93f5f670deb9 26 client.set_blocking(true,NULL);
shiyilei 7:93f5f670deb9 27 printf("the ip addr of the client is :%s\n",client.get_address() );
emilmont 1:5cebe0e38cd2 28
shiyilei 7:93f5f670deb9 29 while(1)
shiyilei 7:93f5f670deb9 30 {
shiyilei 7:93f5f670deb9 31 int n=client.receive(databuffer,sizeof(databuffer));
shiyilei 7:93f5f670deb9 32 if(n<=0)
shiyilei 7:93f5f670deb9 33 break;
shiyilei 7:93f5f670deb9 34 client.send_all("receive OK",sizeof("receive OK"));
shiyilei 7:93f5f670deb9 35 printf("the receive data is :\n");
shiyilei 7:93f5f670deb9 36 for(int i=0;i<n;i++)
shiyilei 7:93f5f670deb9 37 {
shiyilei 7:93f5f670deb9 38 printf("%c\n",databuffer[i]);
shiyilei 7:93f5f670deb9 39 }
shiyilei 7:93f5f670deb9 40
emilmont 1:5cebe0e38cd2 41 }
shiyilei 7:93f5f670deb9 42 client.close();
emilmont 1:5cebe0e38cd2 43 }