1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serveur2;
import java.io.*;
import java.net.*;
import java.util.*;
/**
*
* @author Sephiroth
*/
public class Main {
private static List<Socket> clients = new ArrayList();
private static List<ThreadClient> tlist = new ArrayList();
private static int nbclients = 0;
public static void main(String[] args) {
try {
System.out.println(" HellGate : Selectionnez le port du portail : ");
BufferedReader cmd = new BufferedReader(new InputStreamReader(System.in));
int port = Integer.parseInt(cmd.readLine());
System.out.println(" HellGate : Ouverture du portail sur le port : "+port);
try {
ServerSocket serveur = new ServerSocket(port);
System.out.println(" HellGate : Portail ouvert avec succes sur le port : "+port);
System.out.println(" HellGate : Tapez <close> pour fermer le portail.");
Thread commandes = new Commandes();
commandes.start();
while(true){
Socket client;
client=serveur.accept();
PrintWriter out;
out=new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
nbclients++;
clients.add(client);
System.out.println(" HellGate : Connection de "+client.getInetAddress().getHostAddress()+" (ID="+clients.indexOf(client)+")"+" sur le portail !");
System.out.println(" HellGate : Nombre de clients connectés : "+nbclients);
out.println("");out.flush();
out.println("");out.flush();
out.println(" I IIHH. ");out.flush();
out.println(" .HHHHHHHHI. : : IHHHMHHIIH.");out.flush();
out.println(" IIIIHI.HIIIH :.HI H I I.H:H:H:..II ");out.flush();
out.println(" .I:.HI:.H:.::H .. H HH H :.H.H..I.:..H ");out.flush();
out.println(" IH::HI::IH::.M.H H.H:. H.I..I...I:::.I");out.flush();
out.println(" :IIII:.I:I::I:I..H HHIHHM. H:.H::I.::H:II:: ");out.flush();
out.println(" IIIII:.I:HIIII:H:.:. :I:HH::IH H::::::H:IIH:III:I ");out.flush();
out.println(" :IIHHI::IIIHIIIIII:::H I: ::HH H:::H::IH::IIIIIIII: ");out.flush();
out.println(" IHIMMHHIIHIIIHHHHIIHIIII:H: I:::IH. IHIIHI:IIHHHIIIIIHIHHHHI ");out.flush();
out.println(" HMIHMMHHIHHIHIIHMMMIIIIIIIIHHH..:HIIH. HHIIIIHIHIHM.HIIIHIIIHHMMHI ");out.flush();
out.println(" HHIMHMHIHMMHH HHHIHHHHIIIIH:I::MIHHIIIHHHHHIH IIHHHIIIHMMMHI ");out.flush();
out.println(" :IMMHMMMHH HHHI HIHMI:IIH::.IIHH. HHH HIIHHHIIHMM ");out.flush();
out.println(" MHIMMMMHH H HMHH:IIHIII:I :IHHHHHHMM ");out.flush();
out.println(" IIHHMMHMI HHH :IIHHH:H IIHHHHHIHH ");out.flush();
out.println(" IHMHMMHII IIIH:: HHHMHMMHIH ");out.flush();
out.println(" I.HHHHMH: H:IHM : HHMMMMMMHH ");out.flush();
out.println(" I :MHH HH:MH:H HHMMH..:HHM ");out.flush();
out.println(" MH MI:MH:I IMM H ");out.flush();
out.println(" H: IH:MMIMM .HH ");out.flush();
out.println(" H IHMIHIMH IM ");out.flush();
out.println(" H IH:HMH IH");out.flush();
out.println(" HIIIII");out.flush();
out.println(" :IHHH");out.flush();
out.println(" :IH");out.flush();
out.println(" IIM .HH");out.flush();
out.println(" II. :I H");out.flush();
out.println(" II M ");out.flush();
out.println(" HI. H");out.flush();
out.println(" :H MI");out.flush();
out.println(" IIM MH");out.flush();
out.println(" .II HHH");out.flush();
out.println(" IIIIII");out.flush();
out.println("");out.flush();
out.println(" HellGate : Bienvenue !");out.flush();
out.println(" HellGate : Tapez <quit> pour vous deconnecter. ");out.flush();
Thread threadclient = new ThreadClient(client,clients.indexOf(client));
threadclient.start();
tlist.add(threadclient); <------- ERREUR ICI*
}
}catch (IOException e){
System.out.println("Erreur : "+e.getMessage());
}
} catch (IOException ex) {
System.out.println("Erreur : "+ex.getMessage());
}
}
public static void deleteClient(int id){
nbclients--;
System.out.println(" HellGate : Le client "+clients.get(id).getInetAddress().getHostAddress()+" (ID="+id+")"+" s'est déconnecté !");
System.out.println(" HellGate : Nombre de clients connectés : "+nbclients);
clients.remove(id);
}
synchronized public static void sendAll(String msg,int id){
PrintWriter outall;
try{
if (id==-1){
for (int i=0;i<clients.size() ;i++){
outall = new PrintWriter(new OutputStreamWriter(clients.get(i).getOutputStream()));
outall.println(msg);
outall.flush();
}
}else{
for (int i=0;i<clients.size() ;i++){
if (i!=id){
outall = new PrintWriter(new OutputStreamWriter(clients.get(i).getOutputStream()));
outall.println(clients.get(id).getInetAddress().getHostAddress()+" (ID="+id+") :"+msg);
outall.flush();
}
}
}
}catch(IOException exx){
System.out.println("Erreur : "+exx.getMessage());
}
}
} |