|
| 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