0

あるデバイスで単純なUDPサーバーを実行しています。クライアントが接続したい場合、サーバーに「接続」メッセージを送信し、サーバーはアドレスを取得し、別のクライアントが必要なときにそのアドレスにデータを送信し始めます参加すると、そのクライアントは「接続」メッセージを送信しますが、何も起こらない場合があります。参加したいクライアントのメッセージがサーバーに受信されない場合があります。これを修正するにはどうすればよいですか? また、ここにサーバーのコードがあります

 protected String doInBackground(String... params) {
    boolean run = true;
    String data = "";
    DatagramPacket packet = null;
    boolean position = false;
    while( run )
    {
        if( data.equalsIgnoreCase( "" ) )
        {

        }

        //Send some data
        if( data.equalsIgnoreCase( "connect" ) && wait == true )
        {
            Log.d(TAG, "Someone wants to connect");
            //Increase the total players by 1
            players = players + 1;
            //Notify to the host (client) something has change
            //notify client
            //Send a message to the client with the ID
            byte[] bufer = new byte[256];
            //Send a message "connect" to the host
            String msg = Integer.toString( players );
            int msgLength = msg.length();
            bufer = msg.getBytes();
            InetAddress address;
            //Default ip address of the host
            //Take the address from the packet
            addresses.add( packet.getAddress() );
            Log.d(TAG, "Address is " + addresses.get( addresses.size() - 1 ) );
            address = addresses.get( addresses.size() - 1 );
            DatagramPacket p = new DatagramPacket( bufer, bufer.length , address, port );
            //Send packet
            try 
            {
                socket.send( p );
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

            wait = false;
        }

        if( wait == true && position == true )
        {
            position = false;
            wait = false;
        }

        for(int i = 0;i < positions.length; i++)
        {
            if(positions[i] != null)
            {
            //Log.d(TAG, "X and Y position of asset:"+i+", is:"+ positions[i]);
            }
        }

        //Needs to try and reteive data...
        if( wait == false )
        {
            //Log.d(TAG, "Waiting to retreive data");
            byte[] buf = new byte[256];
            packet = new DatagramPacket( buf, buf.length );
            try 
            {
                socket.receive( packet );
                wait = true;
            } 
            catch (IOException e) 
            {
                Log.d(TAG, "Error with receiving data");
                e.printStackTrace();
            }

            data = new String( buf, 0, packet.getLength() );
            //Log.d(TAG, "Data received from :" + packet.getAddress() + ", holds this value: " + data);
            String[] dataStrings = data.split(":");
            if( dataStrings[0].equalsIgnoreCase( "position" ) )
            {
                position = true;
            }
        }



        //Log.d(TAG, "Data received was :" + data);

        /*try 
        {
            Thread.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");
    return "finished";
}

サーバーに接続するためのクライアント コード

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 );

    } catch (UnknownHostException e2) {
        Log.d(TAG, "Unknown host");
        e2.printStackTrace();
    } catch (SocketException e) {
        Log.d(TAG, "Socket problem");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d(TAG, "I/O problem");
        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 
    {
        Log.d(TAG, "Waiting for data");
        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();
}

何かが正しくない場合は、connectToServer メソッドを取得して試行し続ける必要がありますか?

キャンバス

4

1 に答える 1

0

クライアントは、最初の「接続」文字列の後、サーバーに何も送信しません。したがって、サーバーはその行で永遠に待機します。

 socket.receive( packet ); // <- will never return
 wait = true; 

とにかく、サーバーの recv() ループなどの長時間実行されるタスクに AsyncTask を使用することは避け、さまざまなケース (「接続」メッセージまたは既知のクライアントからのメッセージを受信する) に合わせてサーバー コードをリファクタリングする必要があります。

于 2013-04-03T14:29:35.220 に答える