简体   繁体   English

使用Socket发送和接收对象

[英]Sending and receiving object with Socket

I'm writing a little client-server app for creating auctions. 我正在编写一个用于创建拍卖的小型客户端服务器应用程序。 It works on Sockets, client app and server app are exchanging 2 kind of objects - an auction objects, and clients objects. 它适用于Sockets,客户端应用程序和服务器应用程序正在交换2种对象-拍卖对象和客户端对象。 Clients works fine, but there is a problem with auctions. 客户运作良好,但是拍卖存在问题。 When the app is sending one specific auction for the first time it works nice, let's tell the first prize is 100.00. 当应用首次发送特定的竞价时,它的效果很好,让我们说一等奖是100.00。 The other clients receive this auction. 其他客户收到此拍卖。 But when someone bid's there a mirracle occurs. 但是当有人出价时,就会发生奇迹。 I've debugged the connection and the client app is sending an auction with the new prize (110.00), but server receives auction with the old prize (100.00). 我已经调试了连接,客户端应用正在发送带有新奖品(110.00)的拍卖,但是服务器收到带有旧奖品(100.00)的拍卖。 What may couse this problem? 可能导致此问题的原因是什么?

Here's the auction class: 这是拍卖课:

public class Auction implements Comparable<Auction>, Serializable{

private Double prize;
private Item item;
private Client winner;
private Client owner;

public Auction(double prize, Item item, Client owner){
    this.prize = prize;
    this.item = item;
    this.owner = owner;
    this.winner = owner;
}

public Auction(double prize, Item item, Client owner, Client winner){
    this.prize = prize;
    this.item = item;
    this.owner = owner;
    this.winner = winner;
}

public void placeBid(double bidValue, Client winner){
    double newPrize = prize + bidValue;
    setWinner(winner);
    setPrize(newPrize);
}

@Override
public int compareTo(Auction auction) {
    int compare = prize.compareTo(auction.getPrize());
    return compare;
}

public String toString(){
    String value = String.format(item + " : %1$.2f | winner: " + winner, prize);
    return value;
}

public double getPrize(){
    return prize;
}

public Client getWinner(){
    return winner;
}

public Client getOwner(){
    return owner;
}

public Item getItem(){
    return item;
}

public boolean equals(Object anAuction){
    Auction auction = (Auction) anAuction;
    Client testOwner = auction.getOwner();
    Item testItem = auction.getItem();
    String testItemName = testItem.getName();
    String itemName = item.getName();
    double testPrize = auction.getPrize();
    return owner.equals(testOwner) && itemName.equals(testItemName);
}

private void setPrize(double prize){
    this.prize = prize;
}

private void setWinner(Client winner){
    this.winner = winner;
}
}

A method that is sending this auction on client side: 在客户端发送此拍卖的方法:

private void sendAuctions() throws IOException {
    for(Auction auction : auctionList){
        outputStream.writeObject("AUCTIONS");
        outputStream.flush();
        outputStream.writeObject(auction);
        outputStream.flush();
    }
}

and a method that receives the auction on server side: 以及在服务器端接收拍卖的方法:

private void receiveData() {
    String receivedDataLabel = "";
    try {
        while (!receivedDataLabel.equals("END")) {
            receivedDataLabel = (String) inputStream.readObject();
            if (receivedDataLabel.equals("CLIENTS")) {
                receiveClients();
            } else if (receivedDataLabel.equals("AUCTIONS")) {
                receiveAuctions();
            } else if (receivedDataLabel.equals("CONNECTION_END")){
                isConnected = false;
            }
        }
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
}

private void receiveAuctions() throws ClassNotFoundException, IOException {
    Auction auction = (Auction) inputStream.readObject();
    dataContainer.registerAuction(auction);
}

Java serialization preserves object graph integrity, at the possibly confusing expense of not retransmitting objects that have already been sent. Java序列化保留了对象图的完整性,但不重新传输已经发送的对象可能会造成混淆。 You need to look at ObjectOutputStream.reset() or ObjectOutputStream.writeUnshared() and the reasons they exist. 您需要查看ObjectOutputStream.reset()ObjectOutputStream.writeUnshared()及其存在的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM