不发送UDP数据包

时间:2015-03-06 22:57:12

标签: java android sockets

这是我的班级:

public class DatagramClient
{
    private DatagramSocket socket = null;
    InetAddress h;
public DatagramClient() throws SocketException, IOException {
    try {
        socket = new DatagramSocket();
        socket.setBroadcast(true);
        h = InetAddress.getByName("192.168.2.108");

    }
    catch (Exception e) {
        Log.i(Constantes.TAG_LOG, e.getMessage());
    }
}

public DatagramSocket getSocket() {
    return socket;
}

public void setSocket(DatagramSocket socket) {
    this.socket = socket;
}

public void sendPacket(String msg){
    byte[] data = msg.getBytes();
    Log.i(Constantes.TAG_LOG,"Criar DatagramPacket");
    final DatagramPacket packet = new DatagramPacket(data,data.length,h,Constantes.UDP_PORT);
    Log.i(Constantes.TAG_LOG,"Criou DatagramPacket");
    Log.i(Constantes.TAG_LOG,"Enviar packet");
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.i(Constantes.TAG_LOG,"Run Thread");
                socket.send(packet);
                Log.i(Constantes.TAG_LOG,"Envio dentro da thread");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    Log.i(Constantes.TAG_LOG,"Enviou fora da thread");
}

}

我需要在sendPacket方法中发送UDP数据包作为字符串参数。当我查看logcat输出时,我没有看到任何错误,但是没有发送UDP数据包。这里出了什么问题?

我添加了此代码但无效:

try {
            thread.start();
            Log.i(Constantes.TAG_LOG,"Executou thread");
        }catch(Exception e){
            Log.i(Constantes.TAG_LOG,"Erro thread.start()"+e.getMessage());
        }

的logcat>

03-06 21:16:07.814    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Criar DatagramPacket
03-06 21:16:07.814    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Criou DatagramPacket
03-06 21:16:07.814    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Enviar packet
03-06 21:16:07.814    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Executou thread
03-06 21:16:07.814    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Enviou fora da thread
03-06 21:16:07.814    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Send PEDRO
03-06 21:16:07.814    1199-2173/com.pa.homeautomation I/HOME_AUTOMATION﹕ Run Thread
03-06 21:16:07.814    1199-2173/com.pa.homeautomation I/HOME_AUTOMATION﹕ Envio dentro da thread
03-06 21:16:08.324    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Criar DatagramPacket
03-06 21:16:08.324    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Criou DatagramPacket
03-06 21:16:08.324    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Enviar packet
03-06 21:16:08.324    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Executou thread
03-06 21:16:08.324    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Enviou fora da thread
03-06 21:16:08.324    1199-1199/com.pa.homeautomation I/HOME_AUTOMATION﹕ Send PEDRO
03-06 21:16:08.324    1199-2175/com.pa.homeautomation I/HOME_AUTOMATION﹕ Run Thread
03-06 21:16:08.324    1199-2175/com.pa.homeautomation I/HOME_AUTOMATION﹕ Envio dentro da thread

如果我在主线程中放入应用程序崩溃的日志:

03-06 21:46:10.014    4139-4139/com.pa.homeautomation I/HOME_AUTOMATION﹕ Criar DatagramPacket
03-06 21:46:10.034    4139-4139/com.pa.homeautomation I/HOME_AUTOMATION﹕ Criou DatagramPacket
03-06 21:46:10.034    4139-4139/com.pa.homeautomation I/HOME_AUTOMATION﹕ Enviar packet
03-06 21:46:10.034    4139-4139/com.pa.homeautomation D/AndroidRuntime﹕ Shutting down VM
03-06 21:46:10.034    4139-4139/com.pa.homeautomation W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415b38b0)
03-06 21:46:10.044    4139-4139/com.pa.homeautomation E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
            at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
            at libcore.io.IoBridge.sendto(IoBridge.java:477)
            at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
            at java.net.DatagramSocket.send(DatagramSocket.java:284)
            at com.pa.homeautomation.network.DatagramClient.sendPacket(DatagramClient.java:66)
            at com.pa.homeautomation.listeners.OnTesteClickListener.onClick(OnTesteClickListener.java:44)
            at android.view.View.performClick(View.java:4422)
            at android.view.View$PerformClick.run(View.java:17903)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5232)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
            at dalvik.system.NativeStart.main(Native Method)

代码:

public void sendPacket(String msg) throws IOException {
        byte[] data = msg.getBytes();
        Log.i(Constantes.TAG_LOG,"Criar DatagramPacket");
        DatagramPacket packet = new DatagramPacket(data,data.length,getBroadcastAddress(),Constantes.UDP_PORT);
        Log.i(Constantes.TAG_LOG,"Criou DatagramPacket");
        Log.i(Constantes.TAG_LOG,"Enviar packet");
        socket.send(packet);
        Log.i(Constantes.TAG_LOG,"Enviou packet");


    }

0 个答案:

没有答案