Commit 7e066f81 authored by Songyang Wang's avatar Songyang Wang
Browse files

Replace chatServer.java

parent 5b289042
......@@ -4,6 +4,7 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.security.PublicKey;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.io.IOException;
import java.io.InputStream;
......@@ -40,6 +41,7 @@ public class chatServer {
/*Constructeur*/
public ThreadedEchoHandler(Socket s) {
this.socket = s;
System.out.println(this.socket);
}
/**
......@@ -56,11 +58,61 @@ public class chatServer {
OutputStream out = socket.getOutputStream();
out.write(("Entrez votre pseudo: ").getBytes());
byte b[] = new byte[35];
byte b[] = new byte[50];
String MessageClient; //Message vient du client
do {
in.read(b);
try{
in.read(b);
}catch (SocketException se) { //Une déconnextion imprévue
// TODO Auto-generated catch block
se.printStackTrace();
this.socket.close();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = socketList.size()-1; i >= 0; i--){
if (this.socket.equals(socketList.get(i))){
// supprimer
socketList.remove(socketList.get(i));
System.out.println("Supprimer "+this.socket);
System.out.println(socketList);
}
}
for (int i = noms.size()-1; i >= 0; i--){
if (this.name.equals(noms.get(i))){
// supprimer
System.out.println(noms.get(i)+" a quitté ce chatroom ");
System.out.println("********************************");
//out.write((noms.get(i)+" a quitté ce chatroom ").getBytes()); //?
for (int j = 0; j < socketList.size(); j++) {
socket = socketList.get(j);
out = socket.getOutputStream();
out.write((noms.get(i)+" a quitté ce chatroom ").getBytes());
out.write("********************************".getBytes());
}
noms.remove(noms.get(i));
break;
}
}
System.out.println(noms);
break;
}
MessageClient = new String(b);
MessageClient.trim();
......@@ -69,8 +121,26 @@ public class chatServer {
Si on a recu, on met nomRecu=1
*/
if (this.nomRecu == 0) {
noms.add(MessageClient.trim());
this.name = MessageClient.trim();
int doublon=0; //pas de doublon en noms
for(int k = 0;k < noms.size(); k ++){
System.out.println(noms.get(k));
if(noms.get(k).equals(this.name)) {
doublon=doublon+1;
}
}
if(doublon!=0) {
out.write(("Il y a un doublon, Votre nouveau pseudo est: "+ this.name+(doublon+1)).getBytes() );
doublon=doublon+1;
noms.add(this.name+(doublon));
this.name=this.name+(doublon);
} else {
noms.add(this.name);
}
this.nomRecu=1;
System.out.println("noms: " + noms);
}
......@@ -81,22 +151,30 @@ public class chatServer {
if (!socket.isClosed()) {
out = socket.getOutputStream();
System.out.println("size " + socketList.size());
System.out.println(this.bienvenueRecu);
System.out.println(socketList);
//System.out.println(this.bienvenueRecu);
/*
bienvenueRecu==0 : On n'a pas envoyé le bienvenue du nouveau client
Si on a recu, on met bienvenueRecu=1
Si on a recu et envoyé, on met bienvenueRecu=1
*/
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (this.bienvenueRecu == 0) {
for (int j = 0; j < socketList.size(); j++) {
socket = socketList.get(j);
out = socket.getOutputStream();
out.write((MessageClient.trim() + " a rejoint la conversation").getBytes());
out.write((this.name + " a rejoint la conversation").getBytes());
out.write("-----------------------------------".getBytes());
System.out.println(MessageClient.trim() + " a rejoint la conversation");
System.out.println(this.name + " a rejoint la conversation");
System.out.println("-----------------------------------");
}
......@@ -110,10 +188,48 @@ public class chatServer {
}
if (MessageClient.equalsIgnoreCase("quit"))
break;
//if (MessageClient.equalsIgnoreCase("quit")) break;
if (MessageClient.startsWith("quit")) {
this.socket.close();
for (int i = socketList.size()-1; i >= 0; i--){
if (this.socket.equals(socketList.get(i))){
// supprimer
socketList.remove(socketList.get(i));
System.out.println("Supprimer "+this.socket);
}
}
System.out.println(socketList);
for (int i = noms.size()-1; i >= 0; i--){
if (this.name.equals(noms.get(i))){
// supprimer
System.out.println(noms.get(i)+" a quitté ce chatroom ");
out.write("********************************".getBytes());
//out.write((noms.get(i)+" a quitté ce chatroom ").getBytes());
for (int j = 0; j < socketList.size(); j++) {
socket = socketList.get(j);
out = socket.getOutputStream();
out.write((noms.get(i)+" a quitté ce chatroom ").getBytes());
out.write("********************************".getBytes());
}
noms.remove(noms.get(i));
}
}
System.out.println(noms);
}
b = new byte[35];
b = new byte[50];
} while (!MessageClient.startsWith("quit")); //Boucle jusqu'à recevoir "quit"
......@@ -144,6 +260,7 @@ public class chatServer {
/*Boucle infinie pour attendre un nouveau client */
while (true) {
Socket socket = serverSocket.accept();
//socket.setSoTimeout(10*1000); //
socketList.add(socket);
System.out.println(socketList);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment