#include #include #include #include #include #include #include #include #include using namespace std; const char * g_wss_srv = "wss://localhost:8000"; // OK //const char * g_wss_srv = "wss://192.168.1.1:8000"; // OK //const char * g_wss_srv = "wss://[::1]:8000"; // Not OK //const char * g_wss_srv = "wss://[fd01:2345:6789:a::1]:8000"; // Not OK class ServerTest { public: ServerTest() {}; virtual ~ServerTest() { cout << "DTOR : ServerTest" << endl; }; static int Launch() { nng_tls_config * cconf; nng_tls_config_alloc(&cconf, NNG_TLS_MODE_SERVER); nng_tls_config_cert_key_file(cconf, "/path/to/certkey.pem", NULL); nng_socket rep_sock_c; nng_rep0_open(&rep_sock_c); nng_socket_set_ptr(rep_sock_c, NNG_OPT_TLS_CONFIG, cconf); nng_listen(rep_sock_c, g_wss_srv, NULL, 0 ); cout << "Server : listen" << endl; const char * msg; size_t sz; nng_recv(rep_sock_c, (void*) msg, &sz, 0); cout << "Server received : " << (const char*) msg << " " << sz << endl; nng_send(rep_sock_c, (void*) "abcd", 5, 0); cout << "Server sending..." << endl; cout << "Server exit" << endl; return 0; } }; class ClientTest { public: ClientTest() {}; virtual ~ClientTest() { cout << "DTOR : ClientTest" << endl; }; static int Launch() { nng_tls_config * cconf; nng_tls_config_alloc(&cconf, NNG_TLS_MODE_CLIENT); nng_tls_config_auth_mode(cconf, NNG_TLS_AUTH_MODE_REQUIRED); nng_tls_config_ca_file(cconf, "/path/to/ca.pem"); nng_socket req_sock_c; nng_req0_open(&req_sock_c); nng_socket_set_ptr(req_sock_c, NNG_OPT_TLS_CONFIG, cconf); cout << "Client : dial" << endl; nng_dial(req_sock_c, g_wss_srv, NULL, 0); cout << "Client sending..." << endl; nng_send(req_sock_c, (void*) "hello", 6, 0); const char * msg; size_t sz; nng_recv(req_sock_c, (void*) msg, &sz, 0); cout << "Client received : " << (const char*) msg << " " << sz << endl; cout << "Client exit" << endl; return 0; } }; int main() { thread s(ServerTest::Launch); s.detach(); sleep(1); thread c(ClientTest::Launch); c.detach(); sleep(5); return 0; }