0

hello I've a problem with services. I'm making an application that implements an UDPServer and log in main activity the received packet. I want that the service starts even if the activity was closed so I think that I must to use Service. Is it true? I have already realize an app the implements a music player service with no problem, but when insert the UDP server's code in the service onStart function my application crush. This is the code about udp server:

    InetAddress serverAddr = InetAddress.getByName(SERVERIP);       
    //Create the socket using the serverAddress 
    //notifica("STARTING SERVER!", "UDP***Creating server" );
    //System.out.println("UDP***Creating server"); 
    DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr); 

    //create a buffer to copy packet contents into 
    byte[] buf = new byte[260]; 
    //create a packet to receive 
    DatagramPacket packet = new DatagramPacket(buf, buf.length); 
    notifica("START!", "Server started!" );
    while(true) {

        try{
            socket.receive(packet); 
            System.out.println("UDP***" + new String(packet.getData())); 
            notifica("RECEIVED!", new String(packet.getData()) );

        } catch (Exception e) { 
        notifica("EXC_INT:", "UDP Received Exception! " + e.toString() );
        }

    } 

I think that the problem is on the command:

socket.receive(packet); 

it's block the execution and waiting for something to read. i want that the main program doesn't block waiting for the service.

i try to insert a timeout and a sleep command in Service without any results:

    InetAddress serverAddr = InetAddress.getByName(SERVERIP);       
    //Create the socket using the serverAddress 
    //notifica("STARTING SERVER!", "UDP***Creating server" );
    //System.out.println("UDP***Creating server"); 
    DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr); 

    //create a buffer to copy packet contents into 
    byte[] buf = new byte[260]; 
    //create a packet to receive 
    DatagramPacket packet = new DatagramPacket(buf, buf.length); 
    notifica("START!", "Server started!" );
    while(true) {

        try{
            socket.setSoTimeout( 100 );
            socket.receive(packet); 
            System.out.println("UDP***" + new String(packet.getData())); 
            notifica("RECEIVED!", new String(packet.getData()) );
        } catch (SocketTimeoutException e) { 
        //notifica("EXC:", "UDP No data received!" );
        //Toast.makeText(getApplicationContext(), "UDP No data received!", Toast.LENGTH_SHORT).show();   
        } catch (Exception e) { 
        notifica("EXC_INT:", "UDP Received Exception! " + e.toString() );
        }
        try{
             Thread.sleep(30000);
        } catch (Exception e) { 
             notifica("EXC_INT_TIMER:", "Thread sleep exc! " + e.toString() );
        }
    } 

Thanks!

Dabidi
  • 397
  • 5
  • 14

1 Answers1

0

You will have to implement threading if you do not want the main program to block on socket.receive(packet);

one way of doing it is to place your blocking code under here:

new Thread(){

 public void run(){

//place blocking code here for example

while(true) {

    try{
        socket.setSoTimeout( 100 );
        socket.receive(packet); 
        System.out.println("UDP***" + new String(packet.getData())); 
        notifica("RECEIVED!", new String(packet.getData()) );
    } catch (SocketTimeoutException e) { 
    //notifica("EXC:", "UDP No data received!" );
    //Toast.makeText(getApplicationContext(), "UDP No data received!", Toast.LENGTH_SHORT).show();   
    } catch (Exception e) { 
    notifica("EXC_INT:", "UDP Received Exception! " + e.toString() );
    }
    try{
         Thread.sleep(30000);
    } catch (Exception e) { 
         notifica("EXC_INT_TIMER:", "Thread sleep exc! " + e.toString() );
    }
} 

}

}.start();

Also if you placed timeout and sleep to solve the issue then remove it because it will not solve your crashing issue.

user_CC
  • 4,686
  • 3
  • 20
  • 15