我在使用静态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应用程序,我尝试连接到服务器,它在我的家用电脑上运行正常,但是当我尝试连接到我的Google Compute Engine VM时,它会超时:
java.net.ConnectException: failed to connect to /146.148.66.128 (port 4800): connect failed: ETIMEDOUT (Connection timed out)
答案 0 :(得分:1)
很抱歉,如果这很明显,但您open the firewall on the GCE VM了吗?
默认情况下,GCE虚拟机除了从互联网上阻止SSH之外的所有流量。