向客户端发送消息并将结果发送回服务器

时间:2017-11-23 09:38:46

标签: java

所以我试图在这种情况下创建一个多线程服务器(尝试解释尽可能简单):

  1. 一台服务器和k台客户端。
    1. 服务器创建n个随机数的列表并均匀分布 这些数字来自k个连接的客户端。
    2. 客户计算其指定数字的平均值并将此值返回给 服务器
    3. 服务器等待所有值都存在并计算其间的标准差 所有收到的平均值。服务器写入所有客户端的平均值以及整体 屏幕上的标准偏差
    4. 服务器使用不同的随机数列表重复此整个过程几次。
  2. 目前,我的客户端无法连接到服务器。我对java很新,所以请耐心等待。我如何将计算结果发送回服务器?

    MultiThreadServer:

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class MultiThreadServer {
    
        public static double SendNumber;
        public static int Delay = 500;
        public static String ReceivedSum;
        public static final int PORT = 2000;
    
        public static void main(String[] args) throws IOException, InterruptedException {
            ServerSocket s = new ServerSocket(PORT);
            System.out.println("Server socket: " + s);
            System.out.println("Server listening...");
    
            try { while(true) {
                Socket socket = s.accept();
                // Blocks until a connection occurs:
                System.out.println("Connection accepted."); 
                System.out.println("The new socket: " + socket);
    
                DataInputStream din = new DataInputStream(socket.getInputStream());
                DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
                Client t = new Client(socket);
    
                System.out.println("New thread started.");
                System.out.println("The new thread: " + t);
                Random r = new Random();
                for(int i=0;i<10;i++) {
                    SendNumber = r.nextDouble();
                    dout.writeDouble(SendNumber);
                }
                Thread.sleep(Delay);
                System.out.println("Client Sum is: " + din.read());
                    }
    
            } //catch, while, try
            finally { s.close(); }
            } //main
        } //MultiServer
    

    客户端:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Random;
    import javafx.application.Platform;
    
    
    
    class Client extends Thread{
        private  PrintWriter out;
        private  BufferedReader in;
        private Socket socket;
        public static ArrayList<Double> Numbers = new ArrayList<Double>();
        public static ArrayList<Client> allClients = new ArrayList<Client>();
        public static double FinalClientSum;
    
    
        public Client(Socket s) throws IOException {
            socket = s;
            in = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(
                    new BufferedWriter(
                            new OutputStreamWriter(socket.getOutputStream())), true);
    
            String line;
            while((line = in.readLine()) != null) {
                Numbers.add((Double.parseDouble(line)));
            }
            synchronized(this) {
                allClients.add(this);
            }
    
            start();
        }
    
        public void run(){
                try {
    
                    DataInputStream din = new DataInputStream(socket.getInputStream());
                    DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
    
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    for(int i = 0; i<Numbers.size();i++) {
                        FinalClientSum += Numbers.get(i); //calc sum
                    }
                    //send back the information to server.
                    dout.writeDouble(FinalClientSum);
    
                }catch(Exception e) {
    
                }finally {
                    try { socket.close(); }
                    catch(IOException e) {
                        System.out.println("Client thread " +  ": Socket not closed!");
                    }
                    synchronized(this) {
                        allClients.remove(allClients.indexOf(this));
                    }
            }
    
        }
    }
    

1 个答案:

答案 0 :(得分:-1)

你的Socket socket = s.accept();应该进入while循环。