0

接続するクライアントがあり、ホストに文字列を送信して、ホストがそのパケットをどう処理するかを認識します。クライアントの起動方法は次のとおりです

public void connectToServer()
{
    //Send a connect message to the server
    try {
        socket = new DatagramSocket( port );
        byte[] bufer = new byte[256];
        String msg = "connect";
        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
        e2.printStackTrace();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Receive the message back
    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() );
    //ID = Integer.parseInt( data );

    Log.d(TAG, "Data received was :" + ID);
}

クライアントが接続されると、これを実行し続けます

while( true )
    {
        //Tell the server to give position of players
        if( host == false)
        {
            try {
                if(socket == null)
                {
                    socket = new DatagramSocket( port );
                }
                byte[] bufer = new byte[256];
                String msg = "position";
                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
                e2.printStackTrace();
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                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() );
            Log.d(TAG, "Data received is in run method:" + data);
            //Take the data from the server and use it
            //String[] pos = data.split(":");
            //assets.get(0).setPosition( Integer.parseInt( pos[0] ), Integer.parseInt( pos[1] ) );
            //ID = Integer.parseInt( data );
        }
    }

現在、サーバーは次のように実行されます

protected Object doInBackground(Object... params) {
    Log.d(TAG, "Ip address used:" + getLocalIPAddress() );
    InetAddress client = null;
    boolean run = true;
    String data = "";
    while( run )
    {
        Log.d(TAG, "Data value:" + data);
        if( data.equalsIgnoreCase( "" ) )
        {
            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");
            players++;
            String msg = String.valueOf( players );
            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();
                data = "";
            }
        }

        if( data.equalsIgnoreCase( "position" ) )
        {
            Log.d(TAG, "position send");
            String msg = String.valueOf( assets.get(0).returnPosX() + ":" + assets.get(0).returnPosY() );
            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();

            }
        }

        data = "";

        //Receive some data
        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();
        }

        data = new String( buf, 0, packet.getLength() );
        client = packet.getAddress();

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

    }
    return null;
}

.data.equalsIgnoreCase( "position" ) .data.equalsIgnoreCase( "connect" ) および data.equalsIgnoreCase( "" ) のように、これは機能しますが、IF ステートメントでは機能しません。サーバーはクライアントこれ

data received is in run: 250:300
data received is in run: 51 (depends on how long the client has connected for)

しかし、サーバーはIDをもう送信するべきではなく、250:300である私のオブジェクトの位置だけです

ここに大きなログがありますので、ご理解ください

03-26 17:53:53.814: D/gameClient(20073): Data received is in run method:928
03-26 17:53:53.814: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.824: D/gameClient(20073): Data received is in run method:929
03-26 17:53:53.834: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.844: D/gameClient(20073): Data received is in run method:930
03-26 17:53:53.854: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.864: D/gameClient(20073): Data received is in run method:931
03-26 17:53:53.874: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.884: D/gameClient(20073): Data received is in run method:932
03-26 17:53:53.894: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.914: D/gameClient(20073): Data received is in run method:933
03-26 17:53:53.914: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.934: D/gameClient(20073): Data received is in run method:934
03-26 17:53:53.944: D/gameClient(20073): Data received is in run method:250:300
03-26 17:53:53.954: D/gameClient(20073): Data received is in run method:935

また、サーバーのトレースを行いました。接続後にクライアントから位置だけを受信して​​いますが、IDを送信し続ける理由についてまだ混乱していますか???

4

0 に答える 0