我一直在尝试调试这个 2 个小时,但我无法解释它。我有一个服务器和一个客户端。(服务器管理一些拍卖)。
发生什么了:
客户端请求一些东西,服务器发回数据,客户端接收它就好了。
客户端向服务器发送一些东西,服务器更新一些数据。
客户端发出与第一次(1 .)
正在发送的数据只是一个带有两个 List-s 的 Java Bean。和代码:
 // CLIENT CLASS
// creates socket, sends and listens on the socket
// listening is done on a separate thread
public class ServerConnector {
private Socket socket = null;
private ObjectOutputStream out = null;
private Display display;
private ServerListener listener;
public ServerConnector(Display display) {
    this.display = display;
        try {
            socket = new Socket("localhost",33333);
            out = new ObjectOutputStream(socket.getOutputStream());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        listener = new ServerListener(socket, display);
        new Thread(listener).start();
}
public void sendRequest(Request request) {
    try {
        out.writeObject(request);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
class ServerListener implements Runnable {
    private Socket socket;
    private ObjectInputStream in = null;
    private Display display;
    public ServerListener(Socket socket,Display display) {
        this.socket = socket;
        this.display = display;
        try {
            in = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        Response response =null;
        try {
            while ((response = (Response)in.readObject()) != null) {
                if (response.getCars().size() > 0) {
                    display.showAvailableCars(response.getCars());
                }
                if(response.getAucs().size() > 0) {
                    List<Auction> auctionz  = response.getAucs();//HERE 1st time it gets the GOOD data, 2nd time should get UPDATED DATA but instead receives the OLD DATA (same as 1st time).
                    display.showOpenAuctions(auctionz);
                }
                response = null;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}
//CLIENT CLASS
// controls when something should be sent, and print out responses
public class Display {
Scanner console = new Scanner(System.in);
ServerConnector server = new ServerConnector(this);
List<Car> cars;
List<Auction> aucs;
public void show() {
    int opt = 0;
        System.out.println("1. Show available cars for auction.");
        System.out.println("2. Show open auctions.");
        opt = console.nextInt();
        Request request = new Request();
        if (opt == 1)
            request.setRequest(Request.GET_CARS);
        if (opt == 2) {
            request.setRequest(Request.GET_OPEN_AUCTIONS);
        }
        server.sendRequest(request);
    }
public void showAvailableCars(List<Car> cars) {
    int i = 0;
    for (Car c : cars ){
        i++;
        System.out.println(i +". " + c.getMaker() + " " + c.getModel() + " price: " + c.getPrice());
    }
    System.out.println("Select car to open Auction for:");
    int selectedCar = console.nextInt();
    if (selectedCar != 0) {
        if (selectedCar <= cars.size()) {
            Request request= new Request();
            request.setRequest(Request.OPEN_AUCTION);
            Car c = cars.get(selectedCar-1);
            request.setCar(c);
            server.sendRequest(request);
        }
    }
    show();
}
public void setCars(List<Car> cars) {
    this.cars = cars;
}
public void showOpenAuctions(List<Auction> aucs2) {
    int i = 0;
    for (Auction auc : aucs2) {
        i++;
        System.out.println(i+ ". " + auc.getCar().getModel() + " " + auc.getCar().getMaker() + " last price: " + auc.getPrice());
    }
    System.out.println("You can now make offers");
    System.out.println("Input auction number:");
    int selectedAuction = 0;
    selectedAuction = console.nextInt();
    if (selectedAuction > 0 && selectedAuction <= aucs2.size()) {
        System.out.println("Offer new price:");
        int price = console.nextInt();
        Request request= new Request();
        request.setRequest(Request.MAKE_OFFER);
        request.setAuctionId(aucs2.get(selectedAuction-1).getId());
        request.setPrice(price);
        server.sendRequest(request);
    }
    show();
}
public void setOpenAuctions(List<Auction> aucs2) {
    this.aucs = aucs2;
}
}
// SERVER CLASS : send and receives
public class ClientManager implements Runnable {
private AuctionManager manager = new AuctionManagerImpl();
private Socket client;
private ObjectInputStream in = null;
private ObjectOutputStream out = null;
public ClientManager(Socket socket) {
    this.client = socket;
     try {
          in = new ObjectInputStream(client.getInputStream());
          out = new ObjectOutputStream(client.getOutputStream());
     } catch(Exception e1) {
             try {
                 e1.printStackTrace();
                client.close();
       }catch(Exception e) {
               System.out.println(e.getMessage());
             }
             return;
         }
}
@Override
public void run() {
    Request req = null;
    try {
            while ((req = (Request)in.readObject()) != null) {
                if (req.getRequest() != null) {
                    if (req.getRequest().equals(Request.GET_CARS)) {
                        Response response = new Response();
                        response.setCars(manager.getAvailableCars());
                        out.writeObject(response);
                        continue;
                    }
                    if (req.getRequest().equals(Request.OPEN_AUCTION)) {
                        manager.openAuction(req.getCar());
                        continue;
                    }
                    if (req.getRequest().equals(Request.GET_OPEN_AUCTIONS)) {
                        Response response = new Response();
                        response.setAucs(manager.getHoldedAuctions()); //this line ALWAYS sends to the client GOOD, UPDATED DATA
                        out.writeObject(response);
                        out.flush();
                        continue;
                    }
                    if (req.getRequest().equals(Request.MAKE_OFFER)) {
                        Auction auction = manager.getOpenAuction(req.getAuctionId());
                        manager.updateAuction(auction, req.getPrice(),client.getRemoteSocketAddress().toString());
                        continue;
                    }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
这可能是因为您正在使用ObjectOutputStreams. 请记住,这ObjectOutputStreams将缓存写入它们的所有对象,以便将来再次写入同一对象时,它可以写入反向引用而不是重写整个对象。这在编写对象图时是必要的。
您的代码片段:
if (req.getRequest().equals(Request.GET_CARS)) {
    Response response = new Response();
    response.setCars(manager.getAvailableCars());
    out.writeObject(response);
    continue;
}
正在写入由 返回的对象manager.getAvailableCars()。下次收到请求时,将写入相同的对象(但现在具有不同的内容) - 但ObjectOutputStream不知道新内容,因此它只写入反向引用。在ObjectInputStream另一端看到的反向参考,并返回其读取上次相同的对象,即原始数据。
您可以通过ObjectOutputStream.reset()在每次响应后调用来解决此问题。这将清除流的缓存。
| 归档时间: | 
 | 
| 查看次数: | 2224 次 | 
| 最近记录: |