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

现在这确实有效,但不是 IF 语句,如 .data.equalsIgnoreCase("position")、.data.equalsIgnoreCase("connect") 和 data.equalsIgnoreCase(""),服务器将发送到客户这个

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