문제

Basically I want whenever Client connect It would send a String "connect" to Server. In response Server should start a thread for that client which will keep sending "Awake" message to client after 5 seconds.

I have used Timer class with Server but it is unable to ping client on same socket. Can you tell me some way for this. Sample code will be beneficial.

code: -

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);
    }
}

Thanks

도움이 되었습니까?

해결책

I think the simplest solution for your implementation is to have a static List of Socket (instead of a single static socket). This way you would know everybody that connected there. Then you have to change the rest of the code to deal with the list, instead of a single instance.

다른 팁

I would suggest looking into Executor framework and thread pools. Inside a thread you can just Thread.sleep(5000). It should work for small amount of clients (<50).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top