2016-08-09 90 views
0

我正在編寫一個用於創建拍賣的小客戶端 - 服務器應用程序。它適用於套接字,客戶端應用程序和服務器應用程序正在交換兩種對象 - 拍賣對象和客戶端對象。客戶工作正常,但拍賣有問題。當應用程序第一次發送特定的拍賣作品時,它表現不錯,讓我們先說一等獎是100.00。其他客戶收到此次拍賣。但是當有人在那裏出價時就會出現一個奇蹟。我調試了連接,客戶端應用程序發送了一個新的獎品(110.00),但服務器收到舊的獎品(100.00)的拍賣。什麼可能會導致這個問題?使用套接字發送和接收對象

這裏的拍賣類:

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

以及接收在服務器側的拍賣的方法:

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); 
} 

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; 
} 
} 

正在發送在客戶端該拍賣的方法

+0

您能提供例外嗎? http://stackoverflow.com/help/how-to-ask –

+0

有沒有例外,程序的作品,它只是更新客戶端應用程序的獎品,將更新的拍賣發送到服務器,服務器接收拍賣,但與舊的獎品。 – egzaell

回答

1

Java序列化保留了對象圖的完整性,但可能會造成混淆,而不是重新傳輸處理已發送的對象。您需要查看ObjectOutputStream.reset()ObjectOutputStream.writeUnshared()及其存在的原因。

+0

這很好,謝謝! – egzaell