20/12/09 8:46
blackphoenix2
Non riesco a capire l'errore, mi solleva un eccezione a client.add();
(il programma è incompleto lo so, per il momento crea solo la connessione).
Se potete date un occhiata ed aiutatemi, grazie mille in anticipo, ciao!
(il programma è incompleto lo so, per il momento crea solo la connessione).
Se potete date un occhiata ed aiutatemi, grazie mille in anticipo, ciao!
public class Server extends Thread{ private ServerSocket server; private Socket socket; private Utenti client; public Server() throws IOException { server = new ServerSocket(1000, 5); } public void connessione() throws IOException, ChatException{ String str; socket=server.accept(); Utente nuovo = new Utente(socket,""); try{ client.add(nuovo); } catch(ChatException e){ System.out.println("Errore"); } str=nuovo.getReceive().readLine(); System.out.println(str); } public static void main(String[] args) throws IOException, ChatException { Server s = new Server(); s.connessione(); } } public class Utenti { private Vector utenti; public Utenti() { this.utenti = new Vector(); } public boolean isPresent(String nickname){ if(!(utenti.isEmpty())) {Iterator it=utenti.iterator(); Utente temp; while(it.hasNext()) { temp=(Utente)it.next(); if(nickname.equals(temp.getNickname())) return true; } } return false; } public void add(Utente utente)throws ChatException { if(!(isPresent(utente.getNickname()))) { if(utente.getNickname()=="") utente.setNickname("Guest"+utenti.size()… utenti.add(utente); } else throw new ChatException(); } public void remove(String nickname) throws ChatException{ Utente temp; if((!(utenti.isEmpty()))&&(isPresent(nic… { for(int i=0;i<utenti.size();i++){ temp=(Utente)utenti.elementAt(i); if(temp.getNickname().equals(nickname)); { utenti.removeElementAt(i); utenti.trimToSize(); } } }else throw new ChatException(); } } public class Client { private Socket socket; private BufferedReader receive; private PrintStream send; private String nickname; public Client(String nickname) { this.nickname = nickname; try { socket = new Socket("127.0.0.1", 1000); receive = new BufferedReader(new InputStreamReader(socket.getInputStream(… send = new PrintStream(socket.getOutputStream()); send.print(nickname); System.out.println("Connesso"); } catch(IOException e) { System.out.println(e); } } public static void main(String[] args) throws IOException, ChatException { Client c = new Client("pippo"); } }
aaa