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: EthernetInterface SimpleSocket mbed-rtos mbed
findbuddy.cpp
00001 #include "SimpleSocket.h" 00002 00003 void findbuddy() 00004 { 00005 int myIP[4]; 00006 sscanf(EthernetInterface::getIPAddress(), "%d.%d.%d.%d", myIP, myIP + 1, myIP + 2, myIP + 3); 00007 00008 DatagramSocket datagram(7777); 00009 00010 char buddy[32] = {}; 00011 int port; 00012 for (int i = myIP[3] + 1;; i = (i - myIP[3] + 260) % 10 + myIP[3] + 1) { 00013 sprintf(buddy, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], i); 00014 printf("sending message to %s\n", buddy); 00015 datagram.printf("Hello World\n"); 00016 00017 datagram.send(buddy, 7777); 00018 // this will not reach target during the first round, 00019 // and in order to receive successfully, 00020 // timeout must be at least 4 seconds (default = 5.0 seconds) 00021 datagram.setTimeout(4.0); 00022 if (datagram.receive(buddy, &port) > 0) { 00023 char buf[80] = {}; 00024 int len = datagram.read(buf, sizeof(buf) - 1); 00025 printf("received from %s: %s", buddy, buf); 00026 break; 00027 } 00028 } 00029 00030 for (int i = 0; i < 2; i++) { 00031 datagram.printf("There you are!\n"); 00032 datagram.send(buddy, 7777); 00033 00034 if (datagram.receive() > 0) { 00035 char buf[80] = {}; 00036 int len = datagram.read(buf, sizeof(buf) - 1); 00037 printf("received: %s", buf); 00038 } 00039 } 00040 }
Generated on Mon Jul 18 2022 00:17:10 by
1.7.2