我正在创建一个客户端 - 服务器应用程序,它只是通过套接字从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--;
}
}
}
服务器没有看到使用移动数据创建的连接,因此不存在服务器问题。
谢谢,对不起我的英文!
答案 0 :(得分:0)
问题隐藏在我的固件中,因此重新安装解决了这个问题。