KABINGO!
I quickly made a client server and sort of put your code in the while true:
Server
package ImageSending;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
*
* @author Wim
*/
public class Server {
private ServerSocket socket;
private int number;
public Server() throws Exception {
socket = new ServerSocket(8090);
listen();
}
private void listen() throws Exception {
Socket client = socket.accept();
ObjectInputStream oin = new ObjectInputStream(client.getInputStream());;
while (true) {
ImageIcon img = (ImageIcon) oin.readObject();
System.out.println(" Receive.");
ImageIO.write(
new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB),
"JPG",
new File("image" + number++ + ".jpg"));
}
}
public static void main(String[] args) throws Exception{
Server server = new Server();
}
}
Client:
package ImageSending;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;
/**
*
* @author Wim
*/
public class Client {
private Socket server;
private Robot robot;
private ObjectOutputStream oout;
public Client() throws Exception {
robot = new Robot();
server = new Socket("127.0.0.1", 8090);
oout = new ObjectOutputStream(server.getOutputStream());
run();
}
private void run() throws Exception{
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle scrRect = new Rectangle(screenDim);
BufferedImage screenshot;
ImageIcon sendImage;
while (true) {
screenshot = robot.createScreenCapture(scrRect);
sendImage = new ImageIcon(screenshot);
oout.writeObject(sendImage);
System.out.println("Send.");
oout.flush();
Thread.sleep(1000);
oout.reset();
}
}
public static void main(String[] args) throws Exception{
Client client = new Client();
}
}
(Ye, i just put throws Exception everywhere for readability. You may want to
NOT do that :P)
Anyways, I luckily had the same problem as you.
Quote
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
at java.awt.image.Raster.createPackedRaster(Raster.java:458)
That was quite interesting because, DataBufferInt and createPackedRaster are parts of the socket connection.
Then, when i read about the methods of the ObjectInputStream at the server side it has this reset() method:
Quote
Repositions this stream to the position at the time the mark method was last called on this input stream
Which gave me the feeling that everything on the stream was read.. but it stayed on the stream, it just doesn't start reading from the beginning next time.
So I started looking for a way to clear the things i've read from the stream. But the ObjectInputStream didn't seem to have such a method.
I swapped over to the client code and looked at what the ObjectOutputStream could do.
And there I checked what the reset() method did:
Quote
Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.
So I tried adding that.. and it works :)
package ImageSending;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;
/**
*
* @author Wim
*/
public class Client {
private Socket server;
private Robot robot;
private ObjectOutputStream oout;
public Client() throws Exception {
robot = new Robot();
server = new Socket("127.0.0.1", 8090);
oout = new ObjectOutputStream(server.getOutputStream());
run();
}
private void run() throws Exception{
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle scrRect = new Rectangle(screenDim);
BufferedImage screenshot;
ImageIcon sendImage;
while (true) {
screenshot = robot.createScreenCapture(scrRect);
sendImage = new ImageIcon(screenshot);
oout.writeObject(sendImage);
System.out.println("Send.");
oout.flush();
Thread.sleep(1000);
[B][SIZE="4"] oout.reset();[/SIZE][/B]
}
}
public static void main(String[] args) throws Exception{
Client client = new Client();
}
}
I also flush, but i don't think that's needed.
Anyways, the reset() method also says
Quote
Throws: IOException - if reset() is invoked while serializing an object.
So when it's busy writing something on the stream it will crash. That's why i put flush there because, I think, it forces to start sending right now.
Because there is a Thread.sleep(1000) there, it's pretty safe to reset after that time.
I tested and aborted MYSELF after 50 full screen images.
(Oh ye, my images are all black. I hope that's working fine at your side :D)