Skip to content

Commit d7e94ac

Browse files
alek3yarthw
authored andcommitted
server : bind to any port when specified (ggml-org#10590)
1 parent 4537839 commit d7e94ac

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

examples/server/server.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,8 +3347,18 @@ int main(int argc, char ** argv) {
33473347
llama_backend_free();
33483348
};
33493349

3350-
// bind HTTP listen port, run the HTTP server in a thread
3351-
if (!svr->bind_to_port(params.hostname, params.port)) {
3350+
// bind HTTP listen port
3351+
bool was_bound = false;
3352+
if (params.port == 0) {
3353+
int bound_port = svr->bind_to_any_port(params.hostname);
3354+
if ((was_bound = (bound_port >= 0))) {
3355+
params.port = bound_port;
3356+
}
3357+
} else {
3358+
was_bound = svr->bind_to_port(params.hostname, params.port);
3359+
}
3360+
3361+
if (!was_bound) {
33523362
//LOG_ERROR("couldn't bind HTTP server socket", {
33533363
// {"hostname", params.hostname},
33543364
// {"port", params.port},
@@ -3357,6 +3367,8 @@ int main(int argc, char ** argv) {
33573367
clean_up();
33583368
return 1;
33593369
}
3370+
3371+
// run the HTTP server in a thread
33603372
std::thread t([&]() { svr->listen_after_bind(); });
33613373
svr->wait_until_ready();
33623374

0 commit comments

Comments
 (0)