ping
j'ai développé un truc magnifique
Je montre à ceux qui sauront apprécier
C'est un listener sur socket SCTP. Y a un thread qui attend des connexions, si qqn se connecte ça ouvre un thread qui va s'occuper de recevoir le texte et le renvoyer à tout le monde.
C'est bête mais je suis fier de moi
simulation ici:
http://img22.imageshack.us/img22/175/ecrani.png
[cpp] public class Serveur extends Thread {
private SctpChannel comm;
public void setChannel(SctpChannel communication) { comm = communication; }
public void run(){
try {
while(comm.isOpen() || comm.finishConnect()) {
ByteBuffer txt = ByteBuffer.allocate(32000);
if(comm.receive(txt, null, null).bytes() != -1) {
txt.limit(txt.position());
txt.rewind();
String texte = encodage.decode(txt).toString();
System.out.println("LOG: " + texte);
for (int i = 0; i < channels.size(); ++i) {
ByteBuffer buf = ByteBuffer.wrap(texte.getBytes(encodage));
((SctpChannel) channels.get(i)).send(buf, msg);
System.out.println("Envoyé: " + texte + " à " + ((SctpChannel)channels.get(i)).toString());
}
}
else {
channels.remove(comm);
System.out.println("Envoi message de déco");
for (int i = 0; i < channels.size(); ++i) {
((SctpChannel) channels.get(i)).send(ByteBuffer.wrap("Qqn est parti".getBytes(encodage)), msg);
}
break;
}
}
System.out.println("Fin du thread d'écoute");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class Clients extends Thread {
public void run() {
try {
SctpChannel communication;
while ((communication = serv.accept()) != null) {
channels.add(communication);
ByteBuffer buf = ByteBuffer.wrap("Bienvenue".getBytes(encodage));
communication.send(buf, msg);
Serveur srv = new Serveur();
srv.setChannel(communication);
srv.start();
System.out.println("Client n°" + channels.size() + " connecté");
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}[/cpp]