2016-04-25 2 views
-2

Я пишу чат-программу на Java. Кажется, я не могу отправлять сообщения. Мои ClientConnectionHandler обрабатывает экземпляр каждого клиентане может отправлять сообщения в чате

import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 


public class ClientConnectionHandler extends Thread { 



private Socket socket; 
String username; 
private UUID id; //= UUID.randomUUID(); 
BufferedReader reader; 
PrintWriter writer; 
private final HashMap<UUID, ClientConnectionHandler> clients = new HashMap<>(); 
volatile boolean messageLoop = true; 

ServerGUI serverGUI; 

public ClientConnectionHandler(Socket socket){ 
    this.socket = socket; 
    try{ 
     this.socket.setSoTimeout(1000); 
    } 
    catch (SocketException e) { 
     System.out.println(e); 
    } 

    try { 

     reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

public void run(){ 
    if (socket != null && reader != null) { 
     addToClients(); 
     try { 
      String messageInput; 
      while (messageLoop){ 
       try{ 
        messageInput = reader.readLine(); 
        sendMessage(messageInput); 
       } 
       catch(SocketTimeoutException ste){ 
        //Thread.yield(); 
       } 
      } 
     } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } 
     finally { 
      removeFromClients(); 
      try { 
       reader.close(); 
       writer.close(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
    } 

    else { 
     System.out.println("Socket connection and BufferedReader are closed"); 
    } 
} 
private void sendMessage(String msg){ 
    for (ClientConnectionHandler cchandler : clients.values()){ 
     try { 
      //if (!clientconn.id.equals(this.id)) { 
       cchandler.writer.write(msg+"\n"); 
       cchandler.writer.flush(); 
      //} 
     } 
     catch (Exception e){ 
      System.err.println("Unable to write to client"); 
      clients.remove(cchandler.id); 
     } 
    } 
} 

} 

Вот мой Client класс

public class Client { 
    private Socket clientSocket; 
    private String username; 
    private static int port = 7777; 
    BufferedReader inReader; 
    PrintWriter outWriter; 
    private SimpleDateFormat sdf; 
    ClientGUI clientGUI; 
    ServerGUI serverGUI; 
    private Login login; 

    public Client(String username, int port){ 
     this.username = username; 
     this.port = port; 
     sdf = new SimpleDateFormat("dd.MM.yyyy 'at' hh:mm:ss a"); 
    } 

    /** 
    * Connects the client to the Server 
    */ 
    public void connectClient(){ 
     try{ 
      clientSocket = new Socket("127.0.0.1", port); 
      Thread thread = new Thread(new ClientConnectionHandler(clientSocket)); 
      thread.start(); 
      String connected = sdf.format(new Date())+"\n"+username+" just connected \n"; 
      System.out.println(connected); 
     } 
     catch(Exception e){ 

      System.out.println(sdf.format(new Date())+"\n"+username+" did not connect to server "+e+"\n"); 
     }  
    } 

    /** 
    * Disconnects the Client from the Server 
    */ 
    void disconnectClient(){ 
     try{ 
      clientSocket.close(); 
      System.out.println(username+" disconnected \n"); 
     } 
     catch(Exception e){ 
      System.out.println(username+" failed to disconnect \n"); 
     } 
    } 


    //send message to the server 
    public void sendMessage() throws IOException{ 
     try{ 
      inReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
      outWriter = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
     } 
     catch(IOException ioe){ 

     } 

     //infinite loop to check for messages 
     try{ 
      String response; 
      String inputMsg = inReader.readLine(); 
      outWriter.println(inputMsg);  
      while((response = inReader.readLine()) != null){ 
       System.out.println(response); 
       outWriter.println(inputMsg); 
      } 
      inReader.close(); 
      outWriter.close(); 
      //clientGUI.appendMessage(inputMsg); 
     } 
     catch(IOException ioe){ 

     } 
    } 

    void appendToCG(String str){ 
     clientGUI.appendMessage(str); 
    } 

    public static void main(String[] args) { 
     Client client = new Client("127.0.0.1",port); 
    } 

} 

EDIT

startServerConnection() метод в моем классе Server:

public void startServerConnection() 
    { 
     try { 
      serverSocket = new ServerSocket(portNumber); 

      while(connection){ 
       System.out.println("Waiting for a client..."); 
       Socket clientSocket = serverSocket.accept(); 
       System.out.println("Server connection established on port: "+clientSocket.getLocalPort()); 
       ClientConnectionHandler clientConnection = new ClientConnectionHandler(clientSocket); 
       Thread thread = new Thread(clientConnection); 
       thread.start(); 
      } 
     } 
     catch (Exception e) { 
      // System.out.println(sdf.format(new 
      // Date())+": Server didn't connect"); //append to eventsLog 
      e.printStackTrace(); 
      return; 
     } 
    } 

Я уверен, что я создал всех читателей и писателей, необходимых как для ClientConnectionHandler (для каждого пользователя для отправки сообщения) и Client для чтения сообщений. Я что-то упускаю?

+1

Я не вижу 'ServerSocket' где-нибудь здесь ... – 3kings

+0

@ 3kings Я отправил часть моего Класс 'Server'. Есть идеи? – Guest1235

+0

Пожалуйста, не отрицайте ваши сообщения. Чтобы удалить сообщение, нажмите «Удалить», если хотите удалить его. –

ответ

1

Возможно не полное разрешение, но:

  1. ClientConnectionHandler расширяет тему. Вероятно, он должен расширить Runnable, поскольку вы создаете Thread в startServerConnection().

  2. Похоже, вы пытаетесь повторно использовать ClientConnectionHandler между Server и Client. Этот подход может быть не таким, каким вы хотите. Скорее всего, вам нужен другой клиент Runnable. Я не думал, что через все последствия, но вы можете быть в состоянии сделать Client класс реализации Runnable, а затем изменить Client

    public void connectClient(){ 
    try{ 
        clientSocket = new Socket("127.0.0.1", port); 
        // set a timeout here, or you will block the IO! Alternatively, 
        // use the NIO stuff 
        Thread thread = new Thread(this); 
        thread.start(); 
        String connected = sdf.format(new Date())+"\n"+username+" just connected \n"; 
        System.out.println(connected); 
    } 
    catch(Exception e){ 
    
        System.out.println(sdf.format(new Date())+"\n"+username+" did not connect to server "+e+"\n"); 
    }  
    } 
    
    public void run() { 
        try { 
         clientSocket.setSoTimeout(1000); 
    
         //loop to read messages sent to *this* client 
         while (true) { 
         try { 
          String inputMsg = reader.readLine(); 
          // just print to stdout; might want to update some GUI 
          System.out.println(inputMsg); 
         } 
         catch (SocketTimeoutException noop) { } 
         } //end of while loop 
        } 
        catch (Exception e) { 
         // note that this error may not be a problem since 
         // disconnect client will close the socket, which will 
         // cause an error at some future point. 
         System.err.println(e); 
        } 
    
        } 
    } 
    
  3. В методе Client.sendMessage():
    а. Какое сообщение вы хотите отправить? Вы должны собирать данные от где-то
    b. Не прикрепляйте и не закрывайте потоки в методе.

  4. Так предположительно в Client классе вам нужно сделать что-то вроде:

    public void sendMessage() throws IOException{ 
        try{ 
        // get the message to send from somewhere 
        String msgToSend = "foo"; // just dumb text; replace with actual 
        writer.write(msgToSend); 
        writer.write("\n"); // often needed 
        writer.flush(); // flush the buffer 
        } 
    
    } 
    
+0

есть ли способ поговорить об этом в комнате? Я знаю, как реализовать свой код, но есть некоторые вещи, которые я не уверен, и я не хочу начинать дискуссию по разделу комментариев. – Guest1235

+0

Мне нужно, чтобы вы создали комнату, так как у меня недостаточно репутации. – Guest1235

+0

да, я здесь новый. Не знаю ничего о чатах. – JohnDorrian

Смежные вопросы