背景很少,目前正在尝试完成我的学位作业,尝试实现一个“拍卖行”本地主机服务器客户端程序,其中客户端可以对服务器上保存的元素进行竞价。当新客户端连接到服务器时,用户必须注册他/她的名字,然后服务器将向客户端发送所有项目信息。
目前,所有其他信息都正确发送,但是当我尝试通过 ObjectOutputStream 将项目的图像发送到客户端时,服务器总是无法发送 ImageIcons,当我断开客户端连接时,我遇到了以下错误消息:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: 53 at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at AuctionClient.retrieveItemInformation(AuctionClient.java:365)
at AuctionClient.main(AuctionClient.java:341)
我认为这可能与我尝试直接通过输出流发送 ImageIcons 而不使用可序列化有关,但我们的 java 讲师告诉我们 ImageIcon 不需要它才能正常运行,所以我不确定,有什么建议吗会很好!我将把服务器/客户端的代码放在下面!
服务器线程代码:
public ClientHandler(Socket socket, BiddingItem item1, BiddingItem item2, int number) throws IOException
{
client = socket;
outputImage = new ObjectOutputStream(client.getOutputStream());
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
startedItem1 = item1;
startedItem2 = item2;
clientNumber = number;
images = new ImageIcon[2];
images[0] = startedItem1.getImage();
images[1] = startedItem2.getImage();
}
尝试向客户端发送信息:
public void run()
{
//previous code...
for (int i=0; i<images.length;++i)
{
try
{
outputImage.writeObject(images[i]);
}
catch (IOException ioEx)
{
System.out.println("\n**ERROR: Unable to send Item " + i + " image to client!**");
}
try
{
outputImage.flush();
}
catch (IOException e1)
{
System.out.println("\n**ERROR: Unable to flush OutputObjectStream!**");
}
}
客户端创建输入流:
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 Scanner(socket.getInputStream());
inputImageFromServer = new ObjectInputStream(socket.getInputStream());
outputToServer = new PrintWriter(socket.getOutputStream(), true);
}
catch(IOException ioEx)
{
txtBoxServerMessage.setText("CLIENT-ERROR: Unable to connect to server! Exiting...");
Thread.sleep(2000);
System.exit(1);
}
}
客户端用来接受信息的函数:
private static void retrieveItemInformation() throws InterruptedException, ClassNotFoundException, IOException
{
txtBoxServerMessage.setText("CLIENT: Waiting for Item Information...");
Thread.sleep(3000);
clientNumber = Integer.parseInt(inputFromServer.nextLine());
item1Code = inputFromServer.nextLine();
item1Desc = inputFromServer.nextLine();
item1Deadline = inputFromServer.nextLine();
item1AuctionValue = Double.parseDouble(inputFromServer.nextLine());
item2Code = inputFromServer.nextLine();
item2Desc = inputFromServer.nextLine();
item2Deadline = inputFromServer.nextLine();
item2AuctionValue = Double.parseDouble(inputFromServer.nextLine());
item1Image = (ImageIcon)inputImageFromServer.readObject(); // READING IMAGE 1
item2Image = (ImageIcon)inputImageFromServer.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();
}
干杯!
中号
最佳答案
问题是这样的:
outputImage = new ObjectOutputStream(client.getOutputStream());
// ...
output = new PrintWriter(client.getOutputStream(), true);
您不能将单个 OutputStream 包装在两个不同的包装器中。它们各自的状态和缓冲不兼容。
特别是,如果您使用 ObjectOutputStream 以外的任何内容写入哪怕一个字节,对象流就会损坏。
如果要同时发送对象和文本,可以将 String 对象写入 ObjectOutputStream。您也可以直接调用ObjectOutputStream.writeUTF。
关于Java - 通过 ObjectOutputStream read/writeObject() 从服务器向客户端发送 ImageIcons 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581167/