Java - CONTINUATION FROM:35581167 - 尝试通过ObjectInputStream.readObject()接收ImageIcons时出现EOF异常

时间:2016-02-23 17:40:00

标签: java object server client imageicon

这是上一个问题的延续:35581167

测试过前面的信息仍然通过WM_QUERYUISTATE发送,但是当程序到达第一个ImageIcon的writeObject(<string>)时,我收到以下错误消息:

readObject()

这是每个部分的更改代码:

服务器线程代码已更新:

Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(Unknown Source)
at java.io.ObjectInputStream.readInt(Unknown Source)
at javax.swing.ImageIcon.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at AuctionClient.retrieveItemInformation(AuctionClient.java:378)
at AuctionClient.main(AuctionClient.java:356)

向客户发送信息:

public ClientHandler(Socket socket, BiddingItem item1, BiddingItem item2, int number) throws IOException
{
    client = socket;
    output = new ObjectOutputStream(client.getOutputStream());
    input = new ObjectInputStream(client.getInputStream());
    startedItem1 = item1;
    startedItem2 = item2;
    clientNumber = number;
}

客户端创建输入流:

public void run()
{
    //previous code..
    try
    {
        output.writeObject(clientNumber);

        output.writeObject(Integer.toString(startedItem1.getCode()));
        output.writeObject(startedItem1.getDescription());
        output.writeObject(startedItem1.getDeadlineString());
        output.writeObject(startedItem1.getCurrentPrice());

        output.writeObject(Integer.toString(startedItem2.getCode()));
        output.writeObject(startedItem2.getDescription());
        output.writeObject(startedItem2.getDeadlineString());
        output.writeObject(startedItem2.getCurrentPrice());

        output.writeObject(startedItem1.getImage());
        output.writeObject(startedItem2.getImage());
    }
    catch(IOException IOEx)
    {
        System.out.println("Unable to send information!");
    }
    //code continues...
}

客户端用来接受信息的功能:

public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException 
{
    //previous code...
    try
    {
        txtBoxServerMessage.setText("CLIENT: Attempting to connect to server...");
        Thread.sleep(2000);
        socket = new Socket(host, PORT);

        inputFromServer = new ObjectInputStream(socket.getInputStream());
        outputToServer = new ObjectOutputStream(socket.getOutputStream());
    }
    catch(IOException ioEx)
    {
        txtBoxServerMessage.setText("CLIENT-ERROR: Unable to connect to server! Exiting...");
        Thread.sleep(2000);
        System.exit(1);
    }
//code continues...
}

0 个答案:

没有答案