File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Networking question

Payam Fard
Ranch Hand

Joined: Jan 31, 2003
Posts: 75
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.
public abstract class DSTimer {
private Timer timer;
private PropertyReader reader = new
private Task task = null;
public DSTimer() {
timer = new Timer();

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

public void startTimer(int seconds) {


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

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() {

public void start(int port) throws IOException {

try {

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

servRequests = new
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 !=


public void whenExpires(){

Socket clientSocket = null;

try {


while(listening &&
servRequests[whenExpiresCount].isUpDown()) {
clientSocket =

catch(IOException ioe){

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 method. But there are other 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: 75
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?
I agree. Here's the link:
subject: Networking question
jQuery in Action, 3rd edition