繁体   English   中英

在Java中通过Socket发送PriorityQueue会发送一个空队列

[英]Sending a PriorityQueue over Socket in Java sends an empty queue

我有两个程序-服务器和客户端。 基本上,服务器的工作是维护某个“项目”类的对象的ArrayList。 可以将项目添加到ArrayList中,也可以根据客户端应用程序的请求更改现有项目。 有时,客户端应用程序必须接收列表中包含的某些项目,而这些正是问题所在。 Item类具有其他对象(可序列化)的PriorityQueue作为实例变量。 问题在于,当服务器发送某些项目时,客户端会接收所有内容,但每个项目的优先级队列为空(正确接收了Item对象的所有其他实例变量)。

现在的代码:
这是与问题相关的Item类的一部分:

public class Item implements Serializable   
{  
    ...Some instance variables...   
    PriorityQueue<Bid> pq;

    public Item(...)  
    {      
        pq = new ProrityQueue<Bid>(); 
    }  

    public void addStuff(Stuff bid)  
    {  
        this.pq.add(bid);  
    }  

    ...other methods...      
}  

发送客户端请求的ArrayList我只是在服务器端

ObjectOutputStream oos = new ObjectOutputStream(serverSocket.getOutputStream);
oos.writeObject(someArrayList<Item>);  

在客户端

ObjectInputStream ois = new ObjectInputStream(serverSocket.getInputStream);  
ArrayList<Item> tempList = (ArrayList<Item>) ois.readObject();  

我发现客户端接收到它必须接收的每个对象及其所有实例变量(包括PriorityQueue),但PriorityQueue为空,这真的很奇怪。 为什么PriorityQueue中的对象是客户端唯一未收到的东西? 关于通过套接字发送对象列表,其中包含其他对象的集合,我还应该了解一些其他信息吗?

编辑 :我正在发布部分代码以获取更多详细信息。 实际上,对于服务器与客户端之间的通信,我使用ClientCommunications类和ServerCommunication类来处理所有内容,因此这两个应用程序不必处理套接字。

发送一些我有客户要求的物品

ArrayList<Item> list = handleItemsRequest(rim); 
//this uses the server output stream to transfer an object to the client's output stream   
comms.sendItems(list);  

sendItems(list)方法的代码:

public void sendItems(ArrayList<Item> items)
{
    try
    {
        //oos is the output stream of the server socket
        oos.writeObject(items);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

其中handleItemsRequest()是一种接受一些约束,然后仅返回与它们匹配的项目的方法:

public ArrayList<Item> handleItemsRequest(RequestItemsMessage rim)  
{  
    //it is the new array list that consist of items matching the conditions
    ArrayList<Item> it = new ArrayList<Item>();     
    //items is the arrayList maintained by the server  
    for (Item item : items)  
    {  
       if (item.matchesConditions)  
       {  
           it.add(item);
       }
    }

    //it contains all the right items and the priorityQueue for each item has everything that it has to have  
    return it;  
}  

然后,客户端应用通过调用来接收列表

//this comms variable is different from the one in the server and is a par of a different class  
ArrayList<Item> list = comms.receiveItems(); 

receiveItems()的代码:

public ArrayList<Item> receiveItems()
{
    try
    {
        ArrayList<Item> list = (ArrayList<Item>) ois.readObject();
        return list;
    }
    catch(ClassNotFoundException | IOException e)
    {
        e.printStackTrace();
    }

    return null;
}

现在,客户端具有所有必须发送的项目,但它们的优先级队列为空。
还有更多代码,但是与这个问题无关,我不需要全部发布。

解决方案事实证明,在发送列表之前,必须重置服务器的OutputStream。 否则,客户端将一次又一次地获取旧实例。 非常感谢EJP指出了这个问题!

您曾经关闭流吗? 尝试在oos.close()之后调用oos.close() oos.writeObject()

暂无
暂无

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

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