繁体   English   中英

doInBackground卡住了

[英]doInBackground gets stuck

我正在尝试创建一个使用多个端口的服务器,因此可以轻松地发送和接收不同的数据和信息,但在我的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启动它们?

从Javadoc for DatagramSocket.receive

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM