Using ServerBuilder, how do I tell that the port is in use? (C++)

40 views
Skip to first unread message

He Ke

unread,
Dec 3, 2020, 7:16:02 PM12/3/20
to grpc.io

int bound_port=0;
std::string server_address("0.0.0.0:50051");
builder.AddListeningPort(server_address, grpc::SslServerCredentials(ssl_options) &bound_port);
builder.RegisterService(&service);
std::unique_ptr server(builder.BuildAndStart());
if(bound_port == 0) { cout<<"port already in use!" << endl; }

I don't know why above code is not work, the bound_port return always is 50051, I can't get 0 as expected when port already in use.

Thanks for your help in advance!

yas...@google.com

unread,
Dec 24, 2020, 3:48:46 AM12/24/20
to grpc.io
gRPC Core sets SO_REUSEPORT by default and I believe that's why you are running into this. To disable this, you can set the channel argument GRPC_ARG_ALLOW_REUSEPORT to 0.
Reply all
Reply to author
Forward
0 new messages