0

I'm trying to send a file from my PC to my Android Phone via the LAN.

Client (sender, PC):

public class Client {

    public static void main(String[] args) {

    String host = "192.168.0.108"; //IP of phone (I know, hardcoding is evil...)
    int port = 19999;
    String TimeStamp;
    Socket con;
    File f = new File("/home/sebastian/test");

    try {
        InetAddress address = InetAddress.getByName(host);
        con = new Socket(address, port);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }    

    Thread t = new Thread(new Runnable() {

    @Override
        public void run() {
        try {
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(f));
            BufferedOutputStream bos = new BufferedOutputStream(con.getOutputStream());
            int i;
            System.out.println("Starting transmission");
            while ((i = fis.read()) != -1) {
                bos.write(i);
            }
            System.out.println("Finished");
            bos.flush();
            fis.close();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }}

    });
    t.start();
    }
}

Server (Receiver, phone):

class Server extends Activity {

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

    private void startserver() {
    try {
        socketl = new ServerSocket(port, 0, InetAddress.getByName("192.168.0.108"));
        Thread t = new Thread (new Runnable() {

            @SuppressLint("SdCardPath")
            @Override
            public void run() {
                while (true) {
                    try {
                        connection = socketl.accept();
                        BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
                            InputStreamReader isr = new InputStreamReader(is);
                            instr = new StringBuffer();
                            int i;
                            File f = new File("/sdcard/jaisb/streamtest");
                            f.delete();
                            f.createNewFile();
                            Log.d("log", "file created");
                            FileOutputStream fos = new FileOutputStream(f);
                            while ((i = isr.read()) != -1) {
                                fos.write(i);
                        }
                        Log.d("log", "file written");
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

        });
    t.start();
    }

}

If I make a textfile called test in my homedir, it wokrs fine, the file arrives without errors... However, if I replace the text by other media (namely picures and music (I've tried JPG and MP3)), the files are corrupted.

Comparing them with the original in hexedit, i noticed that when sending the music file, the problem was that some bytes got lost; the first few bytes of the file were OK, but then it started losing bytes...

When sending a JPG, most bytes were wrong. Even the first four bytes changed from FF D8 FF E0 to FD FD FD FD.

What am I doing wrong? How can I send non-text files via the local network?

s3lph
  • 4,515
  • 4
  • 20
  • 37

0 Answers0