Ciao
Qual'è un metodo semplice per inviare in broadcast un messaggio che scrive un utente in una chat? Ho trovato vari codici a riguardo, ma troppo complessi per i miei scopi.
Sotto posto il codice che ho provato a modificare, è semplice ma non invia il testo a tutti gli utenti connessi, solo all'utente di cui si specifica il nome. Ho iniziato facendo un ciclo for, conto il numero di coppie nick-valore presenti nella hashMap. Vorrei che il messaggio venga letto da tutti i clienti presenti nella hashMap. Ma non funziona
Premetto che non sono molto capace a programmare in java, ho iniziato da poco!
codice:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.util.*;
public class ChatServerT {
private static int port = 8888;
public static void main (String[] args) throws IOException {
ServerSocket server = null;
try {
server = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.err.println(e);
System.exit(1);
}
Socket client = null;
while(true) {
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
System.exit(1);
}
Thread t = new Thread(new ClientConn(client));
t.start();
}
}
} // End class ChatServerT
class ChatServerProtocol {
private String nick;
private ClientConn user;
private static HashMap<String, ClientConn> nicks = new HashMap<String, ClientConn>();
private static final String msg_OK = "OK";
private static final String msg_NICK_IN_USE = "NICK IN USE";
private static final String msg_SPECIFY_NICK = "SPECIFY NICK";
private static final String msg_SEND_FAILED = "FAILED TO SEND";
private static boolean add_nick(String nick, ClientConn c) {
if (nicks.containsKey(nick)) {
return false;
} else {
nicks.put(nick, c);
return true;
}
}
public ChatServerProtocol(ClientConn c) {
nick = null;
user = c;
}
private void log(String msg) {
System.err.println(msg);
}
public boolean isAuthenticated() {
return ! (nick == null);
}
private String authenticate(String msg) {
if(msg.startsWith("NICK")) {
String tryNick = msg.substring(5);
if(add_nick(tryNick, this.user)) {
log("Nick " + tryNick + " joined.");
this.nick = tryNick;
return msg_OK;
} else {
return msg_NICK_IN_USE;
}
} else {
return msg_SPECIFY_NICK;
}
}
// Invia un messaggio in broadcast
private boolean sendMsg(String recipient, String msg) {
if (nicks.containsKey(recipient)) {
int e = nicks.size();
for(int i=0;i<e;i++){
ClientConn c = nicks.get(recipient);
c.sendMsg(nick + ": " + msg);
}
return true;
} else {
return false;
}
}
// l'input del cliente viene processato
public String process(String msg) {
if (!isAuthenticated())
return authenticate(msg);
String[] msg_parts = msg.split(" ", 2);
if(sendMsg(msg_parts[0], msg_parts[0])) return msg_OK;
else return msg_SEND_FAILED;
}
} // End class ChatServerProtocol
class ClientConn implements Runnable {
private Socket client;
private BufferedReader in = null;
private PrintWriter out = null;
ClientConn(Socket client) {
this.client = client;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.err.println(e);
return;
}
}
public void run() {
String msg, response;
ChatServerProtocol protocol = new ChatServerProtocol(this);
try {
while ((msg = in.readLine()) != null) {
response = protocol.process(msg);
out.println("SERVER: " + response);
}
} catch (IOException e) {
System.err.println(e);
}
}
public void sendMsg(String msg) {
out.println(msg);
}
} // End class ClientConn