TCP,UDP, ECHO

Dependencies:   mbed mbed-rtos EthernetInterface

Committer:
emilmont
Date:
Tue Jul 31 11:51:28 2012 +0000
Revision:
2:ec5ae99791da
Parent:
1:5cebe0e38cd2
Child:
3:36fd3cfad85a
Add explicit setting of socket blocking/non-blocking mode and timeout

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:38cbb854d85f 1 #include "mbed.h"
emilmont 1:5cebe0e38cd2 2 #include "EthernetInterface.h"
emilmont 1:5cebe0e38cd2 3
emilmont 1:5cebe0e38cd2 4 int main (void) {
emilmont 1:5cebe0e38cd2 5 EthernetInterface eth;
emilmont 1:5cebe0e38cd2 6 eth.init(); //Use DHCP
emilmont 1:5cebe0e38cd2 7 eth.connect();
emilmont 1:5cebe0e38cd2 8 printf("IP Address is %s\n", eth.getIPAddress());
emilmont 1:5cebe0e38cd2 9
emilmont 1:5cebe0e38cd2 10 TCPSocketServer server;
emilmont 1:5cebe0e38cd2 11 server.bind(7);
emilmont 1:5cebe0e38cd2 12 server.listen(1);
emilmont 1:5cebe0e38cd2 13
emilmont 1:5cebe0e38cd2 14 while (true) {
emilmont 1:5cebe0e38cd2 15 printf("\nWait for new connection...\n");
emilmont 1:5cebe0e38cd2 16 TCPSocketConnection client;
emilmont 1:5cebe0e38cd2 17 server.accept(client);
emilmont 2:ec5ae99791da 18 client.set_blocking(false);
emilmont 1:5cebe0e38cd2 19
emilmont 1:5cebe0e38cd2 20 printf("Connection from: %s\n", client.get_address());
emilmont 1:5cebe0e38cd2 21 char buffer[256];
emilmont 1:5cebe0e38cd2 22 while (true) {
emilmont 2:ec5ae99791da 23 int n = client.receive(buffer, 256);
emilmont 1:5cebe0e38cd2 24 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 25
emilmont 2:ec5ae99791da 26 client.send_all(buffer, n);
emilmont 1:5cebe0e38cd2 27 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 28 }
emilmont 1:5cebe0e38cd2 29 client.close();
emilmont 1:5cebe0e38cd2 30 }
emilmont 1:5cebe0e38cd2 31 }