简体   繁体   English

我尝试了java套接字中的连接重置异常,但是我没有发现,这里是代码

[英]a connection reset exception in java socket ,i have tried but i didn't catch this out,here is code

First i run serveur_final.java,creating thread for clients,getting login and password from client and in the second thread verify them in database and after sending "ok" to client and initialize communication 首先,我运行serveur_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);
            }


            }
        }   

    }   


}

/ /

///class connect-serveur ///类connect-servur

public class connect_serveur extends Thread {

    private Socket client;
     private BufferedReader reception;
     private static PrintWriter envoi;
     private String pseudo,passwd;
    /* private static Vector<String> list_nom_client;*/
     initialisation_communication thread_client;
    private boolean verif=false, autorisation=false;
    public connect_serveur(Socket client_entrant) throws IOException {

        this.client=client_entrant;
        envoi=new PrintWriter(client.getOutputStream());
        reception=new BufferedReader(new InputStreamReader(client.getInputStream()));
        pseudo=reception.readLine();
        passwd=reception.readLine();
        verif=verifier_info(pseudo, passwd);
        setPseudo(pseudo);
        start();
    }

    @Override
    public void run() {

        try {

                if(!verif){
                    envoi.println("bye4");
                    envoi.flush();
                    Serveur_final.supprimer_client(this);
                    reception.close();
                    envoi.close();
                    client.close();
                    Serveur_final.deconnection(pseudo);

                }
                envoi.println("ok");
                envoi.flush();
                 Serveur_final.ajouter_list(pseudo);
                thread_client=new initialisation_communication(pseudo, envoi,reception);
                setThead_client(thread_client);






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

    }
public void setPseudo(String pseudo2) {
        this.pseudo=pseudo2;

    }
public String getPseudo() {
    return this.pseudo;

}
public initialisation_communication getThead_client(){
    return thread_client;
}
public  void setThead_client(initialisation_communication com ){
    thread_client=com;

}

/*public static void diffuser_list(Vector<String>client){
    list_nom_client=client;

        for(int i=0;i<list_nom_client.size();i++){

            diffuser(list_nom_client.get(i));
            Serveur_final.diffusion_list();
        }


    }*/
 public static  void diffuser(String client) {
        System.out.println("en diffusion");
    envoi.println("0>>:"+client);
    envoi.flush();


}

    boolean verifier_info(String pseudo, String passwd) {

     autorisation=connexion_bdd.getInstance().verification_log(pseudo, passwd);
     System.out.println("AUTORISER"+autorisation);
    if(autorisation)
    { 
           envoi.println("ok");
           envoi.flush();
            System.out.println("adding user");



    }

    else {
           envoi.println("no");
           envoi.flush();

        }

return autorisation;
}



}

//initialisation-comm class //初始化通讯类

public class initialisation_communication extends Thread {
    private String pseudo;
    private BufferedReader reception;
    private PrintWriter envoi;
    private envoyer_message envoi_msg;
    private recevoir_message recoi_msg;
    public initialisation_communication(String pseudo,PrintWriter envoi,BufferedReader reception) {
        // TODO Auto-generated constructor stub
        this.envoi=envoi;
        this.reception=reception;
        this.pseudo=pseudo;

        start();

    }

    @Override
    public void run() {
        /*envoi=new PrintWriter(socket_client.getOutputStream());
        BufferedReader reception = new BufferedReader(new InputStreamReader(socket_client.getInputStream()));*/
        System.out.println("initialiser");
        envoi_msg=new envoyer_message(pseudo,envoi,reception);

        recoi_msg=new recevoir_message(reception);
        setEnvoi_msg(envoi_msg);



        }
    public envoyer_message getEnvoi_msg(){
        return envoi_msg;
    }
    public void setEnvoi_msg(envoyer_message com ){
        this.envoi_msg=com;

    }

    }

//envoyer_msg class:from which i'm getting errors // 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);

}

}

and finally my client 最后是我的客户

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();
    }

}

}
}

error message after running client_chat.java 运行client_chat.java后出现错误消息

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)

sorry for my english, I need your help please! 对不起,我的英语,请帮忙!

I have tried all the tips 我尝试了所有技巧

"All the tips" say the same thing, or they should. “所有技巧”都说相同的话,或者应该。 You have written to a connection that had already been closed by the other end. 您已写入另一端已关闭的连接。 An application protocol error. 应用程序协议错误。

There isn't nearly enough EOS-checking in your code either. 您的代码中也几乎没有足够的EOS检查。 For example you are just assuming you receive both the username and the password. 例如,您只是假设您同时收到了用户名和密码。

I don't know if this will fix this, but i noticed your check: 我不知道这是否可以解决此问题,但我注意到您的支票:

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

in envoyer_msg.run() is wrong. envoyer_msg.run()中的错误。

message is a String, string comparison is done by equals()/compareTo(). message是一个字符串,字符串比较由equals()/ compareTo()完成。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM