I am currently programming a multi-user server using Sockets and ServerSockets. I was wondering if there is anyway to timeout the code if it does not complete within a certain time-frame. For example:
ServerSocket ss = new SeverSocket(PORT);
timeout(1000) {
ss.accept();
}
Where the ServerSocket is accepting a new user. Yet, since there are no users trying to connect, the ss.accept() will end after 1 second.
timeout(DURATION) {//code}
doesn't exist but it is just an arbitrary concept I came up with in this example. Another location that I would be using this in is when I am accepting the data from the user:
ServerSocket ss = new ServerSocket(PORT);
Socket client = ss.accept();
BufferedReader in =
new BufferedReader(new InputStreamReader(client.getInputStream()))
String data = "";
timeout(1000) {
data = in.readLine();
}
Where the program will timeout if there is no data being submitted from the client.
Note: The code for the server I am writing will constantly run through a HashMap of users and capture the data that the user is trying to send to the server. It will then take that data and do whatever its specified function is with it.