This example uses the mbed libraries to check Ethernet TCP Echo Server Example. It was tested successfully with K64F.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of FRDM_K64F-Ethernet by
main.cpp@1:f4d12640cd54, 2015-09-29 (annotated)
- Committer:
- elpellini
- Date:
- Tue Sep 29 14:50:45 2015 +0000
- Revision:
- 1:f4d12640cd54
- Parent:
- 0:bbc9cfdee3bc
Teste de publica??o
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 | |
elpellini | 1:f4d12640cd54 | 4 | #define MBED_DEV_IP "172.16.3.100" |
elpellini | 1:f4d12640cd54 | 5 | #define MBED_DEV_MASK "255.255.0.0" |
elpellini | 1:f4d12640cd54 | 6 | #define MBED_DEV_GW "172.16.0.254" |
issaiass | 0:bbc9cfdee3bc | 7 | #define ECHO_SERVER_PORT 5000 |
issaiass | 0:bbc9cfdee3bc | 8 | |
elpellini | 1:f4d12640cd54 | 9 | Serial PC(USBTX,USBRX); |
issaiass | 0:bbc9cfdee3bc | 10 | |
issaiass | 0:bbc9cfdee3bc | 11 | int main (void) { |
issaiass | 0:bbc9cfdee3bc | 12 | EthernetInterface eth; |
elpellini | 1:f4d12640cd54 | 13 | PC.baud(9600); |
elpellini | 1:f4d12640cd54 | 14 | PC.printf("K64F is alive!!!\r\n"); |
issaiass | 0:bbc9cfdee3bc | 15 | eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway |
issaiass | 0:bbc9cfdee3bc | 16 | eth.connect(); |
elpellini | 1:f4d12640cd54 | 17 | PC.printf("IP Address is %s\r\n", eth.getIPAddress()); |
issaiass | 0:bbc9cfdee3bc | 18 | |
issaiass | 0:bbc9cfdee3bc | 19 | TCPSocketServer server; |
issaiass | 0:bbc9cfdee3bc | 20 | server.bind(ECHO_SERVER_PORT); |
issaiass | 0:bbc9cfdee3bc | 21 | server.listen(); |
issaiass | 0:bbc9cfdee3bc | 22 | |
issaiass | 0:bbc9cfdee3bc | 23 | while (true) { |
elpellini | 1:f4d12640cd54 | 24 | PC.printf("Wait for new connection...\r\n"); |
issaiass | 0:bbc9cfdee3bc | 25 | TCPSocketConnection client; |
issaiass | 0:bbc9cfdee3bc | 26 | server.accept(client); |
elpellini | 1:f4d12640cd54 | 27 | client.set_blocking(false, 5000); // Timeout after (5)s |
issaiass | 0:bbc9cfdee3bc | 28 | |
elpellini | 1:f4d12640cd54 | 29 | PC.printf("Connection from: %s\r\n", client.get_address()); |
issaiass | 0:bbc9cfdee3bc | 30 | char buffer[256]; |
issaiass | 0:bbc9cfdee3bc | 31 | while (true) { |
issaiass | 0:bbc9cfdee3bc | 32 | int n = client.receive(buffer, sizeof(buffer)); |
issaiass | 0:bbc9cfdee3bc | 33 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 34 | |
issaiass | 0:bbc9cfdee3bc | 35 | client.send_all(buffer, n); |
issaiass | 0:bbc9cfdee3bc | 36 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 37 | } |
issaiass | 0:bbc9cfdee3bc | 38 | |
issaiass | 0:bbc9cfdee3bc | 39 | client.close(); |
issaiass | 0:bbc9cfdee3bc | 40 | } |
issaiass | 0:bbc9cfdee3bc | 41 | } |