Java线程服务器客户端消息传递应用程序

时间:2017-11-07 03:19:43

标签: java multithreading

我试图在java中创建一个线程服务器客户端消息传递应用程序。我在发送客户端发送给连接到服务器的每个其他客户端的消息时遇到问题。我将所有连接的线程添加到数组列表中,因此当发送消息时,我可以遍历列表并将其发送给所有客户端,但这似乎并不起作用。我在这里做错了什么?

当一个客户端连接时,它完美地运行服务器回应客户端所说的一切。当连接两个客户端时,只有发送消息的客户端才会收到消息。如果同一个客户端发送另一条消息,服务器似乎挂断了,没有消息得到回应。

线程服务器代码

import java.io.*;
import java.net.*;
import java.util.*;
public class ThreadedSever
{
  private static final int port=5000;
  ArrayList<ServerThread> clientList = new ArrayList<ServerThread>();

  //Threaded Server
  public ThreadedSever(){
     System.out.println("A multi-threaded server has started...");
     ServerSocket serverSocket = null; 
     try {
         serverSocket = new ServerSocket(port);
     }catch(Exception e){
         System.out.println("Could not create a server socket: " + e);
     }
     //Chat with the client until breaks connection or says bye
     try{
         while(true){
             System.out.println("Waiting for client communication");
             Socket currentSocket = serverSocket.accept();
             //Create a new thread to deal with this connection
             clientList.add(new ServerThread(currentSocket));
             System.out.println(clientList);
         }

     }catch(Exception e){
         System.out.println("Fatal Server Error!");
     }
  }
   //Inner class to handle individual commincation
   private class ServerThread extends Thread{
   //Possible add name to then get private messages
   private Socket sock;
   private DataInputStream in = null;
   private DataOutputStream out = null;

   public ServerThread(Socket sock){
       try{
           this.sock = sock;
           in = new DataInputStream(this.sock.getInputStream());
           out = new DataOutputStream(this.sock.getOutputStream());
           System.out.print("New client connection established");
           out.writeUTF("Type bye to exit, otherwise, prepare to be echoed");
           start();
       }catch(Exception e){
           System.out.println("Oops");
       }
   }
   public void run(){
     try{
        String what = new String("");
        while(!what.toLowerCase().equals("bye")){
            what = in.readUTF();
            echoMessage(what);

        }
     }catch(Exception e){
         System.out.println("Connection to current client has been broken");
     }
     finally{
       try{
           sock.close();
       }catch(Exception e){
           System.out.println("Error socket could not be closed");
       }
     }
   }
   public void echoMessage(String what){
     try{
      for (ServerThread Thread: clientList) {
            in = Thread.in;
            out = Thread.out;

            System.out.println("Client told me: " + what);
            out.writeUTF(what);
      }
     }catch(Exception e){

     }
   }
  }
  public static void main(){
    new ThreadedSever();
  }
}

客户代码

import java.io.*;
import java.net.*;
public class simpleClient
{
    private static final int port= 5000;
    private static String server = "localhost";
    private static Socket socket = null;
    private static DataInputStream input = null;
    private static DataOutputStream output = null;
    private static InputStreamReader inReader = null;
    private static BufferedReader stdin = null;

    public static void main(){
        try{
            socket = new Socket(server, port);
        }catch(UnknownHostException e){
            System.err.println("Unknow IP address for server");
            System.exit(-1);
        }
        catch(IOException e){
            System.err.println("No server found at specified port");
            System.exit(-1);
        }
        catch(Exception e){
            System.err.println("Something happened!");
            System.exit(-1);
        }
        try{
            input = new DataInputStream(socket.getInputStream());
            output = new DataOutputStream(socket.getOutputStream());
            inReader = new InputStreamReader(System.in);
            stdin = new BufferedReader(inReader);
            String what = new String("");
            String response;
            while(!what.toLowerCase().equals("bye")){
                // Expect something from the server and output it when it arrives
                response = input.readUTF();
                System.out.println("Server said  \"" + response + "\"");
                //Read a line from the user and send it to the server
                what = stdin.readLine();
                output.writeUTF(what);
            }
        }
        catch(IOException e){
            System.err.println("Broken connection with server");
            System.exit(-1);
        }

    }
}

0 个答案:

没有答案