Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Problema con programma java socket
Forum - Java - Problema con programma java socket

Avatar
blackphoenix2 (Normal User)
Newbie


Messaggi: 2
Iscritto: 20/12/2009

Segnala al moderatore
Postato alle 9:46
Domenica, 20/12/2009
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!

Codice sorgente - presumibilmente Java

  1. public class Server extends Thread{
  2.  
  3. private ServerSocket server;
  4. private Socket socket;
  5. private Utenti client;
  6.  
  7. public Server() throws IOException {
  8.  
  9. server = new ServerSocket(1000, 5);
  10.  
  11. }
  12.  
  13. public void connessione() throws IOException, ChatException{
  14. String str;
  15. socket=server.accept();
  16. Utente nuovo = new Utente(socket,"");
  17. try{
  18. client.add(nuovo);
  19. }
  20. catch(ChatException e){
  21. System.out.println("Errore");
  22. }
  23.  
  24.  
  25. str=nuovo.getReceive().readLine();
  26. System.out.println(str);
  27.  
  28. }
  29.  
  30. public static void main(String[] args) throws IOException, ChatException {
  31. Server s = new Server();
  32.  
  33. s.connessione();
  34.  
  35. }
  36.  
  37. }
  38.  
  39. public class Utenti {
  40.  
  41. private Vector utenti;
  42.  
  43. public Utenti() {
  44. this.utenti = new Vector();
  45. }
  46.  
  47. public boolean isPresent(String nickname){
  48. if(!(utenti.isEmpty()))
  49. {Iterator it=utenti.iterator();
  50. Utente temp;
  51. while(it.hasNext())
  52. {
  53. temp=(Utente)it.next();
  54. if(nickname.equals(temp.getNickname()))
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60.  
  61.  
  62. public void add(Utente utente)throws ChatException
  63. {
  64. if(!(isPresent(utente.getNickname())))
  65. {
  66. if(utente.getNickname()=="")
  67. utente.setNickname("Guest"+utenti.size()
  68.  
  69. utenti.add(utente);
  70. }
  71. else throw new ChatException();
  72. }
  73.  
  74. public void remove(String nickname) throws ChatException{
  75. Utente temp;
  76. if((!(utenti.isEmpty()))&&(isPresent(nic…
  77. {
  78. for(int i=0;i<utenti.size();i++){
  79. temp=(Utente)utenti.elementAt(i);
  80. if(temp.getNickname().equals(nickname));
  81. {
  82. utenti.removeElementAt(i);
  83. utenti.trimToSize();
  84. }
  85.  
  86. }
  87. }else throw new ChatException();
  88. }
  89. }
  90. public class Client {
  91.  
  92. private Socket socket;
  93. private BufferedReader receive;
  94. private PrintStream send;
  95. private String nickname;
  96.  
  97. public Client(String nickname) {
  98. this.nickname = nickname;
  99. try {
  100.  
  101. socket = new Socket("127.0.0.1", 1000);
  102. receive = new BufferedReader(new InputStreamReader(socket.getInputStream(
  103. send = new PrintStream(socket.getOutputStream());
  104. send.print(nickname);
  105. System.out.println("Connesso");
  106. }
  107. catch(IOException e) {
  108. System.out.println(e);
  109. }
  110. }
  111.  
  112.  
  113. public static void main(String[] args) throws IOException, ChatException {
  114. Client c = new Client("pippo");
  115.  
  116. }
  117.  
  118. }


PM Quote
Avatar
tasx (Dev Team)
Expert


Messaggi: 439
Iscritto: 15/12/2008

Segnala al moderatore
Postato alle 16:13
Domenica, 20/12/2009
Ciao!!
Ad una prima occhiata sembra che tu nn inizializzi l'oggetto clien,
infatti subito dopo aver accettato la connessione lo chiami in causa senza averlo inizializzato.

Ciao ciao!! :k::k:

PM Quote