-1

Im doing an application in android,which connects to a PC via a Java server program using socket programming...

I need to run the Java server program as a background service...

can anybody pls help me???

 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();
         }
user1057197
  • 479
  • 1
  • 6
  • 15

3 Answers3

3

You will need to "wrap" your java application to run as a service. This will allow the java program to execute just like other services. There are tools available for this purpose. See Java Service Wrapper and YAJSW for more details.

Manish
  • 3,913
  • 2
  • 29
  • 45
1

yes, you can run the java application by use Java Service Wrapper.

Liping Huang
  • 4,378
  • 4
  • 29
  • 46
0

Try setting the Java code as a Batch file;if it is for a Windows OS...

subrussn90
  • 1,142
  • 1
  • 14
  • 27
  • OP says, "Im doing an application in android" – Nishant Jan 09 '12 at 06:45
  • 1
    @Nishant: OP says "which connects to a PC via a Java server program using socket programming". He needs to run the Java Server program as service, not the android application. – Manish Jan 09 '12 at 06:53