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:
multicast.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 multicast() {
yamaguch 39:108499af2b53 5 EthernetNetIf eth;
yamaguch 39:108499af2b53 6 eth.setup();
yamaguch 39:108499af2b53 7
yamaguch 39:108499af2b53 8 Host multicast(IpAddr(239, 192, 1, 100), 50000);
yamaguch 39:108499af2b53 9 DatagramSocket datagram(multicast);
yamaguch 39:108499af2b53 10
yamaguch 39:108499af2b53 11 while (true) {
yamaguch 39:108499af2b53 12 Host host;
yamaguch 39:108499af2b53 13 if (datagram.receive(&host, 1 + (rand() % 5) / 3.0) > 0) {
yamaguch 39:108499af2b53 14 int value;
yamaguch 39:108499af2b53 15 datagram.scanf("%d", &value);
yamaguch 39:108499af2b53 16 IpAddr ip = host.getIp();
yamaguch 39:108499af2b53 17 printf("received from %d.%d.%d.%d:%d %d\n", ip[0], ip[1], ip[2], ip[3], host.getPort(), value);
yamaguch 39:108499af2b53 18 } else {
yamaguch 39:108499af2b53 19 char* message = "12345!";
yamaguch 39:108499af2b53 20 datagram.printf(message);
yamaguch 39:108499af2b53 21 datagram.send(multicast);
yamaguch 39:108499af2b53 22 printf("sent: %s\n", message);
yamaguch 39:108499af2b53 23 wait(1);
yamaguch 39:108499af2b53 24 }
yamaguch 39:108499af2b53 25 }
yamaguch 39:108499af2b53 26 }