Simple code for comunication via TCP between the mbed and PC.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
Fork of SimpleSocketExamples by
examples/multicast.cpp@1:016774025718, 2014-12-17 (annotated)
- Committer:
- numeral369
- Date:
- Wed Dec 17 16:06:00 2014 +0000
- Revision:
- 1:016774025718
- Parent:
- 0:6dc3cfd058c6
A simple code for comunication via TCP between the Mbed and a PC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 0:6dc3cfd058c6 | 1 | #include "SimpleSocket.h" |
yamaguch | 0:6dc3cfd058c6 | 2 | |
yamaguch | 0:6dc3cfd058c6 | 3 | void multicast() { |
yamaguch | 0:6dc3cfd058c6 | 4 | Endpoint multicast; |
yamaguch | 0:6dc3cfd058c6 | 5 | multicast.set_address("239.192.1.100", 50000); |
yamaguch | 0:6dc3cfd058c6 | 6 | DatagramSocket datagram(multicast); |
yamaguch | 0:6dc3cfd058c6 | 7 | |
yamaguch | 0:6dc3cfd058c6 | 8 | while (true) { |
yamaguch | 0:6dc3cfd058c6 | 9 | Endpoint host; |
yamaguch | 0:6dc3cfd058c6 | 10 | datagram.setTimeout(1 + (rand() % 5) / 3.0); |
yamaguch | 0:6dc3cfd058c6 | 11 | if (datagram.receive(host) > 0) { |
yamaguch | 0:6dc3cfd058c6 | 12 | int value; |
yamaguch | 0:6dc3cfd058c6 | 13 | datagram.scanf("%d", &value); |
yamaguch | 0:6dc3cfd058c6 | 14 | char *ip = host.get_address(); |
yamaguch | 0:6dc3cfd058c6 | 15 | printf("received from %s:%d %d\n", ip, host.get_port(), value); |
yamaguch | 0:6dc3cfd058c6 | 16 | } else { |
yamaguch | 0:6dc3cfd058c6 | 17 | char* message = "12345!"; |
yamaguch | 0:6dc3cfd058c6 | 18 | datagram.printf(message); |
yamaguch | 0:6dc3cfd058c6 | 19 | datagram.send(multicast); |
yamaguch | 0:6dc3cfd058c6 | 20 | printf("sent: %s\n", message); |
yamaguch | 0:6dc3cfd058c6 | 21 | wait(1); |
yamaguch | 0:6dc3cfd058c6 | 22 | } |
yamaguch | 0:6dc3cfd058c6 | 23 | } |
yamaguch | 0:6dc3cfd058c6 | 24 | } |