想要在服务器上为每个客户端启动一个线程

时间:2014-04-02 09:55:30

标签: java sockets timer client-server

基本上我想要每当客户端连接时它会向服务器发送字符串“connect”。作为响应,服务器应为该客户端启动一个线程,该线程将在5秒后继续向客户端发送“唤醒”消息。

我在服务器上使用了Timer类,但无法在同一个套接字上ping客户端。你能告诉我一些方法吗?示例代码将是有益的。

代码: -

import java.io.BufferedReader;

public class Server2 {
    public static void main(String[] args) {
        Server ob = new Server();
        ob.dt();
    }
}

class Server {

    public static Socket socket;

    public void dt() {
        try {

            int port = 25000;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port 25000");

            // Server is running always. This is done using this while(true) loop
            while (true) {
                // Reading the message from the client
                socket = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String number = br.readLine();
                if (number.equals("2")) {
                    System.out.println("asda");
                    TimerExampleRepeating tr = new TimerExampleRepeating();
                    tr.heartbeat(this);
                } else if (number.equals("disconnect")) {
                } else {
                    System.out.println("Message received from client is " + number);

                    // Multiplying the number by 2 and forming the return message
                    String returnMessage;
                    try {
                        int numberInIntFormat = Integer.parseInt(number);
                        int returnValue = numberInIntFormat * 2;
                        returnMessage = String.valueOf(returnValue) + "\n";
                    } catch (NumberFormatException e) {
                        // Input was not a number. Sending proper message back to client.
                        returnMessage = "Please send a proper number\n";
                    }

                    // Sending the response back to the client.
                    OutputStream os = socket.getOutputStream();
                    OutputStreamWriter osw = new OutputStreamWriter(os);
                    BufferedWriter bw = new BufferedWriter(osw);
                    bw.write(returnMessage);
                    System.out.println("Message sent to the client is " + returnMessage);
                    bw.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (Exception e) {
            }
        }
    }

    public void clientsend(String message) {
        try {
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(message);
            System.out.println("Message sent to the client is " + message);
            bw.flush();
        } catch (Exception e) {
        }
    }
}

class TimerExampleRepeating {

    public void heartbeat(final Server srv) throws Exception {
        // Creating timer which executes once after five seconds
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                try {
                    System.out.println("Message sent to the client is I am up1");
                    srv.clientsend("I am Up");
                    System.out.println("Message sent to the client is I am up3");
                } catch (Exception e) {
                }
                // System.out.println("Message sent to the client is I am up");
            }
        }, 5000, 5000);
    }
}

由于

2 个答案:

答案 0 :(得分:1)

我认为实现的最简单的解决方案是拥有一个静态的Socket列表(而不是一个静态套接字)。通过这种方式,您可以了解与之相关的所有人。然后你必须改变代码的其余部分来处理列表,而不是单个实例。

答案 1 :(得分:0)

我建议调查Executor框架和thread pools。在线程内部,您只需Thread.sleep(5000)。它应该适用于少量客户端(< 50)。