Hello Members,

I have a simple Client/Server program which does the following:

Server sends an array of Images one at a time to the Client. Client displays them one at a time.

My Server:

import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server {
    public static void main(String[] args) throws IOException{

        ServerSocket serverSocket = null;
     

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
       Socket socketOut = serverSocket.accept();
       ObjectOutputStream oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
       String imageFile[] = new String [6];
      
       imageFile[1] = "dice1.gif";
       imageFile[2] = "dice2.gif";
       imageFile[3] = "dice3.gif";
       imageFile[4] = "dice4.gif";
       imageFile[5] = "dice5.gif";
       
       for (int i=1; i<=5; i++)
       {
       String p = JOptionPane.showInputDialog("Enter the next image number");
       
       FileInputStream fis = new FileInputStream( imageFile[i]);
       byte[] buffer = new byte[fis.available()];
       fis.read(buffer);
       
       oos.writeObject(buffer);
       }
       
        socketOut.close();
        serverSocket.close();
    }
}

My Client:

import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;

public class Client
{
     private static Socket socket  = null;
        
    public static void main (String args[]) throws IOException, ClassNotFoundException
    {

       
         try {
            socket = new Socket("localhost", 4444);
             } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
            System.exit(1);
        }
        JFrame frame = new JFrame();
        JLabel label;
        
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        byte[] buffer = (byte[])ois.readObject();
        Image image = Toolkit.getDefaultToolkit().createImage(buffer); 

        label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
        
        socket.close();

}
}

My Problem:
The first two images that the Server sends is received and displayed at the Client side accurately. After that, I get the following exception:

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1838)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1876)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1537)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:329)
at Server.main(Server.java:34)

I would be grateful for any help!!

Thank you!!
sciprog1

Hello Members,

Could someone possibly reply?

Thank you!!

Why does the client only read a single image whereas the server writes out 5 images? Why doesn't the server run in a headless mode i.e. why doesn't it accept the image number from the client or via a configuration rather than popping up a box?

The error probably means that the client closed the connection to the server hence writing out to the "client stream" gives an exception...

Hello s.o.s,

Thanks for the reply! That was very silly on my part. It works well now!!

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.