Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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_Connection_Socketexception - Fatal编程技术网

java套接字中的连接重置异常,我尝试过,但没有';我没听懂,这是密码

java套接字中的连接重置异常,我尝试过,但没有';我没听懂,这是密码,java,connection,socketexception,Java,Connection,Socketexception,首先,我运行serveu_final.java,为客户端创建线程,从客户端获取登录名和密码,在第二个线程中,在数据库中验证它们,然后向客户端发送“ok”并初始化通信 public class Serveur_final { private ServerSocket sock_serv=null; private Socket client_entrant=null; private static Vector<String> nom_client=null;

首先,我运行serveu_final.java,为客户端创建线程,从客户端获取登录名和密码,在第二个线程中,在数据库中验证它们,然后向客户端发送“ok”并初始化通信

public class Serveur_final {
    private ServerSocket sock_serv=null;
    private Socket client_entrant=null;
    private static Vector<String> nom_client=null;
    private static Vector<connect_serveur> list_client=null;
    private int port=1991;

    public Serveur_final() throws IOException{
        initialisation();

    }
   public void initialisation() throws IOException {
        sock_serv=new ServerSocket(port);
        System.out.println("ecoute au"+port);
        list_client=new Vector<connect_serveur>();
        nom_client=new Vector<String>();
        while(true){
            client_entrant=sock_serv.accept();
            connect_serveur con=new connect_serveur(client_entrant);
             list_client.add(con) ;


        }

    }
    public static void ajouter_list(String nom_cli){
          nom_client.add(nom_cli);

              diffusion_list(nom_client);



      }
    public static void supprimer_nom_client(String con){
        for(String nom: nom_client){
            if(nom.equals(con)){
        nom_client.remove(nom);
        }
        }
    }

    public static void supprimer_client(connect_serveur con){
        for(connect_serveur nom: list_client){
            if(nom.equals(con)){
        list_client.remove(nom);}
        }
    }
    public static  Vector<connect_serveur> getList_client(){
        return list_client;
    }
    public static void diffusion_list(Vector<String> client) {
        for(connect_serveur cl: list_client){
            for(int i=0;i<client.size();i++){

                cl.diffuser(nom_client.get(i));

            }
        }

    }

    public static void diffusion_message(String client,String message) {
        for(connect_serveur cl: list_client){
            /*for(int i=0;i<nom_client.size();i++){*/

            cl.getThead_client().getEnvoi_msg().diffusion(client, message);
            /*}*/
        }

    }
    public static void deconnection(String pseudo) {
        for(connect_serveur cl: list_client){
            if(cl.getPseudo().equals(pseudo)){
                list_client.remove(cl);
            }
            for(int i=0;i<nom_client.size();i++){

            if(pseudo.equals(nom_client.get(i))){
                nom_client.remove(i);
            }


            }
        }   

    }   


}
//envoyer_msg类:我从中得到错误

public class envoyer_message extends Thread {

    private PrintWriter emettre;
    private BufferedReader reception;
    private String pseudo;
    public envoyer_message(String pseudo, PrintWriter envoi, BufferedReader reception) {
        this.emettre=envoi;
        this.reception=reception;
        this.pseudo=pseudo;
        System.out.println("dans envoyer dja");
        start();
    }
   public void run(){
       String message;
      emettre.println("bien connectee");
     try {
         while (true) {
             message = reception.readLine();
             if(message=="!!>>fin<<!!"){

                    Serveur_final.deconnection(pseudo);

                    }

             Serveur_final.diffusion_message(pseudo, message); 
         } // end of while
     } // try
     catch (Exception ex) {
         System.out.println(ex.getMessage());
     }

    }
public void diffusion(String pseudo2, String message) {
    emettre.println(">>:"+pseudo2+"dit :"+message);

}

}
对不起我的英语,我需要你的帮助

我已经试过了所有的窍门

“所有提示”都说了同样的话,或者他们应该这么说。您已写入另一端已关闭的连接。应用程序协议错误


也没有足够的EOS签入您的代码。例如,您只是假设您同时收到用户名和密码。

我不知道这是否可以解决此问题,但我注意到您的检查:

if(message=="!!>>fin<<!!"){

if(消息=="!!>>finYou应该发布一个较小的代码..请只发布相关部分..我不知道Maroun如果您能帮助我,我更愿意发布所有代码,因为我不知道错误源我已经检查过,在服务器端我得到了用户名和密码,当这些信息正确时,它会返回ok给客户端,但在输入envoyer\u msg t后出现相同的错误run方法不起作用的线程,但是您不能在代码中假设它。您必须进行检查。
public class envoyer_message extends Thread {

    private PrintWriter emettre;
    private BufferedReader reception;
    private String pseudo;
    public envoyer_message(String pseudo, PrintWriter envoi, BufferedReader reception) {
        this.emettre=envoi;
        this.reception=reception;
        this.pseudo=pseudo;
        System.out.println("dans envoyer dja");
        start();
    }
   public void run(){
       String message;
      emettre.println("bien connectee");
     try {
         while (true) {
             message = reception.readLine();
             if(message=="!!>>fin<<!!"){

                    Serveur_final.deconnection(pseudo);

                    }

             Serveur_final.diffusion_message(pseudo, message); 
         } // end of while
     } // try
     catch (Exception ex) {
         System.out.println(ex.getMessage());
     }

    }
public void diffusion(String pseudo2, String message) {
    emettre.println(">>:"+pseudo2+"dit :"+message);

}

}
public class client_chat  {
    private Socket socket_client;
    private String connected="",passwd,login,adress_server="localhost";
    private int Port=1991;
    private BufferedReader entrer=null;
    private PrintWriter sortie=null;
    private static client_chat client;
    private boolean connecte=false;
    private BufferedReader clavier ;
    private String message;
    private boolean fermer;
    public client_chat(){
        try {
            this.socket_client=new Socket(adress_server,Port);
            connecte=getConnection("nao", "nao");
            init(connecte);

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.getMessage();
        }

    }
    public static client_chat getIntance(){
        if(client==null){
            client=new client_chat();
        }
        return client;

    }
public boolean getConnection(String pseudo,String passwd){
    this.login=pseudo;
    this.passwd=passwd;

    if(socket_client!=null){


        try {
            entrer=new BufferedReader(new InputStreamReader(socket_client.getInputStream()));
            sortie=new PrintWriter(socket_client.getOutputStream());
            sortie.println(pseudo);
            sortie.flush();
            sortie.println(passwd);
            sortie.flush();
            connected=entrer.readLine();
            System.out.println(connected);
            if(connected=="ok"){
                connecte=true;
                System.out.println(connected);

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



}

return connecte;    
}
public void init(Boolean con) throws IOException{
    if(con ){

        new ThreadLecture().start();
        while(!fermer){
            clavier=new BufferedReader(new InputStreamReader(System.in));
            sortie.println(clavier.readLine());

        }
        entrer.close();
        sortie.close();
        socket_client.close();
    }

}

public void setLogin(String ps){
    this.login=ps;
}
public String getLogin(){
    return this.login;
}
public void setPasswd(String ps){
    this.passwd=ps;
}
public String getPasswd(){
    return this.passwd;
}
class ThreadLecture extends Thread{

public void run() {

    try {

        System.out.println("dans runcli");
        while(true){
            message=entrer.readLine();
            System.out.println(message);
            if(message.indexOf("bye4")!=-1 ||message.indexOf ("!!>>fin<<!!")!=-1)
            break;  


    enter code here
        }
        fermer=true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}
}
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at modeles.envoyer_message.run(envoyer_message.java:30)
if(message=="!!>>fin<<!!"){