2

I'm creating an application which I have to send multiple images one after another and I use Object(Output/Input)Stream inside of a while loop. But It only send one file I guess its the first image. Can I send multiple files with Object(Output/Input)Stream? If so, What's the issue with my code?

Server : (Sends Screenshots from PC )

package application;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

import javax.imageio.ImageIO;

public class ScreenCapture {

    Socket socket;
    ObjectOutputStream os;
    Robot robot;
    PrintStream ps;

    public ScreenCapture() throws IOException, AWTException {
        // TODO Auto-generated constructor stub
        socket = SocketWrapper.getSocket();
        os = new ObjectOutputStream(socket.getOutputStream());
        robot = new Robot();
        ps = new PrintStream(socket.getOutputStream());
        new Record().start();

    }

    private class Record extends Thread{
        @Override
        public void run() {
            while(true){
                getScreenShot();
                FileInputStream fis;
                try {
                    File f = new File("/Users/Tomahawk/Documents/send.jpg");
                    fis = new FileInputStream(f);
                    byte[] byteArray = new byte[fis.available()];

                    fis.read(byteArray);

                    os.writeObject(byteArray);
                    os.flush();

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                System.out.println("Sent File");
            }
        }
    }

    public void getScreenShot(){
        Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage img = robot.createScreenCapture(rect);

        try {
            ImageIO.write(img, "jpg", new File("/Users/Tomahawk/Documents/send.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Client : (Receives Screenshots and save It on the phone)

package com.pcontrol.tomahawk.pcontrol;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ScreenCapture extends Activity {

    Socket socket;
    ObjectInputStream is;
    OutputStream os;
    Scanner scanner;
    ImageView screenCap;
    int filesize = 0;
    int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_capture);

        socket = SocketWrapper.getSocket();


        screenCap = (ImageView) findViewById(R.id.screenCap);

        new ReceiveFiles().execute();

    }

    private class ReceiveFiles extends AsyncTask<Void,Void,Void> {
        @Override
        protected Void doInBackground(Void... params) {
            while(true) {
                try {
                    is = new ObjectInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    os = new FileOutputStream("/sdcard/"+i+".jpg");

                    byte[] sentArray = (byte[]) is.readObject();

                    os.write(sentArray);
                    os.flush();
                    publishProgress();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }
        @Override
        protected void onProgressUpdate(Void... values) {
            Bitmap bmp = BitmapFactory.decodeFile("/sdcard/"+i+".jpg");
            screenCap.setImageBitmap(bmp);
            i++;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_screen_capture, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Tomahawk
  • 78
  • 11
  • Yes what is the issue with your code? Any problems? What did you try to tell? – greenapps Aug 23 '15 at 20:08
  • Probable duplicate of http://stackoverflow.com/q/10367698/207421. You're trying to use both an `ObjectOutputStream` and a `PrintWriter` on the same socket. It won't work. Drop them both and use a `DataOutputStream,` and a `DataInputStream` for receiving. You don't need an `ObjectOutputStream` for sending files. No visible question here. – user207421 Aug 23 '15 at 20:41

1 Answers1

0

That endless loop is not so endless -- in fact, it will not even loop once.

You have a "return null" at the end of your "while(true)" block. After the first iteration, your method will return.

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
  • No worries. BTW, you could've found this by stepping through your loop with the debugger. Which is generally a very useful thing to do, but many people don't -- I myself went a very long time before I really realized how useful it was. – Snild Dolkow Aug 24 '15 at 05:12