Android UDP广播本机IP的时候出了问题

在写一个程序,需要用到udp来广播本机的IP
于是我在启动的时候先设一个监听,等待udp广播,然后再设置一个button,按下button的时候就会在局域网内广播本机的ip地址。但是现在程序可以正常启动,监听也可以正常设置,但是一旦按下按钮程序就崩溃,求各位大神帮忙看一下小弟的程序错在哪里了好吗?

这是运行的时候的错误消息
07-24 07:32:35.302: E/cutils-trace(1124): Error opening trace file: No such file or directory (2)
07-24 07:32:40.022: E/cutils-trace(1140): Error opening trace file: No such file or directory (2)
07-24 07:32:44.572: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.582: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.582: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.582: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressStandard.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressDelete.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressReturn.ogg
07-24 07:32:44.612: E/AndroidRuntime(1151): FATAL EXCEPTION: main
07-24 07:32:44.612: E/AndroidRuntime(1151): android.os.NetworkOnMainThreadException
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
07-24 07:32:44.612: E/AndroidRuntime(1151): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-24 07:32:44.612: E/AndroidRuntime(1151): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
07-24 07:32:44.612: E/AndroidRuntime(1151): at com.example.testudp.UdpListener.SendIp(UdpListener.java:34)
07-24 07:32:44.612: E/AndroidRuntime(1151): at com.example.testudp.MainActivity$2.onClick(MainActivity.java:47)
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.view.View.performClick(View.java:4240)
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.view.View$PerformClick.run(View.java:17721)
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.os.Handler.handleCallback(Handler.java:730)
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.os.Looper.loop(Looper.java:137)
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-24 07:32:44.612: E/AndroidRuntime(1151): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 07:32:44.612: E/AndroidRuntime(1151): at java.lang.reflect.Method.invoke(Method.java:525)
07-24 07:32:44.612: E/AndroidRuntime(1151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-24 07:32:44.612: E/AndroidRuntime(1151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-24 07:32:44.612: E/AndroidRuntime(1151): at dalvik.system.NativeStart.main(Native Method)

ps:在PC端上用eclipse运行就正常,移植到Android手机上之后就出错。网络使用的权限也添加了。

下面是UdpListener类:
package com.example.testudp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpListener {
private int port = 9999;
private DatagramSocket ds = null;
private DatagramPacket dp = null;
private byte[] buf = new byte[1024]; //存储发来的消息
private StringBuffer sbuf ;

public UdpListener() {
    sbuf = new StringBuffer();
    try {
        ds = new DatagramSocket(port);
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    dp = new DatagramPacket(buf, buf.length);
}


public String SendIp() {
    String host = "255.255.255.255";        //广播地址
    String message = null;
    try {
        message = InetAddress.getLocalHost().toString();
    } catch (UnknownHostException e1) {
        return e1.getMessage();
    }

    try {
        InetAddress adds = InetAddress.getByName(host);
        DatagramSocket ds = new DatagramSocket();
        DatagramPacket dp = new DatagramPacket(message.getBytes(),
                message.length(), adds, port);
        ds.send(dp);
        ds.close();
    } catch (UnknownHostException e) {
        return e.getMessage();
    } catch (SocketException e) {
        return e.getMessage();
    } catch (IOException e) {
        return e.getMessage();
    }
    return "Success";
}

public String ReceiveMessage()
{
    sbuf.delete(0, sbuf.length());
    System.out.println("等待广播消息");
    try {
        ds.receive(dp);
        int i;
        for(i=0;i<1024;i++){
            if(buf[i] == 0){
                break;
            }
            sbuf.append((char) buf[i]);
        }
        System.out.println("收到广播消息:" + sbuf.toString());
    } catch (IOException e) {
        return e.getMessage();
    }
    return sbuf.toString();
}

}

然后是android的程序
package com.example.testudp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private Button link;
private String message = null;
@SuppressWarnings("unused")
private EditText password;
private TextView txt;
private UdpListener ul;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
link = (Button)findViewById(R.id.link);
txt = (TextView)findViewById(R.id.txt1);
password = (EditText)findViewById(R.id.pass);
ul = new UdpListener();

    link.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            new Thread(){
                @Override
                public void run(){
                    txt.append(ul.SendIp());
                }
            }.start();
        }
    });

    Listener listener = new Listener();
    listener.start();
}

private class Listener extends Thread{
    @Override
    public void run(){
        txt.append("等待消息");
        while(true)
        {
            message = ul.ReceiveMessage();
            txt.append("收到消息" + message);
        } 
    }
}

}

1个回答

好吗?
这是运行的时候的错误消息
07-24 07:32:35.302: E/cutils-trace(1124): Error opening trace file: No such file or directory (2)
07-24 07:32:40.022: E/cutils-trace(1140): Error opening trace file: No such file or directory (2)
07-24 07:32:44.572: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.582: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.582: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.582: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressStandard.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressDelete.ogg
07-24 07:32:44.592: E/SoundPool(280): error loading /system/media/audio/ui/KeypressReturn.ogg
07-24 07:32:44.612: E/AndroidRuntime(1151): FATAL EXCEPTION: main
07-24 07:32:44.612: E/AndroidRuntime(1151): android.os.NetworkOnMainThreadException
07-24 07:32:44.612: E/AndroidRuntime(1151): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
07-24 07:32:44.612: E/AndroidRuntime(1151): at java.net.In

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!