Results 1 to 2 of 2
  1. #1
    devett is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default Server Sockets sending images from URL

    I have a camera that monitors outdoor conditions and need to stream that image to a client. I currently use JBoss but wanted to try sockets. I can get one image from the server but I need the images to stream through as long as the client is opened.

    Server code:
    package server;

    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URL;

    import javax.imageio.ImageIO;
    import common.ByteArrayConversion;

    public class Image_Server {

    private static final int port = 6666;
    private static final String ONESHOTNAME = "oneshotimage.jpg";
    private static byte[] byteImage;

    /**
    * @param args
    * @param ONESHOTNAME
    * @throws IOException
    */
    public static void main(String[] args) throws IOException {

    ServerSocket server = null;

    try {
    server = new ServerSocket(port);
    System.out.println("Server socket ready on port: " + port);
    } catch (IOException e) {
    System.err.println("Could not listen on port: " + port);
    System.exit(-1);
    }

    Socket socket = server.accept();

    /*
    * The following gets the latest image from the camera.*
    */
    URL url = new URL("http://camera3:8080/" + ONESHOTNAME);
    BufferedImage bufferedImage = ImageIO.read(url);
    byteImage = ByteArrayConversion.toByteArray(bufferedImage);

    System.out.println(byteImage.toString());

    OutputStream os = socket.getOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(byteImage);
    }
    }

    client:
    package client;

    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import common.ByteArrayConversion;

    public class Image_client {
    private static Socket socket;
    private static String host = "devett";

    public static void main(String[] args) {

    try {
    socket = new Socket(host, 6666);
    System.out.println("Connection to host established.");
    } catch (UnknownHostException e) {
    System.err.println("Don't know about host: " + host);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    try {
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    byte[] byteImage = (byte[])ois.readObject();
    BufferedImage bufferedImage = ByteArrayConversion.fromByteArray(byteImage);
    System.out.println(bufferedImage.toString());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

  2. #2
    Mrc0d3r's Avatar
    Mrc0d3r is offline Member
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Sending linked lists via sockets
    By deepthought015 in forum Networking
    Replies: 1
    Last Post: 04-26-2009, 03:33 AM
  2. Replies: 6
    Last Post: 12-29-2008, 04:14 AM
  3. Sending files over sockets!
    By rameshraj in forum Networking
    Replies: 2
    Last Post: 05-30-2008, 11:18 PM
  4. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:05 PM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •