From 2aa74a1deabcdaf850641538a7dd380d768ebf1c Mon Sep 17 00:00:00 2001 From: Jan Nikl Date: Fri, 13 Mar 2026 13:46:04 -0700 Subject: [PATCH 1/2] Added non-exclusive port option. --- CHANGELOG | 6 ++++++ glvis.cpp | 34 ++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 46abcd48..bea2ae1a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,12 @@ https://glvis.org +Version 4.5.1 (development) +=========================== + +- Added the option ('-no-ex') to run GLVis in non-exclusive setting when the + server starts listening on the next available port instead of aborting. + Version 4.5 released on Feb 6, 2026 =================================== diff --git a/glvis.cpp b/glvis.cpp index c68418b2..e7dadf48 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -157,7 +157,8 @@ class Session }; void GLVisServer(int portnum, bool save_stream, bool fix_elem_orient, - bool save_coloring, string plot_caption, bool headless = false) + bool save_coloring, string plot_caption, bool headless = false, + bool exclusive = true) { std::vector current_sessions; string data_type; @@ -200,10 +201,26 @@ void GLVisServer(int portnum, bool save_stream, bool fix_elem_orient, #endif const int backlog = 128; - socketserver server(portnum, backlog); - if (server.good()) + unique_ptr server; + if (!exclusive) + { + for (;; portnum++) + { + server = make_unique(portnum, backlog); + if (server->good()) { break; } + } + } + else + { + server = make_unique(portnum, backlog); + } + if (server->good()) { cout << "Waiting for data on port " << portnum << " ..." << endl; + if (!exclusive) + { + cout << "GLVIS_SERVER_PORT=" << portnum << endl; + } } else { @@ -220,7 +237,7 @@ void GLVisServer(int portnum, bool save_stream, bool fix_elem_orient, isock.reset(secure ? new socketstream(*params) : new socketstream(false)); #endif vector> input_streams; - while (server.accept(*isock) < 0) + while (server->accept(*isock) < 0) { #ifdef GLVIS_DEBUG cout << "GLVis: server.accept(...) failed." << endl; @@ -290,7 +307,7 @@ void GLVisServer(int portnum, bool save_stream, bool fix_elem_orient, break; } // read next available socket stream - while (server.accept(*isock) < 0) + while (server->accept(*isock) < 0) { #ifdef GLVIS_DEBUG cout << "GLVis: server.accept(...) failed." << endl; @@ -385,6 +402,7 @@ int main (int argc, char *argv[]) const char *font_name = string_default; int portnum = 19916; bool persistent = true; + bool exclusive = true; int multisample = GetMultisample(); double line_width = GetLineWidth(); double ms_line_width = GetLineWidthMS(); @@ -458,6 +476,9 @@ int main (int argc, char *argv[]) args.AddOption(&persistent, "-pr", "--persistent", "-no-pr", "--no-persistent", "Keep server running after all windows are closed."); + args.AddOption(&exclusive, "-ex", "--exclusive", + "-no-ex", "--no-exclusive", + "Exclusively block the given port or take the next available."); args.AddOption(&secure, "-sec", "--secure-sockets", "-no-sec", "--standard-sockets", "Enable or disable GnuTLS secure sockets."); @@ -693,7 +714,8 @@ int main (int argc, char *argv[]) std::thread serverThread{GLVisServer, portnum, save_stream, win.data_state.fix_elem_orient, win.data_state.save_coloring, - win.plot_caption, win.headless}; + win.plot_caption, win.headless, + exclusive}; // Start message loop in main thread MainThreadLoop(win.headless, persistent); From d51bc85adbc77387a3786f98e4e55972a92dbedc Mon Sep 17 00:00:00 2001 From: Jan Nikl Date: Fri, 13 Mar 2026 13:56:35 -0700 Subject: [PATCH 2/2] Changed cout to cerr for logging the port. --- glvis.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glvis.cpp b/glvis.cpp index e7dadf48..b865a606 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -219,7 +219,7 @@ void GLVisServer(int portnum, bool save_stream, bool fix_elem_orient, cout << "Waiting for data on port " << portnum << " ..." << endl; if (!exclusive) { - cout << "GLVIS_SERVER_PORT=" << portnum << endl; + cerr << "GLVIS_SERVER_PORT=" << portnum << endl; } } else