簡體   English   中英

谷歌計算引擎 + 套接字

[英]google compute engine + socket

我正在使用靜態 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();
    }
}

}

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;
}

因此,我使用我的 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)

抱歉,如果這很明顯,但是您是否在 GCE VM 上打開了防火牆

默認情況下,GCE 虛擬機擁有除 SSH 外的所有流量都被互聯網阻止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM