• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Server app is timing out - how to keep it running.

Hello all,
I have a server application running on a linux server and am timing out after about 15 minutes. I do not know how to make this server program run indefinately. It is an application that listens for clients and responds appropriately. Any ideas?
Thanks,
Jared
0
jrod
Asked:
jrod
  • 10
  • 7
  • 4
  • +2
1 Solution
 
objectsCommented:
Hard to say without seeing your code.
0
 
heyhey_Commented:
maybe you need to call

serverSocket.setSoTimeout(timeout);
0
 
jrodAuthor Commented:
Sorry about that- I was not sure if my code was relavant. NOTE: The app is running perfectly it just stops running after a time frame.

CODE:

public class MyServerApp{
    //...variable delaration etc.
    try{
      server = new ServerSocket(5252);//listen to the socket
      //tried without this, with it set to 0, and with this number
      //server.setSoTimeout(360000000);
    }catch(IOException e){
      System.exit(-1);
    }
    while(true){
      MyConnectionThread w;//thread that handles communication
      try{
        w = new MyConnectionThread(server.accept());//receive new client
        Thread t = new Thread(w);//create new thread with our handler
        t.start();//start new thread
      } catch (IOException e) {
        System.exit(-1);
      }
    }
  }
  //...clean up
}
class MyConnectionThread implements Runnable{//communication handler
  //...
  public void run(){//when run
   //...variable declaration etc.
   try{
      //get streams from client socket
      in = new BufferedReader(new InputStreamReader(client.getInputStream()));
      out = new ObjectOutputStream(client.getOutputStream());
    } catch (IOException e) {...}
    while(true){
      try{
        line = in.readLine();//get data from client
        out.writeBoolean(true);//test to see if running
        out.flush();//flush information
        break;//break out of loop
      }catch(IOException e){break;}
    }
    try{//clean up
      in.close();
      out.close();
    }catch(Exception e){...}
  }
  //...other functions
}

Hope you can help me from here. I am running it on a linux server.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jrodAuthor Commented:
NOTE to heyhey:

I tried the serversocket.setSoTimeOut(timeout) and it did not seem to make a difference. I am still working on variations, but believe that I tried every variation and no luck. See my code above
0
 
objectsCommented:
When you say stops running exactly what do you mean?
I'd also recomend printing exceptions when they occur o/wise you'll have no idea if they are happening.

0
 
jrodAuthor Commented:
I am printing all exceptions, but there are no exceptions. I can access it from my client perfectly when I telnet in and start the program. If I close the telnet port then it stops running.  If I unplug the telnet port (without closing connection properly) it continues to run, but times out after about twenty minutes. I am pretty sure that this is not a code problem, but a problem with how I am initiating the program on the server. A few questions that may asnwer this problem:
Where do I put the program?
How do I run it (which command line do I use)?
0
 
objectsCommented:
The code you posted above is not printing out various exceptions, some that would stop the app.
Can you post the code you are ACTUALLY running then.

> I am pretty sure that this is not a code problem
> but a problem with how I am initiating
> the program on the server.

How are you starting it now?

> A few questions that may asnwer this problem:
> Where do I put the program?

Doesn't matter, as long as it's available in your classpath.
But as it is starting I assume it is.

> How do I run it (which command line do I use)?

java classname

0
 
jrodAuthor Commented:
This is my exact code without the user validation. Maybe you will be able to tell better with this code.


import java.io.*;<br>
import java.net.*;<br>
import java.util.*;<br>
import java.sql.*;<br>
import sun.jdbc.odbc.JdbcOdbcDriver;<br>
<br>
public class igdConnectionManager {<br>
  ServerSocket server = null;//server socket used to listen to port<br>
<br>
  public igdConnectionManager() {}<br>
  public void listenSocket(){<br>
    try{<br>
      server = new ServerSocket(5245);//listen to the socket 5245<br>
      //server.setSoTimeout(360000000);//<----does not seem to help<br>
      System.out.println("Socket Set");//<br>
    }catch(IOException e){<br>
      System.err.println("Could not listen on port 5245");<br>
      System.exit(-1);<br>
    }<br>
    while(true){<br>
      igdConnectionThread w;//thread that handles communication<br>
      try{<br>
        System.out.println("Listening for client");<br>
        w = new igdConnectionThread(server.accept());//receive new client<br>
        Thread t = new Thread(w);//create new thread with our communication handler<br>
        t.start();//start new thread<br>
      } catch (IOException e) {<br>
        System.err.println("Accept failed: 5245");<br>
        System.exit(-1);<br>
      }<br>
    }<br>
  }<br>
<br>
  protected void finalize(){<br>
//Objects created in run method are finalized when<br>
//program terminates and thread exits<br>
     try{<br>
        server.close();//clean up - close serversocket<br>
        server=null;//set to null<br>
    } catch (IOException e) {<br>
        System.err.println("Could not close socket");<br>
        System.exit(-1);<br>
    }<br>
  }<br>
  public static void main(String[] args) {//application<br>
    igdConnectionManager app=new igdConnectionManager();<br>
    app.listenSocket();<br>
  }<br>
}<br>
<br>
class igdConnectionThread implements Runnable{//communication handler<br>
  private Socket client;//client socket<br>
  private String uid="---";//uid<br>
  private String pass="---";//password<br>
  private String license="---";//license code<br>
<br>
  public igdConnectionThread(Socket client) {//receives client on construction<br>
    this.client=client;<br>
  }<br>
<br>
  public void run(){//when run<br>
    String line;//line received<br>
    BufferedReader in = null;//reader stream<br>
    ObjectOutputStream out = null;//output stream<br>
   try{<br>
      //get streams from client socket<br>
      System.out.println("NEW CLIENT: "+client.toString());<br>
      in = new BufferedReader(new InputStreamReader(client.getInputStream()));<br>
      out = new ObjectOutputStream(client.getOutputStream());<br>
    } catch (IOException e) {<br>
      System.err.println("Failed to get streams");<br>
    }<br>
    while(true){<br>
      try{<br>
        line = in.readLine();//get data from client<br>
        //Send data back to client<br>
        System.out.println("User: "+line+"\n");<br>
        out.writeBoolean(validate(line));//send confirmation of match to user<br>
        out.flush();<br>
        break;//break out of loop when data analized<br>
      } catch (IOException e) {<br>
        System.out.println("Read failed");<br>
        break;<br>
      }<br>
    }<br>
    try{//clean up<br>
      in.close();<br>
      out.close();<br>
      in=null;<br>
      out=null;<br>
    }catch(Exception e){<br>
      System.err.println("Failed to close client");<br>
    }<br>
  }<br>
  private boolean validate(String user){//determine if data is valid<br>
    //validates user<br>
    return true;//or false if invalid<br>
  }<br>
  protected void finalize() throws java.lang.Throwable{//cleans up<br>
    try{<br>
      client.close();<br>
      client=null;<br>
    }catch(IOException ioe){<br>
      System.out.println("Error: "+ioe.toString());<br>
    }<br>
  }<br>
}<br>
0
 
