0

複数のポートを使用するサーバーを作成して、さまざまなデータと情報を簡単に送受信できるようにしようとしていますが、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 以外からスレッドを開始する方法がわかりません。

4

1 に答える 1

2

DatagramSocket.receiveの Javadoc から:

このメソッドは、データグラムを受信するまでブロックします

データ パケットが到着するのを待っているため、コードは「スタック」しています。これは、このサーバー/ポートに接続しているクライアントがまだ別のパケットを送信していないため、ソケットが設計上ブロックされていることを意味します。

伝統的に、同時に複数のポートでリッスンしたい場合は、2 つのオプションがあります。

  1. 異なるスレッドで各ブロッキング ソケットを開きます (スレッドごとに 1 つのクライアントが存在するため、多数のクライアントが同時に存在する場合、これは適切にスケーリングされませんが、少数の接続では問題なく機能します)。
  2. Java のノンブロッキング IO である NIO を使用する
于 2013-04-04T03:10:14.663 に答える