eth
Dependencies: EthernetInterface mbed-rtos mbed
Fork of FRDM_K64F-Ethernet by
main.cpp@0:bbc9cfdee3bc, 2014-09-22 (annotated)
- Committer:
- issaiass
- Date:
- Mon Sep 22 02:34:12 2014 +0000
- Revision:
- 0:bbc9cfdee3bc
- Child:
- 1:52ba06578336
This example uses the FRDM-K64F board Ethernet interface with a static ip address connected directly to the PC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
issaiass | 0:bbc9cfdee3bc | 1 | #include "mbed.h" |
issaiass | 0:bbc9cfdee3bc | 2 | #include "EthernetInterface.h" |
issaiass | 0:bbc9cfdee3bc | 3 | |
issaiass | 0:bbc9cfdee3bc | 4 | #define MBED_DEV_IP "192.168.0.52" |
issaiass | 0:bbc9cfdee3bc | 5 | #define MBED_DEV_MASK "255.255.255.0" |
issaiass | 0:bbc9cfdee3bc | 6 | #define MBED_DEV_GW "0.0.0.0" |
issaiass | 0:bbc9cfdee3bc | 7 | #define ECHO_SERVER_PORT 5000 |
issaiass | 0:bbc9cfdee3bc | 8 | |
issaiass | 0:bbc9cfdee3bc | 9 | |
issaiass | 0:bbc9cfdee3bc | 10 | int main (void) { |
issaiass | 0:bbc9cfdee3bc | 11 | EthernetInterface eth; |
issaiass | 0:bbc9cfdee3bc | 12 | eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway |
issaiass | 0:bbc9cfdee3bc | 13 | eth.connect(); |
issaiass | 0:bbc9cfdee3bc | 14 | printf("IP Address is %s\n", eth.getIPAddress()); |
issaiass | 0:bbc9cfdee3bc | 15 | |
issaiass | 0:bbc9cfdee3bc | 16 | TCPSocketServer server; |
issaiass | 0:bbc9cfdee3bc | 17 | server.bind(ECHO_SERVER_PORT); |
issaiass | 0:bbc9cfdee3bc | 18 | server.listen(); |
issaiass | 0:bbc9cfdee3bc | 19 | |
issaiass | 0:bbc9cfdee3bc | 20 | while (true) { |
issaiass | 0:bbc9cfdee3bc | 21 | printf("\nWait for new connection...\n"); |
issaiass | 0:bbc9cfdee3bc | 22 | TCPSocketConnection client; |
issaiass | 0:bbc9cfdee3bc | 23 | server.accept(client); |
issaiass | 0:bbc9cfdee3bc | 24 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
issaiass | 0:bbc9cfdee3bc | 25 | |
issaiass | 0:bbc9cfdee3bc | 26 | printf("Connection from: %s\n", client.get_address()); |
issaiass | 0:bbc9cfdee3bc | 27 | char buffer[256]; |
issaiass | 0:bbc9cfdee3bc | 28 | while (true) { |
issaiass | 0:bbc9cfdee3bc | 29 | int n = client.receive(buffer, sizeof(buffer)); |
issaiass | 0:bbc9cfdee3bc | 30 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 31 | |
issaiass | 0:bbc9cfdee3bc | 32 | client.send_all(buffer, n); |
issaiass | 0:bbc9cfdee3bc | 33 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 34 | } |
issaiass | 0:bbc9cfdee3bc | 35 | |
issaiass | 0:bbc9cfdee3bc | 36 | client.close(); |
issaiass | 0:bbc9cfdee3bc | 37 | } |
issaiass | 0:bbc9cfdee3bc | 38 | } |