Make the server tell not only the port, but also the address it is listening on.

This is necessary because on Windows/msys2, the Java gRPC server listens only on IPv6 but the C++ client only tries to connect over IPv4, resulting in breakage.

--
MOS_MIGRATED_REVID=120689437
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 321706a..cbcf416 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -1859,11 +1859,19 @@
 bool GrpcBlazeServer::Connect() {
   std::string server_dir = globals->options.output_base + "/server";
   std::string port;
+  std::string ipv4_prefix = "127.0.0.1:";
+  std::string ipv6_prefix = "::1:";
 
   if (!ReadFile(server_dir + "/command_port", &port)) {
     return false;
   }
 
+  // Make sure that we are being directed to localhost
+  if (port.compare(0, ipv4_prefix.size(), ipv4_prefix)
+      && port.compare(0, ipv6_prefix.size(), ipv6_prefix)) {
+    return false;
+  }
+
   if (!ReadFile(server_dir + "/request_cookie", &request_cookie_)) {
     return false;
   }
@@ -1873,7 +1881,7 @@
   }
 
   std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel(
-      "localhost:" + port, grpc::InsecureChannelCredentials()));
+      port, grpc::InsecureChannelCredentials()));
   std::unique_ptr<command_server::CommandServer::Stub> client(
       command_server::CommandServer::NewStub(channel));
 
diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
index a5dbbf0..cd6c68d 100644
--- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
+++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
@@ -40,6 +40,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.reflect.Field;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.charset.Charset;
@@ -224,7 +225,8 @@
   @Override
   public void serve() throws IOException {
     Preconditions.checkState(!serving);
-    server = NettyServerBuilder.forAddress(new InetSocketAddress("localhost", port))
+    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
+    server = NettyServerBuilder.forAddress(new InetSocketAddress(loopbackAddress, port))
         .addService(CommandServerGrpc.bindService(this))
         .build();
 
@@ -235,7 +237,7 @@
       port = getActualServerPort();
     }
 
-    writeServerFile(PORT_FILE, Integer.toString(port));
+    writeServerFile(PORT_FILE, loopbackAddress.getHostAddress() + ":" + Integer.toString(port));
     writeServerFile(REQUEST_COOKIE_FILE, requestCookie);
     writeServerFile(RESPONSE_COOKIE_FILE, responseCookie);