簡體   English   中英

無法從多線程客戶端服務器應用程序中獲取正確的值

[英]Not getting back the correct value from a multithreaded client server application

我目前正在開發一個客戶端服務器應用程序,其中2個客戶端在JFrame上具有一個橢圓形,他們每個都可以看到其位置。

我的問題是,當Client1獲得Client2的位置時,他沒有獲得正確的值,因為對手在Frame上的橢圓位置不正確。 我決定使兩個橢圓都向下移動(增加y值)並打印oppYPos。 似乎當y = 250時,它會回到y = 4。

我能夠獲得兩個Client的起始位置並在兩個窗口上繪制橢圓形,但是當我運行該線程時,一切都從那里走下坡路...

由於我無法發布圖片,因此這里有一些代碼:

這發生在客戶端線程中:movePlayer,向下移動橢圓形,檢查碰撞,然后將當前的x和y值發送到服務器,然后服務器將這些新值設置為第二個客戶端的Opponent值,並獲取其opp位置另一個客戶

            //This while loop is in the run method
    while(true){
        movePlayer();
        checkForCollisions();
        sendValuesToServer();
        getOppValuesFromServer();
        repaint();

        try {
            Thread.sleep(120);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


   public  void sendValuesToServer()
{
    try {
        outputToServer.write(myXPos);
        outputToServer.write(myYPos);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public  void getOppValuesFromServer(){

    try {
        this.oppXPos = inputFromServer.read();
        this.oppYPos = inputFromServer.read();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

這是服務器端

       //The xStartPos are overriden by the new x and y values from the client
             while (true) {
        synchronized (this) {

            // Getting the new variables back
            try {
                xStartPos = inputFromClient.read();
                yStartPos = inputFromClient.read();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        // Setting the new values for opponents
        synchronized (this) {
            for (SnakeServerThread sst : snakeThreads) {

                if (sst != this) {
                    currentOppXPos = sst.xStartPos;
                    currentOppYPos = sst.yStartPos;
                }

            }
        }

        synchronized (this) {
            try {
                outputToClient.write(currentOppXPos);
                outputToClient.write(currentOppYPos);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

直到y值為250為止,這個方法都可以正常工作...由於某種原因,它將其設置為零,並且對手在屏幕中間消失並從頂部開始。

抱歉,如果我添加了太多內容,但是在過去的2個小時中,我一直在努力解決這個問題,並且沒有運氣!!

謝謝

直到y值為250為止,這個方法都可以正常工作...由於某種原因,它將其設置為零,並且對手在屏幕中間消失並從頂部開始。

您的問題很可能是您試圖向服務器寫入一個整數,但是OutputStream.write(byte)只看該值的最低8位。 引用javadocs:

將指定的字節寫入此輸出流。 write的一般約定是將一個字節寫入輸出流。 要寫入的字節是參數b的八個低階位。 b的24個高階位被忽略。

一旦超過字節的8個字節,您將再次轉換為0。 如果值大於255,則需要寫入多個字節。應將位置寫為一系列字節:

...write(pos & 0xFF);
...write((pos & 0xFF00) >> 8);
// if the position can be more than 2 bytes
//...write((pos & 0xFF0000) >> 16);
//...write((pos & 0xFF000000) >> 24);

接着:

pos = ...read();
pos |= ...read() << 8;
// if the position can be more than 2 bytes
// pos |= ...read() << 16;
// pos |= ...read() << 24;

這樣的事情。 或者,您可以使用序列化並直接編寫shortint

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM