【发布时间】:2024-01-05 03:59:01
【问题描述】:
我正在创建一个客户端-服务器应用程序,它只是通过套接字从 Java 服务器发送和接收 JSON。 所以它是通过 Wi-Fi 连接工作的。但是当我使用移动数据连接时,我的应用程序无法连接到服务器。 错误:
java.net.SocketTimeoutException: failed to connect to /37.59.196.27 (port 8080) after 90000ms
这是我的代码:
MultiClient.java
...
InetAddress addr = InetAddress.getByName("37.59.196.27");
ClientThread ct = new ClientThread(addr, map);
Thread.currentThread().sleep(100);
...
ClientThread.java
/*imports here*/
class ClientThread extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter = 0;
private int id = counter++;
private static int threadcount = 0;
private static String IMEI = "";
private static String NAME = "";
static final int PORT = 8080;
public static int threadCount() {
return threadcount;
}
HashMap<String, String> inputMap;
private HashMap<String,String> returnMap;
public HashMap<String, String> getData(){
return returnMap;
}
public ClientThread(InetAddress addr, HashMap<String, String> inputMap) {
returnMap=new HashMap<String, String>();
this.inputMap=inputMap;
System.out.println("Making client " + id);
threadcount++;
try {
socket = new Socket(addr, PORT);
socket.setSoTimeout(0);
}
catch (IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
Log.i("socket", e.getMessage());
System.err.println("Socket failed");
}
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
start();
}
catch (IOException e) {
try {
socket.close();
}
catch (IOException e2) {
System.err.println("Socket not closed");
}
}
}
public void run() {
try {
IMEI = inputMap.get("imei");
NAME = inputMap.get("name");
Log.i("ClientThread", "here");
JSONObject obj = new JSONObject();
obj.put("imei", IMEI);
obj.put("name", NAME);
obj.put("os", inputMap.get("os"));
obj.put("osVersion", inputMap.get("osVersion"));
out.println(obj);
String str = in.readLine();
//System.out.println(obj);
//SERVER ANSWER:
JSONParser parser = new JSONParser();
Object jobj = parser.parse(str);
JSONObject jsonObj = (JSONObject)jobj;
String text = jsonObj.get("text").toString();
Log.i("RESPONSE STRING","Image source: "+jsonObj.get("imagesrc")+"\nPrediction text: "+text);
// returnMap.put("imagesrc", jsonObj.get("imagesrc").toString());
// Log.i("text",text);
returnMap.put("text", text);
//System.out.println("Server: "+str);
out.println("END");
}
catch (IOException e) {
System.err.println("IO Exception");
}
catch(Exception e){
e.printStackTrace();
}
finally {
try {
socket.close();
}
catch (IOException e) {
System.err.println("Socket not closed");
}
threadcount--;
}
}
}
服务器看不到使用移动数据创建的连接,因此不是服务器问题。
感谢和抱歉我的英语!
【问题讨论】:
-
So it's working via Wi-Fi connection您的局域网中可能有该服务器。如果它在 LAN 中,您需要弄清楚您的公共 IP 是什么,并在路由器上配置端口转发以使连接到达您的 LAN 内服务器。否则你将无法连接。 -
服务器不在局域网中。我从另一个网络成功连接(例如,Restoraunt wi-fi)。
-
也许您的移动 ISP 阻止了与
80不同的端口的传出连接? -
我尝试 ping google.com:80 并得到“未知主机 google.com”。但我成功地用浏览器浏览了所有网站。通过 wi-fi 连接进行相同的 ping 操作也很好。也许有一些我不知道的关于使用套接字连接和移动数据的功能?
-
你不能ping端口,ping是ICMP,没有端口。如前所述,您应该进行一些测试,例如尝试在您的服务器上使用端口 80,并尝试连接到像 google.com(端口 80)这样的工作服务器,以查看它是否连接,即使之后数据是垃圾。尝试缩小您的问题,否则我们无能为力。
标签: java android sockets client server