处理更多客户

时间:2014-12-19 22:13:32

标签: java multithreading

现在我的服务器一次只能处理一个客户端。我试图使用一个线程,以便服务器可以处理多个客户端,但我做错了。我已经在try / catch子句中添加了线程,其中serverSocket接受客户端,但这没有区别。我没有得到任何错误,但它只是不起作用。 所以我想做的是让服务器不冻结在一个客户端,但仍然接受几个客户端。

这是服务器代码:

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

public class Server {

    private BufferedReader reader;
    private PrintWriter writer;
    private int port;

    public Server(int port)
    {
        this.port = port;
    }

    private String getSeverAddress() {      
        String host = null;
        try {
            InetAddress adr = InetAddress.getLocalHost();
            host = adr.getHostAddress();

        } catch (UnknownHostException e) {          
            e.printStackTrace();
        }

        return host;
    }

    public void startServer() {

        print("Contact this sever on address: " + getSeverAddress() + " port: " + port);

        ServerSocket ss = null; 
        Socket socket = null;
        Thread clientThread = null;


        try {
            ss = new ServerSocket(port);
            socket = ss.accept();                               

            clientThread = new Thread(new Client(socket));
            clientThread.start();

            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(), true);

            String msg = null;

            while (( msg = reader.readLine())  != null) {
                print("System out: " + msg);    

                if(msg.equals("Bye")) {
                    print("Client left");
                    break;
                }
            }

            ss.close();
            socket.close();
            reader.close();
            writer.close();

        } catch(SocketException e) {
            e.printStackTrace();            
        } catch (IOException i ) {
            i.printStackTrace();
            return;
        }

    }       

    private void print(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) {

        Server server = new Server(1111);
        server.startServer();

    }
}

以下是客户端代码:

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client implements Runnable{

    private Socket client;
    private BufferedReader reader;
    private PrintWriter writer;

    public Client(Socket socket)
    {
        client = socket;

        try{
            reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            writer = new PrintWriter(client.getOutputStream(), true);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    @Override
    public void run() {

        String msg = null;
        BufferedReader r = null;

        try {
            r = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        System.out.println("Write message to server");

        while(true) {
            try {
                msg = r.readLine();

                if(msg.equals("Quit") || msg == null) {
                    print("Disconnect");
                    break;
                }   
            } catch (IOException e) {
                e.printStackTrace();
            }
            writeToServer(msg);                         
        }

        try {
            r.close();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

    public void writeToServer(String msg) {                     
        writer.println(msg);                
    }

    private void print(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) {
        Socket socket = null;

        try {
            socket = new Socket("localhost", 1111);         

        } catch (Exception e) {
            e.printStackTrace();
        } 

        Client client = new Client(socket);
        client.run();
    }

}

2 个答案:

答案 0 :(得分:1)

您仍在尝试处理主线程中的客户端。主线程应该只接受新连接并启动新线程。您还必须在循环中执行accept,以便可以接受多个连接:

ss = new ServerSocket(port);
while(true) {
    Socket socket = ss.accept();                               
    Thread clientThread = new Thread(new Runnable() {
        public void run() {
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

            String msg = null;

            while (( msg = reader.readLine())  != null) {
                print("System out: " + msg);    

                if(msg.equals("Bye")) {
                    print("Client left");
                    break;
                }
            }

            socket.close();
            reader.close();
            writer.close();
        }});
    clientThread.start();
}

答案 1 :(得分:0)

您需要将ss.accept()置于while循环中,并为接受的每个客户端创建一个新线程,以处理连接。