簡體   English   中英

客戶端不刷新從服務器發送的數據

[英]Client does not refresh the data sent from Server

我在更新序列化對象時遇到問題。 客戶端不刷新從服務器發送的數據。 我在客戶端中創建了循環,但仍然沒有更新。

    public void run(){
              try {
                    socket = new Socket(host.getText(), new Integer(port.getText()));
                    wyswietlKomunikat("Connected.");

我在客戶端中創建了循環,但仍然沒有更新。

             // Serialization      
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            while(true){
             Pakiet p = (Pakiet) ois.readObject();
             showHome(p.getHome());
             ShowAway(p.getAway());
             showHomeLine(p.getShowHomeLine());
             showAwayLine(p.getShowHomeLine());

             // end of serialization

                    in = new Scanner(socket.getInputStream());                    
                    while(in.hasNextLine()){
                            showComment(in.nextLine());
                    }
              }
            }

      catch (UnknownHostException e) {
            showComment("no connection!");

      }
      catch (IOException e) {
            showComment(e.toString());
      } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
      finally {
             try {
                  socket.close();
            }
            catch(IOException e) {
            }
     }

嘗試

// loop forever
while(true) {
    // check if there is something new
    if (in.hasNextLine()) {
        // there is, read it
        showComment(in.nextLine());
    }
}

這里的問題是,您正在同一套接字上混合使用兩種流。 不要那樣做 由於存在緩沖,因此流之間可以相互“竊取”數據。 繼續使用對象流,或者根本不使用對象流,無論您想要什么,僅使用行,字節。

您還可能遇到ObjectOutputStream.reset()是解決方案的問題:請參見其Javadoc。

暫無
暫無

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

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