Android线程停止了

时间:2013-03-28 09:39:29

标签: java android udp wifi adb

我有一个处理接收和发送UDP数据包的线程,这个工作正常,直到收到X数据包,然后客户端只是停止尝试接收数据包,并停止发送它们,所有的catch函数不显示任何内容,客户端只是停止询问数据包。 这是我的客户端运行代码

public void run()
{
    if( host == true ) { setUpClient(); server.start(); }
    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( host == false )
        {
            try {
                if(socket == null)
                {
                    socket = new DatagramSocket( port );
                }
                byte[] bufer = new byte[256];
                //String msg = "position";
                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 );
                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();
            }

            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket( buf, buf.length );
            try 
            {
                socket.receive( packet );
            } 
            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] );
            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;
                }
                //if( ID == 0 && host == false)
                //{
                //  ID = newTotalPlayers;
                //  setupPlayer = true;
                //}
            }
            //Do a for loop to go through dataArray
            for( int i = 0; i < totalPlayers; i++)
            {
                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] ) );
                }
            }

        }

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

这样可以正常工作,只是因为某些奇怪的原因,连接被丢弃了,这真是让我感到烦恼,我无法找到,因为LogCat根本没有打印出来

帆布

P.S。如果需要,可以提供更多代码

这是我的服务器运行方法

public void run() {
    InetAddress client = null;
    boolean run = true;
    String data = "";
    DatagramPacket packet = null;
    while( run )
    {
        if( data.equalsIgnoreCase( "" ) )
        {
            /*Log.d(TAG, "waiting for clients");
            String msg = "waiting";
            int msgLength = msg.length();
            byte[] message = msg.getBytes();
            DatagramPacket p = new DatagramPacket( message, msgLength, client, port );
            try 
            {
                socket.send( p );
            } 
            catch (IOException e2) 
            {
                Log.d(TAG, "Error with sending");
                e2.printStackTrace();
            }*/
        }

        //Send some data
        if( data.equalsIgnoreCase( "connect" ) )
        {
            Log.d(TAG, "ID send :" + packet.getAddress());
            address.add( packet.getAddress() );
            players += 1;
            String msg = String.valueOf( players );
            int msgLength = msg.length();
            byte[] message = msg.getBytes();
            DatagramPacket p = new DatagramPacket( message, msgLength, packet.getAddress(), port );
            try 
            {
                socket.send( p );
            } 
            catch (IOException e2) 
            {
                Log.d(TAG, "Error with sending");
                e2.printStackTrace();
                data = "";
            }
        }

        //if( /*data.equalsIgnoreCase( "position" )*/ address.size() > 0 )
        //{
            //Create for loop to create the string
            String msg = "";
            msg = players + "#";
            for(int i = 0; i < players; i++)
            {
                        msg += + i + ":" + assets.get(i).returnPosX() + ":" + assets.get(i).returnPosY() + ":"; 
            }
            //msg = String.valueOf( 
            //      players + ":" + 
            //      "1:" + assets.get(0).returnPosX() + ":" + assets.get(0).returnPosY() );
            int msgLength = msg.length();
            byte[] message = msg.getBytes();
            for(int i = 0; i < address.size() ; i++)
            {
                DatagramPacket p = new DatagramPacket( message, msgLength, address.get(i), port );
                try 
                {
                    socket.send( p );
                } 
                catch (IOException e2) 
                {
                    Log.d(TAG, "Error with sending");
                    e2.printStackTrace();

                }
            }
            //Log.d(TAG, "Data sent is:" + msg);
        //}

        data = " ";

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

        data = new String( buf, 0, packet.getLength() );
        //Log.d(TAG, "Data received was :" + data);

        try 
        {
            this.sleep( 25 );
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            Log.d(TAG, "Error with trying to sleep");
            e.printStackTrace();
        }
    }
    Log.d(TAG, "Error with while run value");
}

对评论感到抱歉

1 个答案:

答案 0 :(得分:1)

似乎发生了死锁情况,两个源代码都挂在socket.receive

幸运的是,Android和J2SE UDP服务器客户端代码的代码完全相同,因此请在您的机器中尝试此代码并进行调试以查看发生的情况。您可以更好地了解打印报表。

此外,UDP不可靠。我没有看到代码中实现的数据包接收确认和重发机制。这是必需的。您不能只假设您发送的数据包将被接收到另一端。