单线程服务器实现多线程

时间:2015-08-04 13:52:40

标签: java android multithreading

伙计我想将我的服务器实现转换为多线程,以便它可以处理多个请求。基本上服务器与Android应用程序连接,它正在从Android应用程序接收图像。我想添加一个线程,以便它可以处理多个请求,并且线程应该在收到请求时启动。请帮助我。

这是服务器代码。

public static void main(String[] args) throws UnknownHostException, IOException, MatlabInvocationException, MatlabConnectionException {
    while (true) {
        try {
            serverSocket = new ServerSocket(4001); // Server socket

        } catch (IOException e) {
            System.out.println("Could not listen on port: 4001");
        }
        System.out.println("Server started. Listening to the port 4001");

            clientSocket = serverSocket.accept();
            DataInputStream inputFromClient = new DataInputStream(clientSocket.getInputStream());
            int count = inputFromClient.readInt();
            int available = inputFromClient.available();
            System.out.println("Length of Image in Bytes:" + count);
            System.out.println("available:" + available);
            image = new byte[count];
            inputFromClient.readFully(image);
            System.out.println(image.length);
            System.out.println(image);
            final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image));
            ImageIO.write(bufferedImage, "jpg", new File("image.jpg"));
            System.out.println("Image has been wriiten in the directory.");

        MatlabProxyFactory mpf = new MatlabProxyFactory();
        MatlabProxy proxy = mpf.getProxy();
        proxy.eval("conclusion=DetectColorL");
        Object[] obj = proxy.returningEval("conclusion", 1);
        String Message = obj[0].toString();
        DataOutputStream outTo = new DataOutputStream(clientSocket.getOutputStream());
        outTo.writeUTF(Message.toString());
        System.out.println(Message);
        proxy.disconnect();
        serverSocket.close();

1 个答案:

答案 0 :(得分:1)

要使其成为多线程,您希望能够同时连接多个客户端,以便一次处理多个请求而不是一个。

为此,您的服务器必须永久接受新客户。

public static void main(String[] args) {
    ServerSocket serverSocket;

    try {
        serverSocket = new ServerSocket(4001); // Server socket
        System.out.println("Server started. Listening to the port 4001");

        while (true) {
            // Always accept new clients
            Socket clientSocket = serverSocket.accept(); 
            new RequestHandlingClass(clientSocket).start(); // Make a new thread and call it's run procedure
        } 

    } catch (IOException e) {
        System.out.println("Could not listen on port: 4001");
    }      
}

现在我们的服务器接受了多个客户端,我们必须实现RequestHandlingClass类。您希望该类侦听客户端请求并处理它们。

public class RequestHandlingClass() extends Thread {
    Socket clientSocket;        
    DataInputStream inputFromClient;

    RequestHandlingClass(Socket clientSocket) {
        this.clientSocket = clientSocket;
        this.inputFromClient = new DataInputStream(clientSocket.getInputStream());
        // ...
    }

    public void run() {
        // Handle client requests
    }
}

根据您的问题,我想您要执行"图像处理" run方法中的代码。