Reputation: 20360
I'm on Windows 10, VS2019, 64-bit grpc v1.23.1-1 installed via vcpkg.
Using grpc's ServerBuilder
class. From the examples provided, the server code should look similar to this:
const std::string server_address = "0.0.0.0:12345";
int tcp_port;
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials(), &tcp_port);
builder.RegisterService(&myservice);
auto server(builder.BuildAndStart());
std::cout << "port=" << tcp_port << std::endl;
server->Wait();
No matter what I try to use for server_address
, the resulting tcp_port
is always zero, and calling netstat to view all listening ports confirms my application isn't listening anywhere.
The AddListeningPort()
API is documented here.
Examples of addresses I've tried so far:
[::1]:54321
dns:///[::1]43210
dns:///::1:
::1
::1:
0.0.0.0:12345
localhost
localhost:7525
127.0.0.1
127.0.0.1:9876
(I've tried dozens of random port numbers, not just the ones here, and netstat confirms I don't have anything bound to those ports.)
Is there any way to get more information from grpc? There doesn't seem to be any API to call within grpc::Server
nor grpc::ServerBuilder
to get an error code, status message, etc.
EDIT:
For people googling things related to grpc in the future, I've edited the example code to move the std::cout
line further down, since the port isn't valid until after the call to BuildAndStart()
(Thanks @Botje.)
Also determined why netstat on Windows wasn't showing me my application. I was using this command:
netstat -q -b -n -p tcp
But to see IPv6/TCPv6, I needed this:
netstat -q -b -n -p tcpv6
Since the address was similar to this ipv6 loopback: [::1]:12345
.
Note that you can call this more than once, so now I'm using something similar to these lines:
builder.AddListeningPort("127.0.0.1:12345", grpc::InsecureServerCredentials(), &tcp4_port);
builder.AddListeningPort("[::1]:12345", grpc::InsecureServerCredentials(), &tcp6_port);
Upvotes: 1
Views: 3410
Reputation: 30937
The documentation has this to say about selected_port
:
If not
nullptr
, gets populated with the port number bound to thegrpc::Server
for the corresponding endpoint after it is successfully bound byBuildAndStart()
, 0 otherwise.AddListeningPort
does not modify this pointer.
And for addr_uri
:
To bind to any address, please use IPv6 any, i.e.,
[::]:<port>
, which also accepts IPv4 connections.
Try passing "[::]:12345"
as addr_uri
and only checking the value of tcp_port
after calling BuildAndStart
.
Upvotes: 2