streaming my screen with another client with java (code) - java

I've written a code to stream my screen with another person like skype or teamviewer so the program is working but just for 10 15 seconds and after that a white screen spotted with black points occurs here is the video of my application : Video testing my application
Server Code :
public void run()
{
boolean ok=true;
try
{
Socket s=Serveur.accept();
ObjectInputStream lire_msg=new ObjectInputStream(s.getInputStream());
ObjectOutputStream envoyer_msg=new ObjectOutputStream(s.getOutputStream());
Rectangle rec=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot r= new Robot();
BufferedImage img;
while (ok)
{
img = r.createScreenCapture(rec);
//s.setSendBufferSize(65536);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img.flush();
if(ImageIO.write(img, "png",baos))
{
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
envoyer_msg.write(imageInByte);
envoyer_msg.flush();
sleep(10);
}
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
Client Code:
public void run()
{
try
{
Image img=null;BufferedImage bimg;
while(true)
{
try
{
bimg=ImageIO.read(lire_msg);
img=SwingFXUtils.toFXImage(bimg, null);
}
}catch(Exception e){}
imgv.setImage(img);
envoyer_msg.flush();
sleep(10);
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

Related

App crashed when sending images through TCP

I'm creating a simple stream to send images taken from client's screen from client to server. For now I can receive the first image but then the app crashed unexpectedly. The idea is send the size and the image in byte array, the server receive that byte array and convert to image.
FromClient:
public void run() {
image = new BufferedImage(NORM_PRIORITY, MIN_PRIORITY, MAX_PRIORITY);
while(continueLoop) {
//send captured screen
image = robot.createScreenCapture(rectangle);
try {
//Initiate the stream
OutputStream out = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
// store the size of each image
byte[] size = ByteBuffer.allocate(4).putInt(baos.size()).array();
dos.write(size);
dos.write(baos.toByteArray(), 0, baos.toByteArray().length);
dos.flush();
} catch(IOException e) {
e.printStackTrace();
e.printStackTrace();
continueLoop = false;
}
try {
Thread.sleep(30);
} catch(InterruptedException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
ToServer:
public void run() {
boolean continueLoop = true;
try {
drawGUI();
// Initiate the stream
InputStream is = clientSocket.getInputStream();
DataInputStream configGraphicStream = new DataInputStream(is);
BufferedImage image = null;
while(continueLoop) {
//receive the size of image and convert to type int
byte[] sizeInByte = new byte[64];
configGraphicStream.read(sizeInByte);
int length = ByteBuffer.wrap(sizeInByte).asIntBuffer().get();
try {
// Get images
byte[] img = new byte[length];
configGraphicStream.readFully(img, 0, img.length);
image = ImageIO.read(new ByteArrayInputStream(img));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
//draw images
if( image != null)
{
Graphics graphics = clientPanel.getGraphics();
graphics.drawImage(image, 0, 0, clientPanel.getWidth(), clientPanel.getHeight(), clientPanel);
}
System.out.println("Receiving image");
Thread.sleep(30);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Please help me solve this problem.

for loop doesn't receive full data

I got the problem that my program is stuck in the for loop because the dataInputSteam doesn't receive all data before the DataOutputSteam is finished.
In my program I want to send a secreenshot with the server and the client should receive it:
Server:
public sendScreen(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
Robot robot;
try {
robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screen = new Rectangle( screenSize );
BufferedImage image = robot.createScreenCapture( screen );
BufferedImage scaledImage = Scalr.resize(image, 300);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] screenBytes = baos.toByteArray();
daos = new DataOutputStream(socket.getOutputStream());
daos.writeInt(screenBytes.length);
daos.write(screenBytes);
System.out.println("Screen sent");
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And the client:
public static class GetScreenshot implements Runnable{
Socket socket;
private static DataInputStream din;
private static BufferedImage screenshot;
public GetScreenshot(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
System.out.println("start method getScreenshot");
int length;
byte[] buffer;
PrintWriter out;
try {
//sending command to send screenshot
out = new PrintWriter(socket.getOutputStream(), true);
out.println("GETSCREENSHOT");
din = new DataInputStream(socket.getInputStream());
System.out.println("DIS created");
length = din.readInt();
System.out.println("Got data from DIS");
buffer = new byte[length];
System.out.println("Filled buffer");
for(int i = 0; i < length; i++){
buffer[i] = (byte) din.read();
System.out.println("read" + i+ "while length is " + length + " read data " + buffer);
}
System.out.println("got buffer");
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
screenshot = ImageIO.read(bais);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//writing screenshot to local disk
File outputfile = new File("C:\\users\\XXXX\\documents\\image2.png");
try {
ImageIO.write(screenshot, "png", outputfile);
System.out.println("image written to local disk");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//set screenshot in the tool
Main.labelScreenshot.setIcon(new ImageIcon(screenshot));
}
}
Does anybody know how i can transfer all of the data of the screenshot?
Greetings
Max
I think your problem is because your view (canvas/label/etc) width & height is smaller than your image. Try to resize the screenshoot so the width & height is same as your view (canvas/label/etc).
buffer = new byte[length];
System.out.println("Filled buffer");
At this point this message is simply untrue. You have created the buffer, but you certainly haven't filled it.
for(int i = 0; i < length; i++){
buffer[i] = (byte) din.read();
System.out.println("read" + i+ "while length is " + length + " read data " + buffer);
}
All this is equivalent to:
din.readFully(buffer);
And then:
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
screenshot = ImageIO.read(bais);
//writing screenshot to local disk
File outputfile = new File("C:\\users\\XXXX\\documents\\image2.png");
try {
ImageIO.write(screenshot, "png", outputfile);
All this is entirely equivalent to:
try (new FileOutputStream out = new FileOutputStream("C:\\users\\XXXX\\documents\\image2.png"))
{
out.write(buffer);
}
There is no need to decode and re-encode the image.
You add this to the sender part:
socket.flush();
if not worked, then:
socket.shutdownOutput();

Getting noise while tryin to stream song to android over tcp

I've made some java code which connects and sends data to my android device over LAN, at the receiver side, the app uses AudioTrack to play the audio.
It seems to be streaming the audio, but the receiver plays back only noise!
Here is my sender code:
MainActivity has the following function -
public void startStream() {
//initiate the audioTrack and play it, then start listening
try {
int minBufferSize = AudioTrack.getMinBufferSize(44100,AudioFormat.CHANNEL_OUT_STEREO,AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
audioTrack.play();
Streamer streamer = new Streamer(MainActivity.this);
streamer.execute();
}catch(Exception e) {
say("mainSuperException");
}
}
This is the Streamer class:
public class Streamer extends AsyncTask<Void, Void, Void> {
MainActivity mainActivity = null;
int i = 0;
Streamer(MainActivity m)
{
mainActivity = m;
}
protected Void doInBackground(Void... p) {
try {
byte[] buffer = new byte[1024];
ServerSocket serverSocket = new ServerSocket(3210);
Socket socket = serverSocket.accept();
DataInputStream input = new DataInputStream(socket.getInputStream());
i = 0;
byte[] size = new byte[4];
input.read(size,0,4);
int s = ByteBuffer.wrap(size).getInt();
//Start streaming the file
while (i<s) {
try {
input.read(buffer,0,1024);
++i;
mainActivity.audioTrack.write(buffer, 0, buffer.length);
Log.d("packetTag", "got "+i);
} catch (IOException e) {
e.printStackTrace();
mainActivity.say("InnerioException");
} catch (IllegalStateException e) {
Log.e("packetTag", e.toString());
mainActivity.say("InnerIllegalStateException");
} catch (Exception e) {
Log.e("InnerSuperException", e.toString());
e.printStackTrace();
mainActivity.say("InnerSuperException");
}
}
} catch (IOException io) {
Log.e("ioexception", io.toString());
mainActivity.say("OuterioException");
} catch (Exception e) {
Log.e("SuperException", e.toString());
e.printStackTrace();
mainActivity.say("OuterSuperException");
}
return null;
}
}
And for the sender I used the following Java code:
public class mainClassSend {
public static void main(String[] s)
{
try {
InputStream songInputStream = new FileInputStream("D:\\song.mp3");
byte[] buffer = new byte[1024];
InetAddress group = InetAddress.getByName("192.168.2.8");
Socket socket = new Socket(group,3210);
int i=0;
byte[] size = ByteBuffer.allocate(4).putInt(3359).array();
socket.getOutputStream().write(size);
//Start streaming the file
while ((songInputStream.read(buffer, 0, buffer.length)) > -1) {
try {
socket.getOutputStream().write(buffer, 0, buffer.length);
++i;
System.out.println(i);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
songInputStream.close();
socket.close();
System.out.println(i);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Right now I'm just desperately trying to get this to work, so I've filled in all the values explicitly, even figured out the exact number of frames it sends, and sent that as the size.
It receives the size correctly, and keeps looping till that value, but all I hear is static.
I've looked all over stackOverflow, and tried looking into a lot of java class documentations, can't seem to figure out whats wrong here.

Sending an image through socket as byte array in Java

I know this has been asked in a few different ways, but I've been working on this for 2 days with no avail. My code is failing in that the receiving side throws EOF exceptions constantly. Can someone point me in the right direction?
Receiving side:
class ReceiveThread extends Thread {
ReceiveThread() {
}
#Override
public void run() {
System.out.println("Receive Thread Start");
DataInputStream in;
try {
} catch (Exception e) {
return;
}
try {
in = new DataInputStream(connection.getInputStream());
while (true) {
if (!connection.isConnected()) {
System.out.println("Connection not connected");
break;
}
try {
int len = in.readInt();
byte[] data = new byte[len];
System.out.println("Image size: " + len);
if (len > 0) {
in.readFully(data, 0, len);
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(data));
panel.updateImage(bi);
panel.repaint();
}
in.close();
} catch (Exception e) {
}
pause(100);
}
} catch (Exception e) {
}
}
public void pause(long time) {
try {
Thread.sleep(time);
} catch (Exception e) {
}
}
}
Sending side:
class UpdateScreenThread extends Thread {
Robot robot;
public UpdateScreenThread() {
try {
robot = new Robot();
System.out.println("Update Thread Created");
} catch (Exception e) {
}
}
#Override
public void run() {
System.out.println("Update Thread Running");
Settings.isSharing = true;
Dimension screenSize;
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out;
try {
out = new DataOutputStream(s.getOutputStream());
} catch (Exception e) {
return;
}
while (s.isConnected()) {
//System.out.println("test");
BufferedImage bi = robot.createScreenCapture(screenRectangle);
try {
ImageIO.write(bi, "PNG", baos);
baos.flush();
byte[] bytes = baos.toByteArray();
out.flush();
out.writeInt(bytes.length);
out.flush();
out.write(bytes);
System.out.println("Image sent");
} catch (Exception e) {
}
pause(500);
}
try {
out.close();
} catch (Exception e) {
}
Settings.isSharing = false;
}
}
Thanks to anyone who can help. This is driving me INSANE.
Reduced to the essentials, this is your read loop:
public void run() {
//...
try {
in = new DataInputStream(connection.getInputStream());
while (true) {
//...
try {
int len = in.readInt();
byte[] data = new byte[len];
in.readFully(data, 0, len);
//...
in.close();
} catch (Exception e) {
}
pause(100);
}
} catch (Exception e) {
}
}
Note that the while (true) {...} includes in.close(). Move the close out of the loop.

indexoutofboundsexception by sending pictures over Socket

I want to make a little programm that makes a live-stream for Desktop.
It should be so that you send pictures to an echo-server and he response it to the clients.
There you get be draw the Images. Side by Side. And so it is like a movie(or something like that).
But I always get an indexoutofboundsexception. Where is the error or how can I improve my program.
The ImageIO.write lines thows the Error
//Client Main
public class Main {
public static void main(String[] args) {
Frame frm = new Frame();
Frame.Client client;
frm.setLayout(null);
frm.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
frm.setResizable(false);
frm.setSize(1600,900);
frm.setVisible(true);
}
}
// get and send the Desktopimage
public class desktopCapture {
Robot robo;
BufferedImage screenImage;
Rectangle bounding;
public desktopCapture() {
try {
bounding = new Rectangle(0,0,1600,900);
robo = new Robot();
} catch (AWTException e) {e.printStackTrace();}
}
public void sendScreenCapture(Socket client) {
screenImage = robo.createScreenCapture(bounding);
try {
ImageIO.write(screenImage, "png", client.getOutputStream());
} catch (IOException e) {e.printStackTrace();}
}
}
// in Frame two function for actionListener Objects, so I can say who streams his Desktop and which get only the Images to.
public void readImage() {
while(true) {
try {
while((screenImage = ImageIO.read(in)) != null){
repaintScreen();
}
} catch (IOException e) {e.printStackTrace();}
}
}
public void sendImage() {
try {
while(true){
dC.sendScreenCapture(client);
System.out.println("read1");
while((screenImage = ImageIO.read(in)) != null){
System.out.println("read2");
ImageIO.write(screenImage, "png", new File("image1.png"));
Thread.sleep(250);
}
repaintScreen();
screenImage = null;
}
} catch (IOException | InterruptedException e) {e.printStackTrace();}
}
}
}
//Thread for a Client
public class handler implements Runnable {
Socket client;
OutputStream out;
InputStream in;
PrintWriter writer;
BufferedImage image;
public handler(Socket client) {
this.client = client;
}
#Override
public void run() {
try {
in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while(true) {
System.out.println("write1");
while((image = ImageIO.read(in)) != null){
System.out.println("write2");
for(int i = 1;i <= Server.connectionArray.size();i++){
Socket TEMP_SOCK = (Socket)Server.connectionArray.get(i-1);
out = TEMP_SOCK.getOutputStream();
writer = new PrintWriter(out);
ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
System.out.println("write3");
}
image = null;
}
}
} catch (IOException e) {e.printStackTrace();}
}
}
I would change your for loop to:
int count = Server.connectionArray.size()
int index = count - 1;
for(int i = 0;i < count; i++){
Socket TEMP_SOCK = (Socket)Server.connectionArray.get(index);
out = TEMP_SOCK.getOutputStream();
writer = new PrintWriter(out);
ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
System.out.println("write3");
}

Categories