For lecture

Dependencies:   WIZnetInterface WIZwiki-W7500-lecture mbed

Fork of WIZwiki-W7500-lecture1-tcp by Bohyun Bang

Committer:
bangbh
Date:
Mon Mar 07 00:22:10 2016 +0000
Revision:
5:b0e5f03fe7b8
Parent:
4:d164f370aa56
For lecture

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joon874 0:d7c890f9ee69 1 #include "mbed.h"
joon874 0:d7c890f9ee69 2 #include "EthernetInterface.h"
joon874 0:d7c890f9ee69 3
joon874 0:d7c890f9ee69 4 int main() {
joon874 0:d7c890f9ee69 5
joon874 0:d7c890f9ee69 6 EthernetInterface eth;
bangbh 2:ba228e5b659c 7
bangbh 4:d164f370aa56 8 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0xFF, 0xFF};
bangbh 5:b0e5f03fe7b8 9 char message[] = "Hello World!\r\n";
bangbh 5:b0e5f03fe7b8 10 char buffer[400];
bangbh 5:b0e5f03fe7b8 11 int ret;
bangbh 5:b0e5f03fe7b8 12
bangbh 2:ba228e5b659c 13 eth.init(mac_addr, "192.168.0.180", "255.255.255.0", "192.168.0.1"); // USE Static IP
bangbh 2:ba228e5b659c 14 //eth.init(mac_addr); //Use DHCP
joon874 0:d7c890f9ee69 15 eth.connect();
joon874 0:d7c890f9ee69 16
joon874 0:d7c890f9ee69 17 printf("IP Address is %s\r\n", eth.getIPAddress());
joon874 0:d7c890f9ee69 18
bangbh 5:b0e5f03fe7b8 19 TCPSocketServer server;
bangbh 5:b0e5f03fe7b8 20 server.bind(4545);
bangbh 5:b0e5f03fe7b8 21 server.listen();
bangbh 5:b0e5f03fe7b8 22
bangbh 3:90acfa2e24cb 23 while (true)
bangbh 3:90acfa2e24cb 24 {
bangbh 5:b0e5f03fe7b8 25 TCPSocketConnection client;
bangbh 5:b0e5f03fe7b8 26 server.accept(client);
bangbh 5:b0e5f03fe7b8 27 client.set_blocking(false, 10000);//10 sec
bangbh 5:b0e5f03fe7b8 28
bangbh 5:b0e5f03fe7b8 29 printf("Connection from: %s\r\n", client.get_address());
bangbh 5:b0e5f03fe7b8 30 while(true){
bangbh 5:b0e5f03fe7b8 31 int n = client.receive(buffer, sizeof(buffer));
bangbh 5:b0e5f03fe7b8 32 if(n <= 0) break;
bangbh 5:b0e5f03fe7b8 33 buffer[n] = '\0';
bangbh 5:b0e5f03fe7b8 34 client.send(buffer,n);
bangbh 5:b0e5f03fe7b8 35 if(n <= 0) break;
bangbh 3:90acfa2e24cb 36 }
bangbh 5:b0e5f03fe7b8 37 printf("Disconnected\r\n");
bangbh 5:b0e5f03fe7b8 38 client.close();
joon874 0:d7c890f9ee69 39 }
bangbh 5:b0e5f03fe7b8 40 server.close();
joon874 0:d7c890f9ee69 41
joon874 0:d7c890f9ee69 42 eth.disconnect();
bangbh 3:90acfa2e24cb 43
bangbh 3:90acfa2e24cb 44 printf("Disconnected!");
joon874 0:d7c890f9ee69 45 }