jrodAuthor Commented:
ignore the <br> I placed them in afterwards for JGuru posting. They are not normally there
0
 
objectsCommented:
Change your main as follows and let me know what is printed out when your server unexpectedly exits.

public static void main(String[] args) {//application    
  try
  {
    igdConnectionManager app=new igdConnectionManager();
    app.listenSocket();
  }
  catch (Exception ex)
  {
    System.out.println(ex);
  }
  System.out.println("done");
}
0
 
jrodAuthor Commented:
I did as you asked:

Started it last night at about 9:30 PM,
checked it again at 10:00 PM (still running),
checked it this morning at 6:00 AM (no longer running)

There is output stored for each time I logged in, but none for when it quit. It seems to me that the server is shutting it down or something.
0
 
objectsCommented:
Have you tried on a different machine?
0
 
jrodAuthor Commented:
The server is the main server for running servlets, jsp pages, and is the only server we have configured to run Java. I am nervous about messing around with other machines, as we have a lot of customers that depend on the servers running flawlessly.
I was just hopping that somebody would know how to work a linux server in such a way that I do not have this problem. Here are the steps I am currently taking:
-Program is at same level as JRE and other programs stored on the server
-Telnet in cd into the directory containing myApp
-Run Java as follows: java myApp &
( have also tried with "java myApp" and java myApp 1>out.txt 2>err.txt &")
-Have a little client test program that can tell whether the thing is still running
-If I shut down the telnet port regularly it kills the app so I have to actually physically unplug my network connection to keep it running at all. This tells me that it is depending on my telnet to stay alive.

NOTE: Have also tried to run it from a CGI script, but it fails.

Is there a command line that I could or should type that is different that would allow me to run it without these problems? Any more help would be greatly appreciated.
0
 
heyhey_Commented:
possible problems

1. you call System.exit()
2. when you start your app with
java myApp &
and when it tries to print something to stdout, process is destroyed

what happens if you start the app with
java myApp
(without closing the telnet client)
0
 
jrodAuthor Commented:
I've tried it with
java myApp 1>out.txt 2>err.txt & (where output and error is output to different files- does not help)

I have tried java myApp without closing telnet client and it runs perfectly as long as I have my telnet port open, but it does not return my cursor to me.

Is there a different way I should be running it? Surely there must be a way to do it without keeping a telnet client running forever.

Jared
0
 
objectsCommented:
Didn't follow exactly what you meant earlier when you said that you were closing the telnet port. I do now :)
afaik shutting now a telnet session will kill all foreground processes.
Not sure why it would be failing when started as a background process, does anything get logger to your output files after the telnet port is closed?

Have you tried starting it from your boot scripts?

Another option would be to set it up as a servlet to be started automatically when your servlet container is started.
0
 
jrodAuthor Commented:
The weird thing is that it kills the process even run as background unless I literally pull the plug on my network connection, i.e. do not log out of telnet properly. Based on my understanding of the server it seems like it is keeping that telnet connection alive for x minutes and then terminating it and with it the process that was started with it.
I am considering the servlet approach. Have never programmed servlets or JSP, but thought that may be the way to go. The program I am using right now is simply sending a string of data to the server and returning a response. Is that possible with servlets? Is there an easy quick response you could give me as to how to quickly impliment what I have as a server or should I spend some time learning the full servlet programming api? Thanks for all your help
Jared
0
 
objectsCommented:
Try starting it from your boot script.
0
 
heyhey_Commented:
> The weird thing is that it kills the process even run as background

what OS are you using ?
this is OS specific problem and has nothing to do with Java
0
 
jrodAuthor Commented:
running linux
0
 
heyhey_Commented:
that's the shell script that I use for starting my server applications in background

java -cp mysql.jar:ServerApp.zip ServerApp >> server.log 2>&1
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- To be PAQ'ed and points NOT refunded

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
 
PashaModCommented:
Per Recommendation,

PashaMod
Community Support Moderator @Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 7
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now