SimpleSocket 1.0 examples

Dependencies:   EthernetNetIf SimpleSocket 1.0 mbed

Committer:
yamaguch
Date:
Mon Feb 04 09:04:25 2013 +0000
Revision:
40:84182fc63956
Parent:
findbuddy.cpp@39:108499af2b53
changed SimpleSocket libary name to SimpleSocketV1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 39:108499af2b53 1 #include "EthernetNetIf.h"
yamaguch 39:108499af2b53 2 #include "SimpleSocket.h"
yamaguch 39:108499af2b53 3
yamaguch 39:108499af2b53 4 void findbuddy() {
yamaguch 39:108499af2b53 5 EthernetNetIf eth;
yamaguch 39:108499af2b53 6 eth.setup();
yamaguch 39:108499af2b53 7
yamaguch 39:108499af2b53 8 IpAddr myIP = eth.getIp();
yamaguch 39:108499af2b53 9 DatagramSocket datagram(myIP, 7777);
yamaguch 39:108499af2b53 10
yamaguch 39:108499af2b53 11 Host buddy;
yamaguch 39:108499af2b53 12 for (int i = myIP[3] + 1;; i = (i - myIP[3] + 260) % 10 + myIP[3] + 1) {
yamaguch 39:108499af2b53 13 printf("sending message to %d.%d.%d.%d\n", myIP[0], myIP[1], myIP[2], i);
yamaguch 39:108499af2b53 14 datagram.printf("Hello World\n");
yamaguch 39:108499af2b53 15 datagram.send(IpAddr(myIP[0], myIP[1], myIP[2], i), 7777);
yamaguch 39:108499af2b53 16 // this will not reach target during the first round,
yamaguch 39:108499af2b53 17 // and in order to receive successfully,
yamaguch 39:108499af2b53 18 // timeout must be at least 4 seconds (default = 5.0 seconds)
yamaguch 39:108499af2b53 19 if (datagram.receive(&buddy, 4) > 0) {
yamaguch 39:108499af2b53 20 IpAddr ip = buddy.getIp();
yamaguch 39:108499af2b53 21 char buf[80] = {};
yamaguch 39:108499af2b53 22 int len = datagram.read(buf, sizeof(buf) - 1);
yamaguch 39:108499af2b53 23 printf("received from %d.%d.%d.%d: %s", ip[0],ip[1], ip[2], ip[3], buf);
yamaguch 39:108499af2b53 24 break;
yamaguch 39:108499af2b53 25 }
yamaguch 39:108499af2b53 26 }
yamaguch 39:108499af2b53 27
yamaguch 39:108499af2b53 28 for (int i = 0; i < 2; i++) {
yamaguch 39:108499af2b53 29 datagram.printf("There you are!\n");
yamaguch 39:108499af2b53 30 datagram.send(buddy.getIp(), 7777);
yamaguch 39:108499af2b53 31
yamaguch 39:108499af2b53 32 if (datagram.receive() > 0) {
yamaguch 39:108499af2b53 33 char buf[80] = {};
yamaguch 39:108499af2b53 34 int len = datagram.read(buf, sizeof(buf) - 1);
yamaguch 39:108499af2b53 35 printf("received: %s", buf);
yamaguch 39:108499af2b53 36 }
yamaguch 39:108499af2b53 37 }
yamaguch 39:108499af2b53 38 }