Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.frozen.rpicameraviewer.classes;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import ca.frozen.library.classes.Log;
- public class TcpIpReader
- {
- // public constants
- public final static int IO_TIMEOUT = 1000;
- // local constants
- private final static int CONNECT_TIMEOUT = 5000;
- // instance variables
- private Socket socket = null;
- private InputStream inputStream = null;
- public TcpIpReader(Camera camera)
- {
- try
- {
- socket = getConnection(camera.address, camera.port, CONNECT_TIMEOUT);
- socket.setSoTimeout(IO_TIMEOUT);
- inputStream = socket.getInputStream();
- }
- catch (Exception ex) {}
- }
- public int read(byte[] buffer)
- {
- try
- {
- return (inputStream != null) ? inputStream.read(buffer) : 0;
- }
- catch (IOException ex)
- {
- return 0;
- }
- }
- public boolean isConnected()
- {
- return (socket != null) && socket.isConnected();
- }
- public void close()
- {
- if (inputStream != null)
- {
- try
- {
- inputStream.close();
- }
- catch (Exception ex) {}
- inputStream = null;
- }
- if (socket != null)
- {
- try
- {
- socket.close();
- }
- catch (Exception ex) {}
- socket = null;
- }
- }
- public static Socket getConnection(String baseAddress, int port, int timeout)
- {
- Socket socket;
- try
- {
- socket = new Socket();
- InetSocketAddress socketAddress = new InetSocketAddress(baseAddress, port);
- socket.connect(socketAddress, timeout);
- }
- catch (Exception ex)
- {
- Log.info("TcpIp getConnection: " + ex.toString());
- socket = null;
- }
- return socket;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement