Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序已连接到服务器,但无法通信_Java_Android_Sockets_Java Server_Java Client - Fatal编程技术网

Java 应用程序已连接到服务器,但无法通信

Java 应用程序已连接到服务器,但无法通信,java,android,sockets,java-server,java-client,Java,Android,Sockets,Java Server,Java Client,我为我的英语不好提前道歉 该应用程序能够连接到java服务器,但在交换数据时挂起 这是客户端android代码: @SuppressLint("NewApi") public class Connection extends IntentService{ private String tag = "Ciclo eventi"; private String user; private String pass; public Connection() { super("Connecti

我为我的英语不好提前道歉

该应用程序能够连接到java服务器,但在交换数据时挂起

这是客户端android代码:

@SuppressLint("NewApi")
public class Connection extends IntentService{

private String tag = "Ciclo eventi";
private String user;
private String pass;

public Connection()
{
    super("Connection");
}

public void onCreate(){
    super.onCreate();
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

public void onStart(Intent intent, int startId){

    Log.d(tag, "GetData");
    Bundle extras = intent.getExtras();
    user = (String) extras.get("User");
    pass = (String) extras.get("Password");
    Log.d(tag, user);
    Log.d(tag, pass);
    onHandleIntent(intent);
}

public int onStartCommand(Intent intent, int flags, int startId){
    onHandleIntent(intent);
    return START_NOT_STICKY;
}



@Override
public void onDestroy()
{
    Log.d(tag, "CONNECTION CLOSED");

}

@Override
protected void onHandleIntent(Intent intent) {
    Socket s=null;
    BufferedReader in=null;
    PrintWriter  writer=null;
    try {
        Log.d(tag, "Try to connect");
        s = getConnection("192.168.1.103", 5433);
        Log.d(tag, "Connection done");
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        writer = new PrintWriter(s.getOutputStream(), true);
        writer.println(user);
        writer.println(pass);
        Log.d(tag, "I've send the credential");
        String resp = null;
        resp = in.readLine();
        Log.d(tag, "Receive the results");
        if(resp.equals("done")){
            Log.d(tag, "ACCEPT");
            /*Intent i=new Intent(this,SecondActivity.class);
            startActivity(i);*/
            onDestroy();
        }
        else{
            Log.d(tag, "Refused");
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

protected Socket getConnection(String ip, int port) throws IOException  {
    try {
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream trustStoreStream = getApplicationContext().getResources().openRawResource(R.raw.server);
        trustStore.load(trustStoreStream, "keypass".toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
        SSLSocketFactory factory = sslContext.getSocketFactory();
        SSLSocket socket = (SSLSocket) factory.createSocket(ip, port);
        //socket.setEnabledCipherSuites(getCipherSuitesWhiteList(socket.getEnabledCipherSuites()));
        return socket;
    } catch (GeneralSecurityException e) {
        Log.e(this.getClass().toString(), "Exception while creating context: ", e);
        throw new IOException("Could not connect to SSL Server", e);
    }
}


    public static String[] getCipherSuitesWhiteList(String[] cipherSuites) {
        List<String> whiteList = new ArrayList<>();
        List<String> rejected = new ArrayList<>();
        for (String suite : cipherSuites) {
            String s = suite.toLowerCase();
            if (s.contains("anon") || //reject no anonymous
                    s.contains("export") || //reject no export
                    s.contains("null") || //reject no encryption
                    s.contains("md5") || //reject MD5 (weaknesses)
                    s.contains("_des") || //reject DES (key size too small)
                    s.contains("krb5") || //reject Kerberos: unlikely to be used
                    s.contains("ssl") || //reject ssl (only tls)
                    s.contains("empty")) {    //not sure what this one is
                rejected.add(suite);
            } else {
                whiteList.add(suite);
            }
        }
        return whiteList.toArray(new String[whiteList.size()]);
    }}
交换数据(输入和输出)时,服务器和客户端都崩溃,没有消息错误

网络正常,android应用程序的权限为.INTERNET、.ACCESS\u WIFI\u STATE、.ACCESS\u network\u STATE

该服务器与Java桌面客户端配合良好

多谢各位

试试截击

您还可以将自定义请求用于截击


我用AsyncTask解决了问题,谢谢大家!
public class SocketThread implements Runnable{
private Socket s1;
private BufferedReader in;
private PrintWriter out;
private String user = "admin";
private String pass = "ciao";

SocketThread(Socket s){
    this.s1=s;
}

public void run(){
    boolean loginDone = false;
    String user1 = null;
    String password1 = null;
    String lati = null;
    String longi = null;
    String via = null;
    System.out.println("Connected");
    try {
        in = new BufferedReader(new InputStreamReader(s1.getInputStream()));
        out = new PrintWriter(s1.getOutputStream(), true);
        user1 = in.readLine();
        password1 = in.readLine();
        System.out.println("User : "+user1+" Password : "+ password1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("error on buffer creation");
        e.printStackTrace();
    }
    System.out.println("Access done, wait for check credential");
    do{
    if(user1.compareTo(user)==0 && password1.compareTo(pass)==0){
        loginDone = true;
        out.println("done");

    }
    else{
        out.println("noaccess");

    }
    }while(loginDone == false);
    System.out.println("Login done");
    try {
        System.out.println("Close done");
        s1.close();
        in.close();
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Thread off");
}}