谷歌计算引擎 + 套接字

Fel*_*nto 2 sockets android google-compute-engine google-cloud-platform

我正在使用静态 IP 在 Google 计算引擎上运行套接字服务器,这是我启动服务器的方式:

public class Server {

private static int port=4800;

public static void main(String argv[]) throws Exception {
    System.out.println("STARTED ON PORT 4800!");

    @SuppressWarnings("resource")
    ServerSocket socketIn = new ServerSocket(port);

    while (true) {
        Socket socketConenection = socketIn.accept();
        Thread t = new Thread(new ServerThread(socketConnection, connection));
        t.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

public static BufferedReader conectServer(String body, String function) throws IOException {
    Socket socketClient = new Socket(ipServer, portServer);
    DataOutputStream outToServer = new DataOutputStream(socketClient.getOutputStream());
    BufferedReader fromServer = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
    outToServer.writeBytes(function+ '\n' + body + '\n');
    return fromServer;
}
Run Code Online (Sandbox Code Playgroud)

因此,使用我的 android 应用程序,我尝试连接到服务器,它在我的家用 PC 上运行正常,但是当我尝试连接到我的 Google Compute Engine VM 时,它超时了:

java.net.ConnectException: failed to connect to /146.148.66.128 (port 4800): connect failed: ETIMEDOUT (Connection timed out)
Run Code Online (Sandbox Code Playgroud)

E. *_*son 5

抱歉,如果这很明显,但是您是否在 GCE VM 上打开了防火墙

默认情况下,GCE 虚拟机拥有除 SSH 外的所有流量都被互联网阻止。