Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket question

 
Payam Fard
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I need to have a server that goes up and down at different times. Let's say, server is supposed to start listening to messages on its socket at time 10, then it supposed to go down at time 100, then it comes back up at time 300, .... What I am currently doing is that, I create a ServerSocket and do the following:
while (true) {
new ServerThread(serverSocket.accept(), serverRequests).start();
}
In the run method of ServerThread, I do the following:
public void run() {
try {
ObjectInputStream is = new ObjectInputStream( new
BufferedInputStream(socket.getInputStream()));
Object object = null;
while((object = is.readObject()) != null) {
request = (ClientRequest)object;
System.out.println("Server received the following client request:");
System.out.println(request.toString());
}
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
Now, I want to simulate the server going up and down by blocking the socket when server is down and unblocking it when it is up. During the down time, I want all messages to get ignored (I do not want the server to process messages received during down time when it comes back up).
I was looking at socket.setSoTimeout(int timeout) to implement it, but I am not sure how to use it. Would this method be helpful in my scenario? Or should I use java.util.Timer class?
Could someone please direct me on how to implement this (or better if someone could modify the above code accordingly to handle this feature, assuming all the up and down times in ints are in an array called timeArray and each up time in this array is between two down times and vice versa)?
Thanks a lot,
Payam.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Socket forum
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic