2014-02-24 59 views
0

我一直在尝试使用TCP发送数据列表时遇到了一些麻烦。我制作了一个较小版本的问题并夸大了一点。对象输入/输出流。如何反复发送更新的相同对象?

CLIENT

public class Client { 

    DListStorage dl = new DListStorage(); 
    Data d = new Data(); 

    Socket socket; 

    public Client(){ 
     d.x = 0; 
     dl.dl.add(d); 

     try { 
      socket = new Socket(InetAddress.getLocalHost(), port); 
      ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); 

      for(int i = 0; i < 3; i++){ 
       oos.writeObject(dl); 
       dl.dl.get(0).x++; 
       System.out.println("Client: " + dl.dl.get(0).x); 
      } 

      socket.close(); 
      socket = new Socket(InetAddress.getLocalHost(), port); 
      oos = new ObjectOutputStream(socket.getOutputStream()); 

      for(int i = 0; i < 3; i++){ 
       oos.writeObject(dl); 
       dl.dl.get(0).x++; 
       System.out.println("Client: " + dl.dl.get(0).x); 
      } 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
    } 
} 

服务器

public class NewConnections { 

    ServerSocket ssocket; 
    Socket socket; 

    public NewConnections(){ 
     acceptConnections.start(); 
    } 

    Thread acceptConnections = new Thread(new Runnable(){ 
     public void run(){ 
      try { 
       ssocket = new ServerSocket(port, 0, InetAddress.getLocalHost()); 
       socket = ssocket.accept(); 
       ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 

       DListStorage dl; 

       for(int i = 0; i < 3; i++){ 
        dl = (DListStorage)ois.readObject(); 
        System.out.println("Server: " + dl.dl.get(0).x); 
       } 

       socket.close(); 
       socket = ssocket.accept(); 
       ois = new ObjectInputStream(socket.getInputStream()); 

       for(int i = 0; i < 3; i++){ 
        dl = (DListStorage)ois.readObject(); 
        System.out.println("Server: " + dl.dl.get(0).x); 
       } 

       ssocket.close(); 
      } catch (IOException | ClassNotFoundException e) { 
        e.printStackTrace(); 
      } 
     } 
    }); 
} 

对我来说,结果如下。

Client: 0.0 
Client: 1.0 
Server: 0.0 
Client: 2.0 
Server: 0.0 
Server: 0.0 
Client: 3.0 
Client: 4.0 
Server: 3.0 
Client: 5.0 
Server: 3.0 
Server: 3.0 

数据和数据存储实现Serializable,数据存储包含 public List<Data> dl = new ArrayList<>(); 我无法弄清楚,为什么服务器不读浮动X正常。只有在最初的阅读。

感谢

+0

我加'oos.reset()',它的工作,但我真的需要这要尽可能高效。这不会造成很大的开销,打开和关闭流? –

回答

0

尝试:

服务器:

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class NewConnections { 

public NewConnections() { 
    acceptConnections.start(); 
} 

Thread acceptConnections = new Thread(new Runnable() { 
    public void run() { 
     try { 
      int port = 81; 
      ServerSocket ssocket = new ServerSocket(port, 0, InetAddress.getLocalHost()); 
      while (true) { 
       System.out.println("waiting..."); 
       Socket socket = ssocket.accept(); 
       // buffered reader to read a line at a time from client 
       BufferedReader ois = new BufferedReader(new InputStreamReader(
         socket.getInputStream())); 

       String x = null; 
       // read till input is available from client 
       while ((x = ois.readLine()) != null) { 
        System.out.println("Server: " + x); 
       } 
       ois.close(); 
       socket.close(); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}); 
} 

客户:

import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 

public class Client { 

public Client() { 
    int port = 81; 
    int x = 0; 
    try { 

     Socket socket = new Socket(InetAddress.getLocalHost(), port); 
     // use auto flush to send the data immediately to the server 
     PrintWriter oos = new PrintWriter(socket.getOutputStream(), true); 

     for (int i = 0; i < 3; i++) { 
      x++; 
      oos.println(x); 
      System.out.println("Client: " + x); 
     } 
     oos.close(); 
     socket.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 
+0

在等待每行输入的无限循环中启动服务器线程。现在从客户端逐行发送数据,并立即刷新到服务器。 – Braj

+0

我会尽快与您联系。我发现这个[link](http://note4j.blogspot.co.uk/2008/10/when-to-reset-objectoutputstream.html)对重置out流有很大的帮助。它也给出了一点洞察力,为什么有一个问题开始。谢谢。 –

+0

对不起,它花了这么长时间繁忙的一周..我需要一个对象的数组被发送,否则我需要更多的代码来说明每个单独的值(在数据类),从客户端发送一个后另一个。数据包含大小,位置,布尔值和其他一些值。这是一个移动的精灵,以及发生什么事情。服务器需要被告知所有事情,所以精灵得到了正确的处理。关于其他选择的建议也很好。我期待通过互联网发送和接收数据 –