Keeping socket open to send files on timer calls?

Posted by user3704768 on Stack Overflow See other posts from Stack Overflow or by user3704768
Published on 2014-06-03T21:13:56Z Indexed on 2014/06/03 21:24 UTC
Read the original article Hit count: 132

Filed under:
|
|

I'm writing a program that requires an image to be fetched from a remote server every 10 milliseconds or so, as that's how often the image is updated. My current method calls a timer to grab the image, but it encounters Socket Closed errors all the time, and sometimes does not work at all.

How can I fix my methods to keep the socket open the whole time, so no reconnecting is needed?

Here is the full class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.Timer;

public class Connection {
    public static void createServer() throws IOException {
        Capture.getScreen();
        ServerSocket socket = null;
        try {
            socket = new ServerSocket(12345, 0,
                    InetAddress.getByName("127.0.0.1"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Server started on "
                + socket.getInetAddress().getHostAddress() + ":"
                + socket.getLocalPort() + ",\nWaiting for client to connect.");
        final Socket clientConnection = socket.accept();
        System.out.println("Client accepted from "
                + clientConnection.getInetAddress().getHostAddress()
                + ", sending file");
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Sending File");
                try {
                    pipeStreams(new FileInputStream(new File(
                            "captures/sCap.png")),
                            clientConnection.getOutputStream(), 1024);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        System.out.println("closing out connection");
        try {
            clientConnection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Timer timer = new Timer(10, taskPerformer);
        timer.setRepeats(true);
        timer.start();
    }

    public static void createClient() throws IOException {
        System.out.println("Connecting to server.");
        final Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(InetAddress
                    .getByName("127.0.0.1"), 12345));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {

        }
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Success, retreiving file.");
                try {
                    pipeStreams(socket.getInputStream(), new FileOutputStream(
                            new File("captures/rCap.png")), 1024);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {

                }
            }
        };

        System.out.println("Closing connection");
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Timer timer = new Timer(10, taskPerformer);
        timer.setRepeats(true);
        timer.start();
    }

    public static void pipeStreams(java.io.InputStream source,
            java.io.OutputStream destination, int bufferSize)
            throws IOException {
        byte[] buffer = new byte[bufferSize];
        int read = 0;
        while ((read = source.read(buffer)) != -1) {
            destination.write(buffer, 0, read);
        }
        destination.flush();
        destination.close();
        source.close();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets