2013-02-20 25 views
0

你好我的服务有问题。 我正在制作一个实现UDPServer的应用程序,并在主要活动中登录收到的数据包。我希望即使活动已关闭,服务也会启动,所以我认为我必须使用服务。这是真的吗? 我已经实现了一个应用程序实现一个音乐播放器服务没有问题,但是当在服务onStart函数中插入UDP服务器的代码时我的应用程序被压缩。 这是关于UDP服务器代码:服务区块应用执行

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()); 
     } 

    } 

我认为这个问题是在命令:

socket.receive(packet); 

这是阻止执行,并等待着什么阅读。 我希望主程序不会阻塞等待服务。

我尝试插入一个暂停,并在服务休眠命令没有任何结果:

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()); 
     } 
    } 

谢谢!

回答

0

如果你不想让主程序阻塞socket.receive(packet),你将不得不实现线程化。这样做的

一个办法是把你的阻塞代码在这里下:

新的Thread(){

public void run(){ 

//在这里举行,例如阻止代码

,而(真) {

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();

此外,如果您放置超时和睡眠来解决问题,然后将其删除,因为它不会解决您的崩溃问题。