java - UDP Android 应用程序适用于 HTC,但不适用于 Nexus S

标签 java android sockets networking udp

我有一个简单的应用程序,允许一台 Android 设备连接到另一台设备,现在使用我的 Google Nexus 作为主机并使用 HTC Dessire 连接客户端,它工作正常,但如果我将主机作为我的 Google Nexus 和我的客户端作为 Google Nexus S 时,会出现错误,以下是尝试连接到服务器时的代码

public void connectToServer()
{
    //Send a connect message to the server
    try {
        //Create a socket
        socket = new DatagramSocket( port );
        byte[] bufer = new byte[256];
        //Send a message "connect" to the host
        String msg = "connect";
        int msgLength = msg.length();
        bufer = msg.getBytes();
        InetAddress address;
        //Default ip address of the host
        address = InetAddress.getByName("192.168.1.59");
        DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, port );
        //Send packet
        socket.send( p ); // <-- Error is here

    } catch (UnknownHostException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Receive the message back
    byte[] buf = new byte[256];
    DatagramPacket packet = new DatagramPacket( buf, buf.length );
    //Try to receive a packet from the server
    try 
    {
        socket.receive( packet );
    } 
    //Error
    catch (IOException e) 
    {
        Log.d(TAG, "Error with receiving data");
        e.printStackTrace();
    }

    //Convert the packet to a string
    String data = new String( buf, 0, packet.getLength() );

    //Use the string to find out what ID this client is
    ID = Integer.parseInt( data );
    //Setup the client game
    setUpClient();

    //Debug
    //Log.d(TAG, "Data received was :" + ID);
}

这里还有来自 Eclipse 的日志

04-02 15:51:56.875: D/gameState(2719): Start the game loop
04-02 15:51:59.754: D/gameState(2719): Coords: x=216.09375,y=387.8023
04-02 15:51:59.754: D/gameState(2719): Join game
04-02 15:51:59.773: E/InputEventReceiver(2719): Exception dispatching input event.
04-02 15:51:59.777: E/MessageQueue-JNI(2719): Exception in MessageQueue callback: handleReceiveCallback
04-02 15:51:59.793: E/MessageQueue-JNI(2719): android.os.NetworkOnMainThreadException
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at libcore.io.IoBridge.sendto(IoBridge.java:473)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at java.net.DatagramSocket.send(DatagramSocket.java:284)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at     com.example.gelorph_v1.gameClient.connectToServer(gameClient.java:89)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.example.gelorph_v1.gameClient.<init>(gameClient.java:67)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.example.gelorph_v1.gamePanel.changeState(gamePanel.java:153)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.example.gelorph_v1.gamePanel.checkCollisions(gamePanel.java:117)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.example.gelorph_v1.gamePanel.onTouchEvent(gamePanel.java:77)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.View.dispatchTouchEvent(View.java:7127)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.View.dispatchPointerEvent(View.java:7307)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.os.MessageQueue.nativePollOnce(Native Method)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.os.MessageQueue.next(MessageQueue.java:125)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.os.Looper.loop(Looper.java:124)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at android.app.ActivityThread.main(ActivityThread.java:4745)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at java.lang.reflect.Method.invokeNative(Native Method)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at java.lang.reflect.Method.invoke(Method.java:511)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-02 15:51:59.793: E/MessageQueue-JNI(2719):   at dalvik.system.NativeStart.main(Native Method)

有什么原因导致这不起作用吗?

Canvas

更新

我刚刚将服务器和客户端更改为 AsyncTask 而不是线程,现在这是我的客户端代码

@Override
//Keep the game updated
protected String doInBackground(String... params)
{
    //If the client is host, then start the server thread
    if( host == true ) { setUpClient(); server.execute(); }
    //game us now ready to be played
    rdyForPlay = true;
    boolean run = true;
    boolean setupPlayer = false;

    while( run )
    {
        //Tell the server to give position of players
        //if( setupPlayer == true )
        //{
        //  setUpClient();
        //  setupPlayer = false;
        //}

        //If this is a client then do this
        if( host == false )
        {
            try {
                //If the socket is not yet setup, set it up
                if(socket == null)
                {
                    socket = new DatagramSocket( port );
                }
                byte[] bufer = new byte[256];
                //Using the ID given at the start, send X and Y position to the server
                String msg = ID +":"+ assets[ID].returnPosX() +":"+ assets[ID].returnPosY();
                int msgLength = msg.length();
                bufer = msg.getBytes();
                InetAddress address;
                address = InetAddress.getByName("192.168.1.59");
                DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, port );
                //Send the data
                socket.send( p );

            } catch (UnknownHostException e2) {
                // TODO Auto-generated catch block
                Log.d(TAG, "Error with unknown host");
                e2.printStackTrace();
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                Log.d(TAG, "Error with socket");
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d(TAG, "Error with sending/receiving data");
                e.printStackTrace();
            }

            //Receive the data from the serever
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket( buf, buf.length );
            //Try and receive data from the server
            try 
            {
                socket.receive( packet );
            } 
            //Error
            catch (IOException e) 
            {
                Log.d(TAG, "Error with receiving data");
                e.printStackTrace();
            }

            String data = new String( buf, 0, packet.getLength() );
            //Split the string up
            String[] dataArray = data.split("#");
            int newTotalPlayers = Integer.parseInt( dataArray[0] );
            //Using the string do this
            if( newTotalPlayers != totalPlayers )
            {
                Log.d(TAG," what is total amount of players:" + newTotalPlayers);
                if( newTotalPlayers == 1 )
                {
                    newPlayer( 0 );
                    totalPlayers = newTotalPlayers;
                }
                else
                {
                    newPlayer( newTotalPlayers );
                    totalPlayers = newTotalPlayers;
                }
            }

            //Do a for loop to go through dataArray
            for( int i = 0; i < totalPlayers; i++)
            {
                //Update each user that is connected
                String[] pos = dataArray[(i + 1)].split(":");
                if( Integer.parseInt( pos[(i*3)] ) == ID )
                {
                    Log.d(TAG, "Do nothing please");
                }
                else
                {
                    assets[i].setPosition( Integer.parseInt( pos[(i*3) + 1] ), Integer.parseInt( pos[(i*3) + 2] ) );
                }
            }

        }

        //Host data will ofcourse be differnet

    }
    Log.d(TAG, "Error with run value");
    return "finished";
}

我仍然遇到同样的错误...我是否错误地实现了 AsyncTask?

最佳答案

您正在 UI 线程中运行网络操作(因此出现异常android.os.NetworkOnMainThreadException),这在较新版本的 Android 中是不允许的。请使用 AsyncTask 来代替。

确保在 doBackground 中调用您的方法

关于java - UDP Android 应用程序适用于 HTC,但不适用于 Nexus S,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15767430/

相关文章:

android - Android 7.0版本通话界面上方弹窗

android - Cordova Android 项目无法通过重复的 google/gms 进行编译

windows - Socket.Bind 和 IP 源路由,具有多个本地网络接口(interface)

java - 我如何改进 Map<Integer, Map<Integer, Map<PAXType, BigDecimal>>> 使用 Guava

java - 如何获取android中对象的字段值

java - 为什么这个递归函数比迭代函数快 3 倍?

c - '发送失败';在 C 中使用 sendto 函数、使用 UDP 套接字时出错

java - 客户端的 BufferedReader 未使用套接字从服务器接收行(Java)

android - 在 Android 中从 css 调用 Assets 文件夹

node.js - Socket.io 客户端切换到 xhr-polling 时出现错误