gpt4 book ai didi

Java - 套接字在某些互联网上出现 'connection timed out error' 或 'no route to host' 错误

转载 作者:行者123 更新时间:2023-12-02 00:38:26 27 4
gpt4 key购买 nike

我正在使客户端/服务器与套接字配对来来回发送和接收数据。当我在家中使用两台独立的计算机作为客户端/服务器上网时,它按预期工作正常。数据传输等等。

但是,今天当我在本地的一家咖啡店(Second Cup,如果相关的话)工作时,这不起作用。我不断收到以下错误:连接超时或没有到主机的路由。

相关代码如下:

服务器:

public class TestServer {

public static void main(String[] args) throws Exception {

TestServer myServer = new TestServer();
myServer.run();

}

private void run() throws Exception {

ServerSocket mySS = new ServerSocket(9999);

while(true) {

Socket SS_accept = mySS.accept();

BufferedReader myBR = new BufferedReader(new InputStreamReader(SS_accept.getInputStream()));

String temp = myBR.readLine();
System.out.println(temp);

if (temp!=null) {

PrintStream serverPS = new PrintStream(SS_accept.getOutputStream());
serverPS.println("Response received");

}

}
}

}

客户端:(相关部分)

//sends a command to the server, and returns the server's response
private String tellServer(String text) throws Exception {

mySocket = new Socket("192.168.0.XXX", 9999); //the IPv4 address

//use the socket's outputStream to tell stuff to the server
PrintStream myPS = new PrintStream(mySocket.getOutputStream());
myPS.println(text);

//the following code will get data back from the server
BufferedReader clientBR = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
String temp = clientBR.readLine();

if (temp!=null) {
return temp;
} else {
return "";
}

}

这非常简单。再次,如前所述,在家里的互联网上它工作得很好 - 只需执行 ipconfig,获取 IPv4 地址,然后将其放入客户端即可。在有免费无线网络的咖啡店里,它不起作用。为了以防万一,我什至摆弄了许多不同的端口。

感谢您的帮助,我是套接字新手,发现这很令人困惑。

最佳答案

192.168.x.y 是本地地址。 source

您需要您的家用计算机的 IP 地址,因为互联网可以看到它。

当您下次回家时,请访问 http://www.whatismyip.com/看看它认为你是什么。

请注意,您可能需要进入路由器并将流量从路由器路由到计算机的端口 9999,因为您可能会访问该端口。

关于Java - 套接字在某些互联网上出现 'connection timed out error' 或 'no route to host' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7071923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com