Application TCP Client Serveur en java

La source proposé permet de connecter plusieurs clients à un même serveur. Reste un gros problème, si après l'établissement d'une connexion le serveur meurt, le client ne reçoit pas d'erreur lors de l'envoie de message. Si quelqu'un trouve une solution (pas bourrine), faites moi signe.

Ce code est composé de trois classes

  • Le serveur : Server.java
  • Une classe de connection : Connect.java
  • Une classe client : Client.java

Server.java

import java.net.ServerSocket ;
import java.net.SocketException ;
 
import java.io.IOException ;
 
public class Server {
 
	private boolean ok=true ;
	private ServerSocket server;
 
	private final int PORT= 5163;
 
	public static void main (String [] args ){
		new Server();
	}
 
	public Server (){
		try{
			server = new ServerSocket (PORT);
		}//try
		catch (SocketException e) {
			System.out.println(e.getMessage() + " 01");
		} // catch
		catch (IOException e) {
			System.out.println(e.getMessage()+" 02");
		}//catch
		System.out.println("En écoute sur le port : " + PORT );
		while (ok) {
			try {
				new Connect(server.accept());
			} //try
			catch (IOException e) {
				System.out.println(e.getMessage()+ " 03");
			}//catch
 
		}//while
	}//Serve
 
}//class

Connect.java

import java.net.Socket ;
 
import java.lang.Thread ;
import java.lang.IllegalThreadStateException ;
import java.lang.NullPointerException ;
 
import java.io.PrintWriter ;
import java.io.BufferedWriter ;
import java.io.OutputStreamWriter ;
import java.io.IOException ;
 
import java.util.ArrayList ;
public class Connect implements Runnable {
	private Thread thd ;
	private Socket socket ;
	private PrintWriter out ;
	//private InputStream in  ;
 
	private static ArrayList<Connect> listC = new ArrayList<Connect>(1);
	public Connect (Socket socket_) {
		listC.add(this);
		socket = socket_ ;
		thd = new Thread (this) ;
		thd.start();
	}//connect
 
	public void run () {
		try {
			out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true );
//in  = new BufferedReader(new  InputStreamReader(socket.getInputStream ()));
		}//try
 
		catch (IOException a ) {
			System.out.println("erreur2") ;
			a.printStackTrace() ;  }//catch IO
 
			while (true){
				try {
					/*if (in.ready()==true) {*/
 
//-------------------------Reception des caracteres----------------------------------
					int t;
					String tmp="" ;
					do {t = socket.getInputStream().read() ; tmp = tmp + (char)t; }
					while (t!=10) ;
 
//-----------------------------------------------------------------------------------
 
					System.out.println("|" + tmp + "|" ) ;
// 					dispatch(tmp, );
					switch(tmp.charAt(0)) {
						case 0 :
							System.out.println("MSG> " + tmp) ;
							dispatch(tmp , socket) ;
							break ;
						case 1 :
							System.out.println("ATK> " + tmp) ;
							break ;
						case 2 :
							System.out.println("MOV> " + tmp) ;
							break ;
						default :
							System.out.println("INC>" + tmp) ;
					}//switch
				}//try
				catch (IOException a ) {
					System.out.println("erreur2") ;
					a.printStackTrace() ;  }//catch IO
 
			}//while
	}//run
 
	public static void dispatch (String scor, Socket socket) {
		for (int i = 0 ; i < listC.size() ; i++) {
			if (listC.get(i).socket!=socket){try{listC.get(i).send(scor) ;}
				catch (NullPointerException e) {
					System.out.println("morte la connection :(" ) ;listC.remove(i) ;
				}//catch
			}//if
		}//for
	}//dispatch
 
 
	public void send (String scor){
		out.println(scor) ;
	}//send
}//class

Client.java

import java.net.Socket ;
 
import java.io.PrintWriter ;
import java.io.OutputStreamWriter;
import java.io.IOException ;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.DataOutputStream;
 
 
public class Client implements Runnable {
 
 
	private String addr;
	private String port;
	private PrintWriter out;
	private DataOutputStream out2;
	private	Socket socket;
	private Thread thd;
 
public static void main (String [] args) {
	if(args.length == 0){
		System.out.println("Tentative de connection à localhost:5163");
		new Client("localhost","5163");
	}else{
		new Client(args[0], args[1]);
	}
}
 
 
public Client(String addr, String port){
	try{
		socket = new Socket (addr , Integer.parseInt(port));
		socket.setSoLinger(true , 10);
	}//try
	catch (IOException a ){
		System.out.println("erreur2 " + a.getMessage()) ; a.printStackTrace() ;
	}
 
	try{
		out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true );
		out2 = new DataOutputStream(socket.getOutputStream());
	}//try
 
	catch (IOException a ) {
		System.out.println("erreur3") ;
		a.printStackTrace();
	}//catch IO
	thd = new Thread (this) ;
	thd.start();
 
		while (true){
			try {
//-----------------------------------------------------------------------------------
				int t;
				String tmp="" ;
				do {
					t = socket.getInputStream().read();
					tmp = tmp + t;
				}while (t!=0) ;
 
//-----------------------------------------------------------------------------------
					System.out.println(tmp);
			}//try
			catch (IOException a ) {
				System.out.println("erreur4") ;
				a.printStackTrace() ;  }//catch IO
		}//while
}//Client
 
public void run (){
	BufferedReader entree = new BufferedReader(new InputStreamReader(System.in));
	try {
		out.println(entree.readLine());
		out.flush();
// 		out2.writeBytes(entree.readLine() + "\n");
	} catch( IOException e ) {
		System.out.println("Erreure lors de l'envoie");
		e.printStackTrace();
	}
}
 
}//class