Android Socket编程异常

时间:2016-02-06 12:04:44

标签: android socket.io datagram

我试图使用UDP从Android Studio发送数据,但在线路服务器上遇到异常。(并且数据包)抛出异常.... 这是来自mainActivity的我的代码......

    public void onClickLoginButton(View view) throws IOException
    {
        loginButton = (Button) findViewById(R.id.button);
        editText= (EditText) findViewById(R.id.editText);
        EditText editText1 = (EditText) findViewById(R.id.editText2);
        send=new byte[1024];
        receive=new byte[1024];




        send="login".getBytes();
            DatagramPacket packet2 = new DatagramPacket(send, send.length, InetAddress.getByName("192.168.10.9"), 1205);
             try {
                 server.send(packet2);
             }
             catch(Exception e)
             {

                 Log.d(getClass().getSimpleName(),"error");
             }

        String client = editText.getText().toString();
            send = client.getBytes();
            DatagramPacket packet = new DatagramPacket(send, send.length, InetAddress.getByName("192.168.10.9"), 1205);
            server.send(packet);

            String string = editText1.getText().toString();
            send = client.getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(send, send.length, InetAddress.getByName("192.168.10.9"), 1235);
            server.send(packet);


            DatagramPacket packet1 = new DatagramPacket(receive, receive.length);
            server.receive(packet1);

        Log.d(getClass().getSimpleName(), "Connection failed"
                );



    }
}

例外:

29830/com.example.hamza.weekendmessanger E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method for android:onClick

1 个答案:

答案 0 :(得分:0)

我认为您必须在

中添加适当的权限
  

清单

他们是:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />