-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (60 loc) · 1.86 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
#include <thread>
#include "udp_sender.h"
#include "udp_receiver.h"
// sample program to demonstrate udp_sender/udp_receiver
int main(int argc, char **argv)
{
int udp_port = 10253; // default UDP port
if(argc < 2) {
std::cout << "Insufficient arguments provided, please use one of (default port=10253):" << std::endl;
std::cout << " -s <ip-receiver> [<port>] : send " << std::endl;
std::cout << " -r [<port>] : receive " << std::endl;
return 1;
}
std::string arg(argv[1]);
if(arg == "-s" && argc >=3 ) {
// SENDER
// ========
// ip-address for receiver
std::string ipaddress(argv[2]);
if(argc >=4) {
std::istringstream inport(argv[3]);
inport >> udp_port;
}
// loop and send a message every second forever
udp_sender udp(ipaddress,udp_port);
size_t counter=0;
while(true) {
std::string msg = "Hello from udp_sender " + std::to_string(++counter);
std::cout << "Sending: " << msg << std::endl;
udp.send(msg);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
else if(arg == "-r") {
// RECEIVER
// ========
if(argc >=3) {
std::istringstream inport(argv[2]);
inport >> udp_port;
}
try {
udp_receiver receiver(udp_port);
receiver.receive(std::make_shared<udp_consumer>());
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
else {
std::cout << "Invalid or insufficient arguments provided '"+arg+"', please use one of" << std::endl;
std::cout << " -s <ip-receiver> : send" << std::endl;
std::cout << " -r : receive" << std::endl;
return 1;
}
return 0;
}