ciao ragazzi...ho costruito una chat java pero sono arrivato ad un punto che non so piu cosa manca...l'interfaccia grafica mi e stata già fornita io ho solo implementato theed e socket. il server sembra funzionare solo che quando scrivo nella chat non visualizza quello che scrivo!Non riesco a capire quello che manca e dove manca...!vi incollo il codice anche se un po lunghetto!manca solo la classe GUI che implemente l'interfaccia che si chiama appunto ChatClientGUI.
grazie dell'aiuto ragazzi!
codice:/* IMPORT the required classes */ import java.net.*; import java.io.*; import java.util.*; //import java.lang.*; public class ChatServer{ /* GLOBAL variables */ private static String usage = "usage: java Server <8008>"; protected ServerSocket serverSocket; protected int port; Vector clients; Vector nicks; /* Default constructor */ public ChatServer(){} /* Variable port constructor */ public ChatServer(int port){} /* Initiates the server to listen on a port */ public void listen() throws IOException { clients=new Vector(); nicks=new Vector(); Socket client; serverSocket = new ServerSocket(port); /* Accept new connection */ while(true){ client = startListening(); clients.add(client); startListeningSingleClient(client); try { //Handle new incoming connections. } catch(Exception e){} } } public class ParallelServer implements Runnable { Socket client; BufferedReader is; DataOutputStream os; public ParallelServer(Socket client) { this.client = client; try { // Stream di byte utilizzato per la comunicazione via socket is = new BufferedReader(new InputStreamReader(client.getInputStream())); os = new DataOutputStream(client.getOutputStream()); } catch (Exception ex) { } } public void run() { try { while (true) { sendMessage(receiveMessage(is), is, os, client); } } catch (Exception ex) { } } } public Socket startListening() throws IOException { System.out.print("In attesa di chiamate dai Client... "); return serverSocket.accept(); } private void startListeningSingleClient(Socket client) { Runnable r = new ParallelServer(client); Thread t = new Thread (r); t.start(); } public String receiveMessage(BufferedReader is) throws IOException { String strReceived = is.readLine(); if(strReceived.startsWith("~")) { // Controlla se il nick è gia' presente if (nicks.contains(strReceived.substring(1))) { strReceived = "NOGOODUSER"; } else nicks.add(strReceived.substring(1)); } else if(strReceived.startsWith("^")) { nicks.remove(strReceived.substring(1)); } System.out.println("Il Client ha inviato: " + strReceived); return strReceived ; } public void sendMessage(String recMsg, BufferedReader is, DataOutputStream os,Socket client ) throws IOException { int idxToRemove = -1; for(Iterator all = clients.iterator(); all.hasNext();) { Socket cl = (Socket)all.next(); if(recMsg.startsWith("~")) addNewUser(recMsg, cl, client); else if(recMsg.startsWith("^")) idxToRemove = removeUser(recMsg, idxToRemove, cl, client); else { if(recMsg.equals("NOGOODUSER") && cl.equals(client)) { new DataOutputStream(cl.getOutputStream()).writeBytes(recMsg + "\n"); idxToRemove = removeUser(recMsg, idxToRemove, cl, client); } else broadcastMessage(recMsg, cl); } } // Se ci sono client da rimuovere, tale rimozione viene effettuata // solo al termine del ciclo per evitare problemi sugli indici. if (idxToRemove > -1) clients.remove(idxToRemove); } private void addNewUser(String recMsg, Socket cl, Socket client) throws IOException { if (cl.equals(client)) { StringBuffer response = new StringBuffer(); for (int i = 0; i < nicks.size(); i++) { response.append(nicks.get(i)); response.append("~"); } String strResponse = "OKNEWUSER" + response.toString(); System.out.println(strResponse); new DataOutputStream(cl.getOutputStream()).writeBytes(strResponse + "\n"); } else { String strName = recMsg.substring(1); new DataOutputStream(cl.getOutputStream()).writeBytes("NEWUSER" + strName + "\n"); } } private int removeUser(String recMsg, int idxToRemove, Socket cl, Socket client)throws IOException { if (cl.equals(client)) { close(cl.getInputStream(), cl.getOutputStream(), cl); idxToRemove = clients.indexOf(cl); } else { String strName = recMsg.substring(1); new DataOutputStream(cl.getOutputStream()).writeBytes("REMOVEUSER" + strName + "\n"); } return idxToRemove; } private void broadcastMessage(String recMsg, Socket cl) throws IOException { // Il messaggio "NOGOODUSER" deve essere inviato // solo ad uno specifico client (gestito nella sendMessage). if(! recMsg.equals("NOGOODUSER")) new DataOutputStream(cl.getOutputStream()).writeBytes(recMsg + "\n"); } public void close(InputStream is, OutputStream os, Socket client)throws IOException { System.out.println("Chiamata close"); // chiusura della comunicazione con il Client os.close(); is.close(); System.out.println("Chiusura chiamata Client: " + client.getInetAddress().getHostName() + "su porta: " + port); client.close(); } public static void main(String[] args) throws IOException { if(args.length < 1) { System.out.println(usage); } else { try{ ChatServer x = new ChatServer(Integer.parseInt(args[0])); x.listen(); }catch(Exception e){ } } ChatServer serverChat = new ChatServer(); try { serverChat.listen(); } catch (Exception ex){} } }codice:import javax.swing.UIManager; import java.net.*; import java.io.*; //import java.lang.Integer; import java.util.*; public class ChatClient extends ChatClientGUI{ private Socket socket; private DataOutputStream os; private BufferedReader is; /** * Metodo per connettersi al Chat Server * @param server * on a given port * @param port */ public boolean connectServer( String server, String port ) { boolean ok=true; try { socket=new Socket("127.0.0.1", 8008); os = new DataOutputStream(socket.getOutputStream()); is = new BufferedReader(new InputStreamReader(socket.getInputStream())); displayMessage("SYSTEM MESSAGE", "connection to " + server+ " -- port= "+port); // ..... da implementare } catch(IOException e){} return ok; } /* Interropme i thread per la ricezione dei messaggi dal server e chiude tutti gli stream associati */ public boolean disconnectServer() { displayMessage("SYSTEM MESSAGE", "disconnection"); boolean ok=true; try { System.out.println("Chiusura client"); os.close(); is.close(); socket.close(); } catch(IOException e){} return ok; } /* Tentaticvo di registrare il nickname "name", inviando un opportuno messaggio al server */ public boolean setScreenName( String name ){ boolean ok=true; // ..... da implementare return ok; } /* Invia un messaggio di chat al server ("message" contiene il testo scritto dall'utente)*/ public boolean sendMessage( String message ) { boolean ok=false; try { os.writeBytes(message + '\n'); } catch(IOException e){} return ok; } public static void main(String[] args){ try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){ } ChatClient client = new ChatClient(); client.show(); } }

Rispondi quotando