Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
findbuddy.cpp@34:a108bcc26b69, 2011-11-04 (annotated)
- Committer:
- yamaguch
- Date:
- Fri Nov 04 06:04:36 2011 +0000
- Revision:
- 34:a108bcc26b69
- Parent:
- 33:39d9cdf99de8
removed Serial mon,;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yamaguch |
33:39d9cdf99de8 | 1 | #include "EthernetNetIf.h" |
| yamaguch |
33:39d9cdf99de8 | 2 | #include "SimpleSocket.h" |
| yamaguch |
33:39d9cdf99de8 | 3 | |
| yamaguch |
33:39d9cdf99de8 | 4 | void findbuddy() { |
| yamaguch |
33:39d9cdf99de8 | 5 | EthernetNetIf eth; |
| yamaguch |
33:39d9cdf99de8 | 6 | eth.setup(); |
| yamaguch |
34:a108bcc26b69 | 7 | |
| yamaguch |
33:39d9cdf99de8 | 8 | IpAddr myIP = eth.getIp(); |
| yamaguch |
33:39d9cdf99de8 | 9 | DatagramSocket datagram(myIP, 7777); |
| yamaguch |
33:39d9cdf99de8 | 10 | |
| yamaguch |
33:39d9cdf99de8 | 11 | Host buddy; |
| yamaguch |
33:39d9cdf99de8 | 12 | for (int i = myIP[3] + 1;; i = (i - myIP[3] + 260) % 10 + myIP[3] + 1) { |
| yamaguch |
33:39d9cdf99de8 | 13 | printf("sending message to %d.%d.%d.%d\n", myIP[0], myIP[1], myIP[2], i); |
| yamaguch |
33:39d9cdf99de8 | 14 | datagram.printf("Hello World\n"); |
| yamaguch |
33:39d9cdf99de8 | 15 | datagram.send(IpAddr(myIP[0], myIP[1], myIP[2], i), 7777); |
| yamaguch |
33:39d9cdf99de8 | 16 | // this will not reach target during the first round, |
| yamaguch |
33:39d9cdf99de8 | 17 | // and in order to receive successfully, |
| yamaguch |
34:a108bcc26b69 | 18 | // timeout must be at least 4 seconds (default = 5.0 seconds) |
| yamaguch |
33:39d9cdf99de8 | 19 | if (datagram.receive(&buddy, 4) > 0) { |
| yamaguch |
33:39d9cdf99de8 | 20 | IpAddr ip = buddy.getIp(); |
| yamaguch |
33:39d9cdf99de8 | 21 | char buf[80] = {}; |
| yamaguch |
33:39d9cdf99de8 | 22 | int len = datagram.read(buf, sizeof(buf) - 1); |
| yamaguch |
33:39d9cdf99de8 | 23 | printf("received from %d.%d.%d.%d: %s", ip[0],ip[1], ip[2], ip[3], buf); |
| yamaguch |
33:39d9cdf99de8 | 24 | break; |
| yamaguch |
33:39d9cdf99de8 | 25 | } |
| yamaguch |
33:39d9cdf99de8 | 26 | } |
| yamaguch |
33:39d9cdf99de8 | 27 | |
| yamaguch |
33:39d9cdf99de8 | 28 | for (int i = 0; i < 2; i++) { |
| yamaguch |
33:39d9cdf99de8 | 29 | datagram.printf("There you are!\n"); |
| yamaguch |
33:39d9cdf99de8 | 30 | datagram.send(buddy.getIp(), 7777); |
| yamaguch |
34:a108bcc26b69 | 31 | |
| yamaguch |
33:39d9cdf99de8 | 32 | if (datagram.receive() > 0) { |
| yamaguch |
33:39d9cdf99de8 | 33 | char buf[80] = {}; |
| yamaguch |
33:39d9cdf99de8 | 34 | int len = datagram.read(buf, sizeof(buf) - 1); |
| yamaguch |
33:39d9cdf99de8 | 35 | printf("received: %s", buf); |
| yamaguch |
33:39d9cdf99de8 | 36 | } |
| yamaguch |
33:39d9cdf99de8 | 37 | } |
| yamaguch |
33:39d9cdf99de8 | 38 | } |