Simple code for comunication via TCP between the mbed and PC.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
Fork of SimpleSocketExamples by
examples/udpsender.cpp@0:6dc3cfd058c6, 2013-02-04 (annotated)
- Committer:
- yamaguch
- Date:
- Mon Feb 04 09:29:18 2013 +0000
- Revision:
- 0:6dc3cfd058c6
modified to use SimpleSocket/EthernetInterface
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 udpsender() { |
yamaguch | 0:6dc3cfd058c6 | 4 | DatagramSocket datagram; |
yamaguch | 0:6dc3cfd058c6 | 5 | |
yamaguch | 0:6dc3cfd058c6 | 6 | char message[80] = {}; |
yamaguch | 0:6dc3cfd058c6 | 7 | printf("Enter message => "); |
yamaguch | 0:6dc3cfd058c6 | 8 | int c = 0; |
yamaguch | 0:6dc3cfd058c6 | 9 | while (c < ' ' || 0x7E < c) |
yamaguch | 0:6dc3cfd058c6 | 10 | c = getc(stdin); |
yamaguch | 0:6dc3cfd058c6 | 11 | ungetc(c, stdin); |
yamaguch | 0:6dc3cfd058c6 | 12 | for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++) |
yamaguch | 0:6dc3cfd058c6 | 13 | message[i] = c; |
yamaguch | 0:6dc3cfd058c6 | 14 | |
yamaguch | 0:6dc3cfd058c6 | 15 | int i1, i2, i3, i4; |
yamaguch | 0:6dc3cfd058c6 | 16 | printf("UDP receiver address => "); |
yamaguch | 0:6dc3cfd058c6 | 17 | scanf("%d.%d.%d.%d", &i1, &i2, &i3, &i4); |
yamaguch | 0:6dc3cfd058c6 | 18 | |
yamaguch | 0:6dc3cfd058c6 | 19 | for (int i = 0;; i++) { |
yamaguch | 0:6dc3cfd058c6 | 20 | printf("Sending message : %s (%d)\n", message, i); |
yamaguch | 0:6dc3cfd058c6 | 21 | datagram.printf("(%d) %s\n", i, message); |
yamaguch | 0:6dc3cfd058c6 | 22 | char ip[16] = {}; |
yamaguch | 0:6dc3cfd058c6 | 23 | sprintf(ip, "%d.%d.%d.%d", i1, i2, i3, i4); |
yamaguch | 0:6dc3cfd058c6 | 24 | datagram.send(ip, 7777); |
yamaguch | 0:6dc3cfd058c6 | 25 | wait(1); |
yamaguch | 0:6dc3cfd058c6 | 26 | } |
yamaguch | 0:6dc3cfd058c6 | 27 | } |