Results 1 to 2 of 2
- 06-03-2011, 09:47 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
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();
}
}
}
- 06-07-2011, 07:25 AM #2
Similar Threads
-
Sending linked lists via sockets
By deepthought015 in forum NetworkingReplies: 1Last Post: 04-26-2009, 02:33 AM -
Sending an object via sockets - all fields but array updating
By Wassa in forum NetworkingReplies: 6Last Post: 12-29-2008, 03:14 AM -
Sending files over sockets!
By rameshraj in forum NetworkingReplies: 2Last Post: 05-30-2008, 10:18 PM -
Sending Mail Using Sockets
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:05 PM -
Problems sending file throught TCP sockets
By Nite in forum Advanced JavaReplies: 2Last Post: 08-04-2007, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks