hi all,
first i searched the internet and did not find something help me or i did not understand what they say. i have server client program the client send data to the server and the server reply i want if the client did not send data for 2 hours the server close the thread.

Recommended Answers

All 7 Replies

You can use a java.util.Timer
When you receive anything from the user, cancel any existing Timer and start a new one with a 2 hour delay.
If the Timer's TimerTask ever runs it means 2 hours have expired without any input, so it can close the connection.

thank you for reply i read about what you but i have no idea how to do it i will be thankful for any help

OK, here's little runnable demo code that wants you to keep typing lines of anything. If you don't enter a line for 2 secs it times out and exits.
If you read this code, in conjunction with the API doc, you should be able to write a version that fits your requirement

        Scanner scan = new Scanner(System.in);
        java.util.Timer timer = null;
        System.out.println("keep entering lines...");
        do {
            if (timer != null) timer.cancel();
            java.util.TimerTask task= new java.util.TimerTask() {
                public void run() {
                    System.out.println("You timed out");
                    System.exit(0);
                }
            };
            timer = new java.util.Timer();
            timer.schedule(task, 2000);
        } while (scan.nextLine().length() != 0);

if i understand the example
you mean i do it like this

        java.util.Timer timer = null;
        System.out.println("keep entering lines...");
        do {
            if (timer != null) timer.cancel();
            java.util.TimerTask task= new java.util.TimerTask() {
                public void run() {
                    System.out.println("You timed out");
                    System.exit(0);
                }
            };
            timer = new java.util.Timer();
            timer.schedule(task, 2000);
        } while (in.readutf().tostring() !=null)

the program after 2 seconds will close if it doesnot recieve from the client

Yes ( I'm assuming your line 13 does what it looks like)

thanks i will try it and reply to you if it is working

OK, I'm closing now for the night anyway. Good luck
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.