4

我正在尝试在 mygalaxy tab和 my之间建立联系laptop。所以我试图在我的笔记本电脑上运行服务器活动,在我的选项卡上运行客户端活动,但它不起作用。这是服务器和客户端代码。错误在哪里?

服务器:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView txt = (TextView)findViewById(R.id.textView1);

    int port = 12345;
    byte [] message = new byte [1500];
    DatagramPacket p = new DatagramPacket (message,message.length);
    try {
        InetAddress serveraddr = InetAddress.getByName("192.168.1.116");
        DatagramSocket s = new DatagramSocket (port,serveraddr);
        while (true){
            s.receive(p);
            String text = new String (message,0,p.getLength());
            txt.setText(text);
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

客户:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText edt = (EditText)findViewById(R.id.editText1);
    Button btn = (Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            String msg = edt.getText().toString();
            int port = 12345;
            try {
                DatagramSocket s = new DatagramSocket();
                InetAddress local  = InetAddress.getByName("192.168.1.116");
                int msg_lenght = msg.length();
                byte []message = msg.getBytes();
                DatagramPacket p = new DatagramPacket(message,msg_lenght,local,port);
                s.send(p);
            } catch (SocketException e) {                   
                e.printStackTrace();
            } catch (UnknownHostException e) {                  
                e.printStackTrace();
            } catch (IOException e) {               
                e.printStackTrace();
            }               
        }
    });
}

这是日志

09-17 23:49:55.190:D/dalvikvm(5892):后期启用 CheckJNI 09-17 23:49:55.690:D/CLIPBOARD(5892):在开始输入时隐藏剪贴板对话框:由其他人完成... !09-17 23:49:59.590: D/AndroidRuntime(5892): 关闭 VM 09-17 23:49:59.590: W/dalvikvm(5892): threadid=1: 线程退出未捕获异常 (group=0x40c4f1f8) 09 -17 23:49:59.590: E/AndroidRuntime(5892): 致命异常: main 09-17 23:49:59.590: E/AndroidRuntime(5892): android.os.NetworkOnMainThreadException 09-17 23:49:59.590: E /AndroidRuntime(5892): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 libcore.io.BlockGuardOs.sendto(BlockGuardOs. java:175) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 libcore.io.IoBridge.sendto(IoBridge.java:463) 09-17 23:49:59。137) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 android.app.ActivityThread.main(ActivityThread.java:4507) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在java.lang.reflect.Method.invokeNative(Native Method) 09-17 23:49:59.590: E/AndroidRuntime(5892): at java.lang.reflect.Method.invoke(Method.java:511) 09-17 23 :49:59.590: E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 09-17 23:49:59.590: E/AndroidRuntime(5892): at dalvik.system.NativeStart.main(Native Method) 09-17 23 :50:34.320:I / Process(5892):发送信号。PID:5892 SIG:9 4507) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 java .lang.reflect.Method.invoke(Method.java:511) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 978) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 09-17 23:49:59.590: E/AndroidRuntime( 5892): 在 dalvik.system.NativeStart.main(Native Method) 09-17 23:50:34.320: I/Process(5892): 发送信号。PID:5892 SIG:9 4507) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 java .lang.reflect.Method.invoke(Method.java:511) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 978) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 09-17 23:49:59.590: E/AndroidRuntime( 5892): 在 dalvik.system.NativeStart.main(Native Method) 09-17 23:50:34.320: I/Process(5892): 发送信号。PID:5892 SIG:9 E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:745) 09-17 23:49:59.590: E/AndroidRuntime(5892): at dalvik.system.NativeStart.main(Native Method) 09-17 23:50:34.320: I/Process(5892):发送信号。PID:5892 SIG:9 E/AndroidRuntime(5892): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978) 09-17 23:49:59.590: E/AndroidRuntime(5892): 在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:745) 09-17 23:49:59.590: E/AndroidRuntime(5892): at dalvik.system.NativeStart.main(Native Method) 09-17 23:50:34.320: I/Process(5892):发送信号。PID:5892 SIG:9

4

4 回答 4

4
09-17 23:49:59.590: E/AndroidRuntime(5892): android.os.NetworkOnMainThreadException
09-17 23:49:59.590: E/AndroidRuntime(5892):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-17 23:49:59.590: E/AndroidRuntime(5892):     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)

您不应该在 ui 线程中进行网络或时间密集型操作

另请参阅: Android:针对某些应用用户的 NoClassDefFoundErrorandroid 开发者信息

结帐: activity.runOnUi

于 2012-09-18T09:17:25.040 回答
0

您在服务器的 onCreate 中有一个无限循环。你不应该!创建一个用于轮询套接字的线程。

于 2012-09-18T09:10:43.337 回答
0
  • 您不能在 ui 线程上发送 udp 数据包,因此必须创建一个新的单独线程。

只是一个快速的解决方案...

创建一个 udpOutputData 字符串:
String udpOutputData;

在您的代码中创建一个新线程:

    //-----UDP send thread
Thread udpSendThread = new Thread(new Runnable() {

        @Override
        public void run() {


                while (true) {  

                    try {
                        Thread.sleep(100);
                    } 

                    catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                        if (sendUdp == true) {

                            try {

                                // get server name
                                InetAddress serverAddr = InetAddress.getByName(outputIP);
                                Log.d("UDP", "C: Connecting...");

                                // create new UDP socket
                                DatagramSocket socket = new DatagramSocket();

                                // prepare data to be sent
                                byte[] buf = udpOutputData.getBytes();

                                // create a UDP packet with data and its destination ip & port
                                DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, broadcastPort);
                                Log.d("UDP", "C: Sending: '" + new String(buf) + "'");

                                // send the UDP packet
                                socket.send(packet);

                                socket.close();

                                Log.d("UDP", "C: Sent.");
                                Log.d("UDP", "C: Done.");


                            } 

                            catch (Exception e) {
                                Log.e("UDP", "C: Error", e);

                            }

                                try {
                                    Thread.sleep(100);
                                }

                                catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }


                                sendUdp = false;
                        }

                }
        }

});

每次要发送一些 udp 数据时创建一个调用方法:

    public void sendUdp(String udpMsg) {

       udpOutputData = udpMsg;
       sendUdp = true;
    }

每次要发送 udp 数据包时调用该方法并为输出数据传递一个字符串:

String s = "hello from app";
sendUdp(s);
于 2015-03-10T16:31:31.243 回答
0

您的代码有 2 个问题

  1. 在主线程(UI 线程)上使用网络

09-17 23:49:59.590: E/AndroidRuntime(5892): 致命异常: main 09-17 23:49:59.590: E/AndroidRuntime(5892): android.os.NetworkOnMainThreadException

  1. while(true)在主线程上循环:

    while(true) {
        s.receive(p);
        String text = new String (message,0,p.getLength());
        txt.setText(text);
    }
    
于 2015-03-10T15:22:04.807 回答