首先我运行 serverur_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);
}
}
}
}
}
/
///类连接服务器
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;
}
}
//初始化通信类
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 类:我从中得到错误
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();
}
}
}
}
运行 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)
对不起我的英语,我需要你的帮助!