Solved

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

Posted on 2002-05-06
23
151 Views
Last Modified: 2010-03-31
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
Comment
Question by:jrod
  • 10
  • 7
  • 4
  • +2
23 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6993067
Hard to say without seeing your code.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6993173
maybe you need to call

serverSocket.setSoTimeout(timeout);
0
 

Author Comment

by:jrod
ID: 6997336
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
 

Author Comment

by:jrod
ID: 6997340
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
 
LVL 92

Expert Comment

by:objects
ID: 6997727
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
 

Author Comment

by:jrod
ID: 6997775
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
 
LVL 92

Expert Comment

by:objects
ID: 6997795
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
 

Author Comment

by:jrod
ID: 6997815
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
 

Author Comment

by:jrod
ID: 6997816
ignore the <br> I placed them in afterwards for JGuru posting. They are not normally there
0
 
LVL 92

Expert Comment

by:objects
ID: 6997826
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
 

Author Comment

by:jrod
ID: 6998898
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 92

Expert Comment

by:objects
ID: 7000146
Have you tried on a different machine?
0
 

Author Comment

by:jrod
ID: 7001425
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 7001540
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
 

Author Comment

by:jrod
ID: 7002257
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
 
LVL 92

Expert Comment

by:objects
ID: 7002566
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
 

Author Comment

by:jrod
ID: 7005209
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
 
LVL 92

Expert Comment

by:objects
ID: 7005224
Try starting it from your boot script.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7005431
> 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
 

Author Comment

by:jrod
ID: 7007082
running linux
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7007763
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
 
LVL 35

Expert Comment

by:girionis
ID: 8709830
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
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 8774351
Per Recommendation,

PashaMod
Community Support Moderator @Experts Exchange
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now