0

我在更新我的序列化对象时遇到问题。客户端不刷新服务器发送的数据。我在客户端中创建了循环,但它仍然没有更新。

    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) {
            }
     }
4

2 回答 2

0

尝试

// loop forever
while(true) {
    // check if there is something new
    if (in.hasNextLine()) {
        // there is, read it
        showComment(in.nextLine());
    }
}
于 2013-06-15T15:38:37.147 回答
-1

这里的问题是您在同一个套接字上混合使用两种流。不要那样做。由于缓冲,流可以相互“窃取”数据。继续使用对象流,或者根本不使用,只是和行,字节,无论你想要什么。

您可能还会遇到 ObjectOutputStream.reset() 是解决方案的问题:请参阅它的 Javadoc。

于 2013-06-16T00:52:58.993 回答