Hello,
I have two programs:
a) Server.java : Using a JFileChooser, selects a file and then sends it to Client.
b) Client. java: Displays the file.
My problem:
I am able to send a file from server to client only once. The ObjectOutputStream.writeObject() does not work after that.
I later learnt that ObjectOutputStream.writeObject() needs a new object for every write operation. Also, I would need to reset(). Maybe I am writing/resetting it wrong, but I am am still able to send only the first image. I will be grateful for any help.
import java.net.*;
import java.io.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Server
{
private ServerSocket serverSocket;
private Socket socketOut;
private FileChoose f;
public Server(int port)
{
try
{
serverSocket = new ServerSocket(4444);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
try
{
socketOut = serverSocket.accept();
}
catch (IOException e)
{}
f = new FileChoose();
}
public static void main(String[] args)
{
Server s = new Server(4444);
}
private class FileChoose extends JFrame
{
private JButton openButton, openButton1, openButton2;
private String filelist = "";
private FileInputStream fis;
private ObjectOutputStream oos;
public FileChoose()
{
super("File Chooser Test Frame");
setLayout(new FlowLayout());
openButton = new JButton("Open");
openButton1 = new JButton("Send");
openButton2 = new JButton("Done");
openButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser chooser = new JFileChooser();
int option = chooser.showOpenDialog(FileChoose.this);
if (option == JFileChooser.APPROVE_OPTION)
{
File sf = chooser.getSelectedFile();
filelist = sf.getPath();
System.out.println (filelist);
}
}
}
);
openButton1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
sendFile();
}
}
);
openButton2.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
closeSockets();
}
}
);
try
{
oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
}
catch (IOException e)
{}
add(openButton);
add(openButton1);
add(openButton2);
setSize(350, 200);
setVisible(true);
}
public void sendFile()
{
try
{
fis = new FileInputStream(new String (filelist));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
System.out.println ("Finished Reading into Buffer");
oos.writeObject(buffer);
oos.reset();
System.out.println (buffer.length);
}
catch(FileNotFoundException e)
{}
catch(IOException e)
{}
}
public void closeSockets()
{
try
{
socketOut.close();
serverSocket.close();
}
catch(IOException e)
{}
}
}
}
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);
}
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
byte[] buffer = (byte[])ois.readObject();
Image image = Toolkit.getDefaultToolkit().createImage(buffer);
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
socket.close();
}
}