doInBackground卡住了

时间:2013-04-04 02:38:08

标签: java android sockets networking udp

我正在尝试创建一个使用多个端口的服务器,因此可以轻松地发送和接收不同的数据和信息,但在我的doInBackground方法中,我的代码卡在了socket.receive上, 这是我的代码

while( run )
    {
        //GameServerID
        try
        {
            if(gameServerID == null)
            {
                gameServerID = new DatagramSocket( portID );
            }
            //try to receive data
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket( buf, buf.length );
            try
            {
                Log.d(TAG, "Wait for something to connect");
                gameServerID.receive( packet ); <--GETS STUCK HERE
            }
            catch (IOException e) 
            {
                Log.d(TAG, "Error with receiving data");
                e.printStackTrace();
            }
            String data = new String( buf, 0, packet.getLength() );
            Log.d(TAG, data);
            //Send out the ID to the client
            byte[] bufer = new byte[256];
            //Send a message "connect" to the host
            String msg = Integer.toString( players );
            players = players + 1;
            bufer = msg.getBytes();
            InetAddress address;
            //Default ip address of the host
            address = packet.getAddress();
            DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, portID );
            //Send packet
            gameServerID.send( p );
            addresses.add( address );
        }
        catch (SocketException e) 
        {
            Log.d(TAG, "Error with socket");
            e.printStackTrace();
        } 
        //Listen for a client to connect
        catch (IOException e) 
        {
            Log.d(TAG, "Error with I/O");
            e.printStackTrace();
        }

        //GameServerPositions
        try
        {
            Log.d(TAG, "Run the gamePositions code");
            if(gamePositions == null)
            {
                gamePositions = new DatagramSocket( portPos );
            }
            //Receive position
            //try to receive data
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket( buf, buf.length );
            try
            {
                gamePositions.receive( packet );
            }
            catch (IOException e) 
            {
                Log.d(TAG, "Error with receiving data");
                e.printStackTrace();
            }
            String data = new String( buf, 0, packet.getLength() );
            Log.d(TAG, data);
            String[] pos = data.split(":");
            for(int i = 0;i<pos.length;i++)
            {
                Log.d(TAG, pos[i]);
            }
            xValues[ Integer.parseInt( pos[0] ) ] = Integer.parseInt( pos[1] );
            yValues[ Integer.parseInt( pos[0] ) ] = Integer.parseInt( pos[2] );
        }
        catch (SocketException e) 
        {
            Log.d(TAG, "Error with socket");
            e.printStackTrace();
        } 
        //Listen for a client to connect
        catch (IOException e) 
        {
            Log.d(TAG, "Error with I/O");
            e.printStackTrace();
        }

        //GameServerSendPos
        try
        {
            Log.d(TAG, "Run the gamePositionsSend code");
            String data = "";
            if( gameSendPos == null )
            {
                gameSendPos = new DatagramSocket( portSend );
            }

            //create the string ready to be sent out
            for(int i = 0; i < 8; i++)
            {
                if(xValues[i] >= 0)
                {
                    data += i + ":" + xValues[i] + ":" + yValues[i] + ":";
                }
            }

            byte[] bufer = new byte[256];
            bufer = data.getBytes();
            DatagramPacket p = null;

            for(int i = 0;i < addresses.size(); i++)
            {
                if( addresses.get(i) != null )
                {
                    p = new DatagramPacket( bufer, bufer.length , addresses.get(i), portSend );
                    gameSendPos.send( p );
                }
            }

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

我可以做些什么来阻止它被卡住,或者经过漫长的等待,只是为了跳过它?只是为了获得更多信息,当第一个客户端连接时,代码工作正常,直到它再次到达顶部然后socket.received刚刚堵塞...

帆布

更新

我已将我的代码更改为3个不同的类,我在我的服务器中启动它们,而循环就像这样

while( run )
    {
        if(start == true)
        {
            gsID.doInBackground( );
            addresses = gsID.addresses;
            gsPos.addresses = addresses;
            gsPos.doInBackground( );
            gsSendPos.addresses = addresses;
            gsSendPos.positions = gsPos.positions;
            gsSendPos.doInBackground( );
            start = false;
        }
        else
        {
            addresses = gsID.addresses;
            gsPos.addresses = addresses;
            gsSendPos.addresses = addresses;
            gsSendPos.positions = gsPos.positions;
        }
    }

但它仍然再次卡在gameServerID接收方法上。

我只是将我的所有类更改为线程,并且效果更好,但在更新版本的android 4.0+线程中无法从UI中使用,但我不确定如何从UI中启动它们?

1 个答案:

答案 0 :(得分:2)

来自DatagramSocket.receive的Javadoc:

  

此方法将一直阻塞,直到收到数据报为止

因此,您的代码“卡住”,因为它正在等待数据包到达。这意味着连接到此服务器/端口的客户端尚未发送另一个数据包,因此套接字被设计阻塞。

传统上,如果您想同时收听多个端口,您有两个选择:

  1. 在另一个线程上打开每个阻塞套接字(如果由于每个线程有一个客户端,但由于每个线程有一个客户端会同时拥有大量客户端,这将无法很好地扩展),但
  2. 使用NIO作为Java的非阻塞IO