Bonsoir à tous, vous pouvez m'aider à créer une session privée avec un autre client le code de mon application est le suivant:
public class Serveur {
private ServerSocket ss;
private Hashtable FluxSortie=new Hashtable();
public Serveur(int port) throws IOException {
Ecouter(port);
}
private void Ecouter(int port) throws IOException{
ss=new ServerSocket(port);
System.out.println("Ecoute sur"+ss);
while(true){
Socket s= ss.accept();
System.out.println("Connexion depuis"+s);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
FluxSortie.put(s,dout);
new ServeurThread(this,s);
}
}
Enumeration getFluxSortant(){
return FluxSortie.elements();
}
public void sendToAll(String Msg){
synchronized(FluxSortie){
for(Enumeration e = getFluxSortant();e.hasMoreElements();){
DataOutputStream dout=(DataOutputStream)e.nextElement();
try{
dout.writeUTF(Msg);}
catch(IOException ie){
System.out.println(ie);
}
}
}
}
void remove(Socket s) {
synchronized(FluxSortie){
System.out.println("Deconnexion de"+s);
FluxSortie.remove(s);
try{
s.close();
}
catch(IOException e){
System.out.println("Erreur de fermeture de"+s);
}
}
}
public static void main(String [] args)throws IOException{
int port= Integer.parseInt(args[0]);
new Serveur(port);
}
}
public class Client extends Frame implements Runnable{
TextField tf=new TextField();
TextArea ta=new TextArea();
Socket s;
DataInputStream din;
DataOutputStream dout;
public Client(String Host,int Port) {
setLayout(new BorderLayout());
add(tf,BorderLayout.NORTH);
add(ta,BorderLayout.CENTER);
setVisible(true);
setSize(400,400);
setTitle("Sallon de Discusion");
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
traiterMsg(ev.getActionCommand());
}
});
try{
s=new Socket(Host,Port);
din= new DataInputStream(s.getInputStream());
dout= new DataOutputStream(s.getOutputStream());
new Thread (this).start();
}
catch(IOException e){
System.out.println(e);
}
}
private void traiterMsg(String Msg) {
try{
dout.writeUTF(Msg);
tf.setText("");
}
catch(IOException ie){
System.out.println(ie);
}
}
public void run(){
try{
while(true){
String message=din.readUTF();
ta.append(message+"\n");
}
}
catch(IOException ee){
System.out.println(ee);
}
}
public static void main(String [] args){
new Client("127.0.0.1",2008);
new Client("127.0.0.1",2008);
}
}
public class ServeurThread extends Thread{
private Serveur serveur;
private Socket socket;
public ServeurThread(Serveur serveur,Socket socket) {
this.serveur=serveur;
this.socket=socket;
start();
}
public void run(){
try{ DataInputStream din=new DataInputStream(socket.getInputStream());
while(true){
String Msg=din.readUTF();
serveur.sendToAll(Msg);
}}
catch(IOException e){
System.out.println(e);
}
finally{
serveur.remove(socket);
}
}
}