聊天服务器客户端有时无法在同一服务器上看到来自其他客户端的消息

时间:2015-09-24 01:17:36

标签: java class server client project

我正在为我正在处理的项目的Chat服务器客户端制作2个类。问题是服务器可以看到发送给它的消息(来自客户端)并且它可以将这些消息发送给每个客户端如果他想查看消息,每个客户端必须首先输入一些输入来自其他用户。我不知道我做错了什么。请帮帮我。在此先感谢:)

服务器类

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

public class TestServer extends Thread
{ 
protected Socket clientSocket;
public static ArrayList<Socket> ConnectionArray = new ArrayList<Socket>();
public static ArrayList<String> CurrentUsers = new ArrayList<String>();
final static int PORT = 22;

public static void main(String[] args) throws IOException 
{ 
    ServerSocket serverSocket = null; 

    try
    {           
        serverSocket = new ServerSocket(PORT); 
        System.out.println ("Connection Socket Created");
        try 
        { 
            while (true)
            {
                 System.out.println ("Waiting for Connection");
                 Socket sock = serverSocket.accept();
                 ConnectionArray.add(sock);
                 System.out.println("Client connected from: " + sock.getLocalAddress().getHostName());                   
                 new TestServer (sock);                   
            }
        }
        catch (IOException e) 
        { 
            System.err.println("Accept failed."); 
            System.exit(1); 
        } 
    } 
    catch (IOException e) 
    { 
         System.err.println("Could not listen on port: " + PORT); 
         System.exit(1); 
    } 
    finally
    {
         try {
              serverSocket.close(); 
             }
         catch (IOException e)
             { 
              System.err.println("Could not close port: 10008."); 
              System.exit(1); 
             } 
        }
   }

    private TestServer (Socket inSock)
  {
    clientSocket = inSock;
    start();
  }

public void run()
{
    System.out.println ("New Communication Thread Started");
    //System.out.println ("Client connected from: " + sock.getLocalAddress().getHostName());

    try { 
         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true); 
         BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); 

         String inputLine; 

         while (true) 
             { 
                inputLine = in.readLine();
                System.out.println ("Server: " + inputLine);   

                  for(int i = 1; i <= TestServer.ConnectionArray.size(); i++)
                    {   
                        System.out.println("Total Connection: " + ConnectionArray.size());
                        Socket TEMP_SOCK = (Socket) TestServer.ConnectionArray.get(i-1);
                        if (clientSocket != TEMP_SOCK)
                       {
                            PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream(), true);
                            TEMP_OUT.println(inputLine);
                            TEMP_OUT.flush();                              
                            System.out.println("Sending to: " + TEMP_SOCK.getLocalAddress().getHostName());
                       }
                    }
                  if (inputLine.equals("Bye.")) 
                  break; 
             } 
             out.close(); 
             in.close(); 
             clientSocket.close(); 
    } 
    catch (IOException e) 
    { 
         System.err.println("Problem with Communication Server");
         System.exit(1); 
    } 
}
} 

客户等级

import java.io.*;
import java.net.*;

public class TestClient  {
public static void main(String[] args) throws IOException  {

    String serverHostname = new String ("127.0.0.1");

    if (args.length > 0)
       serverHostname = args[0];
    System.out.println ("Attemping to connect to host " +
            serverHostname);

    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        echoSocket = new Socket(serverHostname, 22);
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + serverHostname);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for "
                           + "the connection to: " + serverHostname);
        System.exit(1);
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String userInput;

    System.out.println ("Type Message (\"Bye.\" to quit)");
    while ((userInput = stdIn.readLine()) != null) 
    {           
        out.println(userInput);         

        if (userInput.equals("Bye."))
            break;

        System.out.println("Other user: " + in.readLine());
    }
        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
   }
}

1 个答案:

答案 0 :(得分:0)

客户端需要一个单独的线程,用于从服务器读取而不阻塞System.in;

Paragraph.AddTab