Java - ПРОДОЛЖЕНИЕ ОТ:35581167 - Исключение EOF при попытке получить ImageIcons через ObjectInputStream.readObject()
Это продолжение предыдущего вопроса: 35581167
Проверено, что предыдущая информация все еще отправляется через writeObject(<string>)
что это так, но когда программа достигает readObject()
для первого ImageIcon я получаю это сообщение об ошибке:
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...
}
Функция, которую клиент использует для приема информации:
private static void retrieveItemInformation() throws InterruptedException, ClassNotFoundException, IOException
{
txtBoxServerMessage.setText("CLIENT: Waiting for Item Information...");
Thread.sleep(3000);
clientNumber = (int) inputFromServer.readObject();
item1Code = (String) inputFromServer.readObject();
item1Desc = (String) inputFromServer.readObject();
item1Deadline = (String) inputFromServer.readObject();
item1AuctionValue = (double) inputFromServer.readObject();
item2Code = (String) inputFromServer.readObject();
item2Desc = (String) inputFromServer.readObject();
item2Deadline = (String) inputFromServer.readObject();
item2AuctionValue = (double) inputFromServer.readObject();
item1Image = (ImageIcon) inputFromServer.readObject(); // READING IMAGE 1 = LINE 378
item2Image = (ImageIcon) inputFromServer.readObject(); // READING IMAGE 2
txtBoxServerMessage.setText("DEBUG: FINISHED IMAGE");
txtBoxServerMessage.setText("CLIENT: Information Recieved! Displaying...");
Thread.sleep(1000);
comboItemSelect.addItem(item1Code);
comboItemSelect.addItem(item2Code);
txtBoxClientNumber.setText(String.valueOf(clientNumber));
updateDisplay();
}