Using timer in a server thread

S

shlomi

Hi,
I just build a server, I want to add timer to thread where user don't
send any message for 5 minuts the thread is close: what I need to do?

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


public class *** extends Thread {
private Socket socket = null;

public ***(Socket socket) {
super("***");
this.socket = socket;
}


public void run() {

try {
PrintWriter out = new PrintWriter(socket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));

String inputLine, outputLine;
*** sftp = new ***(socket);
outputLine = sftp.processCommand(null);
out.println(outputLine);



while ((inputLine = in.readLine()) != null) {
outputLine = sftp.processCommand(inputLine);

out.println(outputLine);
if (outputLine.equals("100 Bye.\r\n"))
break;
}
out.close();
in.close();
socket.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks!
 
K

Knute Johnson

shlomi said:
Hi,
I just build a server, I want to add timer to thread where user don't
send any message for 5 minuts the thread is close: what I need to do?

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


public class *** extends Thread {
private Socket socket = null;

public ***(Socket socket) {
super("***");
this.socket = socket;
}


public void run() {

try {
PrintWriter out = new PrintWriter(socket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));

String inputLine, outputLine;
*** sftp = new ***(socket);
outputLine = sftp.processCommand(null);
out.println(outputLine);



while ((inputLine = in.readLine()) != null) {
outputLine = sftp.processCommand(inputLine);

out.println(outputLine);
if (outputLine.equals("100 Bye.\r\n"))
break;
}
out.close();
in.close();
socket.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks!

Set the timeout on the socket. When it times out it will throw and
exception and you can then close your connection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top