GeeCON Prague 2014*
The moose likes Sockets and Internet Protocols and the fly likes Networking question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Networking question" Watch "Networking question" New topic
Author

Networking question

Payam Fard
Ranch Hand

Joined: Jan 31, 2003
Posts: 73
Hi all,
I am trying to write a server that listens on some
port. When this server comes up, it reads an xml for
its initialization. In that XML file, there are
indications on when this server should be up and when
it should go down. I am trying to achieve this using
java.util.Timer. Assuming server goes up at time 50,
it goes to the while loop and listens for upcoming
messages. At this point server waits here indifintely
until a socket connection is made with a client. When
server is in the waiting mode, even if another timer
says that the server should go down, it cannot process
that until it is out of its infinite loop. Here is my
DSTimer code along with start method of my server. It
goes to an infinite loop and does not go up and down
as desired. Is there anyway to interrupt the server
listening? or how I can fix this problem?
Any ideas/suggestions greatly appreciated.
Thanks,
Payam.
public abstract class DSTimer {
private Timer timer;
private PropertyReader reader = new
PropertyReader();
private Task task = null;
public DSTimer() {
timer = new Timer();
}

public void setTask(Task task){
this.task = task;
}

public void startTimer(int seconds) {

timer.schedule(task,
seconds*reader.getTimeUnit());
}

public class Task extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer
thread
whenExpires();
}
}

public abstract void whenExpires();
}

public abstract class Server extends DSTimer {

private Collection serverRequests = null;
private boolean listening = true;
private boolean currentUpDown = false; //true for
up and false for down
private ServerSocket serverSocket = null;
private int whenExpiresCount = 0;
private ServRequest servRequests[];

/** Creates new Server */
public Server() {
super();
initializeServer();
}

public void start(int port) throws IOException {

try {

serverSocket = new ServerSocket(port);
}
catch (IOException e) {
System.err.println("Could not listen on
port: " + port + ".");
System.exit(-1);
}

servRequests = new
ServRequest[serverRequests.size()];
servRequests =
(ServRequest[])serverRequests.toArray(servRequests);

for(int i=0; i<servRequests.length; i++) {

this.setTask(new Task());

this.startTimer((int)servRequests[i].getTime()); //
Starting timer
}

//Loop until all whenExpires are called.
while(whenExpiresCount !=
serverRequests.size());

serverSocket.close();
}

public void whenExpires(){

Socket clientSocket = null;

try {


if(!servRequests[whenExpiresCount].isUpDown())
serverSocket.close();

while(listening &&
servRequests[whenExpiresCount].isUpDown()) {
clientSocket =
serverSocket.accept();
new
ServerThread(clientSocket).start();
}

}
catch(IOException ioe){
ioe.printStackTrace();
}


whenExpiresCount++;
}
}
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Payam,
You need to use a non blocking ServerSocket. You can get an example here Non Blocking ServerSocket Example That program also blocks when it calls the Selector.select() method. But there are other Selector.select() methods that either timeout or don't block at all. Take a look at the API docs for java.nio.channels.Selector and see if this will solve your problem.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Payam Fard
Ranch Hand

Joined: Jan 31, 2003
Posts: 73
Thanks for replying to my posting. It is a little too late for me to switch to NIO at this point. I was hoping to get rid of the problem by accepting and closing socket from different threads, but it still blocks!!! Any ideas on how to fix this code?
Thanks,
Payam.
 
GeeCON Prague 2014
 
subject: Networking question