udp_hello_world.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD-3 License)
3  *
4  * Copyright (c) 2018 Daniel Koch.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
41 #include <async_comm/udp.h>
42 
43 #include <cstdint>
44 #include <cstring>
45 #include <iostream>
46 
47 #include <chrono>
48 #include <thread>
49 #include <vector>
50 
51 
60 void callback(const uint8_t* buf, size_t len)
61 {
62  for (size_t i = 0; i < len; i++)
63  {
64  std::cout << buf[i];
65  }
66 }
67 
68 
69 int main()
70 {
71  // open UDP ports
72  async_comm::UDP udp1("localhost", 14620, "localhost", 14625);
74 
75  async_comm::UDP udp2("localhost", 14625, "localhost", 14620);
77 
78  if (!udp1.init() || !udp2.init())
79  {
80  std::cout << "Failed to initialize UDP ports" << std::endl;
81  return 1;
82  }
83 
84  // send message one direction
85  char message1[] = "hello world 1!";
86  udp2.send_bytes((uint8_t*) message1, std::strlen(message1));
87 
88  // wait for all bytes to be received
89  std::this_thread::sleep_for(std::chrono::milliseconds(500));
90  std::cout << std::endl << std::flush;
91 
92  // send message the other direction
93  char message2[] = "hello world 2!";
94  udp1.send_bytes((uint8_t*) message2, std::strlen(message2));
95 
96  // wait for all bytes to be received
97  std::this_thread::sleep_for(std::chrono::milliseconds(500));
98  std::cout << std::endl << std::flush;
99 
100  std::cout.flush();
101 
102  // close UDP ports
103  udp1.close();
104  udp2.close();
105 
106  return 0;
107 }
void send_bytes(const uint8_t *src, size_t len)
Send bytes from a buffer over the port.
Definition: comm.cpp:97
bool init()
Initializes and opens the port.
Definition: comm.cpp:61
void register_receive_callback(std::function< void(const uint8_t *, size_t)> fun)
Register a callback function for when bytes are received on the port.
Definition: comm.cpp:110
void callback(const uint8_t *buf, size_t len)
Callback function for the async_comm library.
void close()
Closes the port.
Definition: comm.cpp:74
int main()
Asynchronous communication class for a UDP socket.
Definition: udp.h:56


async_comm
Author(s):
autogenerated on Fri May 14 2021 02:35:38