Skip to content

Commit 029ced4

Browse files
committed
- Added IP changed example
1 parent 7867c24 commit 029ced4

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ target_link_libraries(StartTls-Example socketlib ${LIBCRYPTO} ${LIBSSL})
4848
add_executable(Dtls-Example Dtls-Example.cpp)
4949
target_link_libraries(Dtls-Example socketlib ${LIBCRYPTO} ${LIBSSL})
5050

51+
add_executable(Notify-Example Notify-Example.cpp)
52+
target_link_libraries(Notify-Example socketlib ${LIBCRYPTO} ${LIBSSL})
53+
5154
if(NOT MSVC AND UA_ENABLE_ENCRYPTION_OPENSSL)
5255
target_link_libraries(Tcp-Example pthread)
5356
target_link_libraries(Ssl-Example pthread)
5457
target_link_libraries(StartTls-Example pthread)
5558
target_link_libraries(Dtls-Example pthread)
59+
target_link_libraries(Notify-Example pthread)
5660
endif()

Notify-Example.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
#include <thread>
3+
#include <iostream>
4+
#include <chrono>
5+
6+
#include "SocketLib/SocketLib.h"
7+
8+
#if defined (_WIN32) || defined (_WIN64)
9+
#include <conio.h>
10+
#else // Linux
11+
#include <termios.h>
12+
#include <time.h>
13+
auto _kbhit = []() -> int
14+
{
15+
struct termios oldt, newt;
16+
int ch;
17+
int oldf;
18+
19+
tcgetattr(STDIN_FILENO, &oldt);
20+
newt = oldt;
21+
newt.c_lflag &= ~(ICANON | ECHO);
22+
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
23+
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
24+
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
25+
26+
ch = getchar();
27+
28+
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
29+
fcntl(STDIN_FILENO, F_SETFL, oldf);
30+
31+
if (ch != EOF)
32+
{
33+
ungetc(ch, stdin);
34+
return 1;
35+
}
36+
37+
return 0;
38+
};
39+
#define localtime_s(x,y) localtime_r(y,x)
40+
#endif
41+
42+
int main(int argc, const char* argv[])
43+
{
44+
#if defined(_WIN32) || defined(_WIN64)
45+
// Detect Memory Leaks
46+
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
47+
#endif
48+
49+
BaseSocket::EnumIpAddresses([](int iAddrType, const std::string& strAddr, int iPort, void*) -> int
50+
{
51+
std::cout << "IP Address: " << strAddr << ", Port: " << iPort << ", Type: " << iAddrType << std::endl;
52+
return 0; // continue enumeration
53+
}, nullptr);
54+
55+
BaseSocket::SetAddrNotifyCallback([](bool bAdded, const std::string& strAddr, int iPort, int iAddrType)
56+
{
57+
std::cout << "Address " << (bAdded ? "added" : "removed") << ": " << strAddr << ", Port: " << iPort << ", Type: " << iAddrType << std::endl;
58+
});
59+
60+
const char caZeichen[] = "\\|/-";
61+
int iIndex{0};
62+
while (_kbhit() == 0)
63+
{
64+
std::cout << '\r' << caZeichen[iIndex++] << std::flush;
65+
if (iIndex > 3) iIndex = 0;
66+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
67+
}
68+
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)