I am trying to build a client-server Program in which the server sends the image to client and client displays the received image in JFrame. I am converting Image on the server-side to ImageIcon object and sending it to the client via ObjecOutputStream. But it fails and gives me failed to load image contents error on server-side, it happens while the ObjectOutputStream.writeObject() method is called.
Server-side code
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ImageServer{
private static ServerSocket serverSocket;
private static final int PORT = 1234;
public static void main(String[] args){
System.out.println("Opening port…\n");
try{
serverSocket = new ServerSocket(PORT);
}
catch(IOException ioEx){
System.out.println("Unable to attach to port!");
System.exit(1);
}
while(true){
try{
Socket connection = serverSocket.accept();
ObjectOutputStream outStream =new ObjectOutputStream(
connection.getOutputStream());
ImageIcon icon=new ImageIcon("//Give image path//");
System.out.println(icon.getImageLoadStatus());// To check if image is loded correctly or not.
outStream.writeObject(icon);
outStream.flush();
}
catch(IOException ioEx){
ioEx.printStackTrace();
}
}
}
}
Just give the path for an image file, for testing purposes.
Client-side code
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageClient extends JFrame{
private InetAddress host;
private final int PORT = 1234;
private ImageIcon image;
public static void main(String[] args){
ImageClient pictureFrame = new ImageClient();
pictureFrame.setSize(340,315);
pictureFrame.setVisible(true);
pictureFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public ImageClient(){
try{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx){
System.out.println("Host ID not found!");
System.exit(1);
}
try{
Socket connection = new Socket(host,PORT);
ObjectInputStream inStream =new ObjectInputStream(
connection.getInputStream());
image = (ImageIcon)inStream.readObject();
connection.close();
}
catch(IOException ioEx){
ioEx.printStackTrace();
}
catch(ClassNotFoundException cnfEx){
cnfEx.printStackTrace();
}
repaint();
}
public void paint(Graphics g){
image.paintIcon(this,g,0,0);
}
}
Error generated on Server-side
java.io.IOException: failed to load image contents
at javax.swing.ImageIcon.writeObject(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.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at ImageServer.main(ImageServer.java:27)
This is a code excerpt from the following book: http://www.springer.com/us/book/9781447152538