title: Run JAVA program as background service
body: <p>Im doing an application in android,which connects to a PC via a Java server program using socket programming...</p>
  
  <p>I need to run the Java server program as a background service...</p>
  
  <p>can anybody pls help me???</p>
  
  <pre><code> ServerSocket serverSocket = null;
        Socket socket = null;
        DataInputStream dataInputStream = null;
        DataOutputStream dataOutputStream = null;
  
        try {
         serverSocket = new ServerSocket(8888);
         System.out.println("Listening :8888");
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
  
        while(true){
         try {
          socket = serverSocket.accept();
          dataInputStream = new DataInputStream(socket.getInputStream());
          dataOutputStream = new DataOutputStream(socket.getOutputStream());
          System.out.println("ip: " + socket.getInetAddress());
          System.out.println("message: " + dataInputStream.readUTF());
          //dataOutputStream.writeUTF("Hello!");
          //String dataStream = dataInputStream.readUTF();
         // System.out.println("message: " + dataStream);
          //dataOutputStream.writeUTF("Hello!");
          c++;
          dataOutputStream.writeUTF(ss+c);
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
         finally{
          if( socket!= null){
           try {
            socket.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }
  
          if( dataInputStream!= null){
           try {
            dataInputStream.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }
  
          if( dataOutputStream!= null){
           try {
            dataOutputStream.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
  </code></pre>