0

I'm trying to revise my android code which can only send string originally. The task is to let it can transfer pictures. I know there are so many different ways to implement this. But I tried and really don't know why my pictures cant be sent, although there is no problem with the connecting. Is there anyone who can tell me? I will be really appreciate it. I'm just a beginner, Please forget my poor programming skill.

Here is the main part of server below

private Runnable socket_server = new Runnable(){
    public void run(){
        handler.post(new Runnable() {
            public void run() {
                test.setText("Listening...." + getMyIp());

            }
        });
        try{ 
            serverSocket = new ServerSocket(1234);

            while (true) {
                Socket client = serverSocket.accept();

                handler.post(new Runnable() {
                    public void run() {
                        test.setText("Connected.");
                    }
                });
                try {
                     File myFile = new File ("/sdcard/DCIM/img2.jpg");
                      byte [] mybytearray  = new byte [(int)myFile.length()];
                      FileInputStream fis = new FileInputStream(myFile);
                      BufferedInputStream bis = new BufferedInputStream(fis);
                      bis.read(mybytearray,0,mybytearray.length);
                      OutputStream os = client.getOutputStream();
                      os.write(mybytearray,0,mybytearray.length);
                      os.flush();
                      client.close();
                    test.setText("Received.");

                } catch (Exception e) {
                    handler.post(new Runnable() {
                        public void run() {
                            test.setText("Sending erro");
                        }
                    });
                }
            }
        }catch(IOException e){
            handler.post(new Runnable() {
                public void run() {
                    test.setText("Fail to buitl the socket");
                }
            });
        }
    }
};

and here is part of client

new Thread() 
            {
                @Override 
                public void run() { 
                    // TODO Auto-generated method stub 
                    InetAddress serverAddr = null;
                    SocketAddress sc_add = null;
                    Socket socket = null;

                    try 
                    { 
                        serverAddr = InetAddress.getByName("192.168.1.105");
                        sc_add= new InetSocketAddress(serverAddr,1234);
                        socket = new Socket();
                        socket.connect(sc_add,2000);

                        File myFile = new File("/sdcard/DCIM/img2.jpg");
                        InputStream fis = new FileInputStream("/sdcard/DCIM/img2.jpg");
                        OutputStream outputStream = socket.getOutputStream();

                        byte [] buffer = new byte[(int)myFile.length()];

                        int temp = 0 ;  
                        while((temp = fis.read(buffer)) != -1){  
                            outputStream.write(buffer, 0, temp);  
                        }  
                        outputStream.flush();
                        socket.close();


                } catch (UnknownHostException e) {

                    //TextView01.setText("InetAddress fail");

                } catch (SocketException e) {

                    //TextView01.setText("fail to develop socket");

                } catch(IOException e) {

                    //TextView01.setText("fail to sending");

                }
                } 

            }.start();
Do Re Mi
  • 21
  • 2
  • 5

1 Answers1

2

Here is your problem:

byte [] mybytearray  = new byte [(int)myFile.length()];
// ...
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = client.getOutputStream();
os.write(mybytearray,0,mybytearray.length); // <- This one.

I wrote this up once, here: Working unbuffered streams

The problem is, that this code assumes that the buffer is entirely full, which is not necessarily the case. The documentation for InputStream.read(byte[], int, int) states:

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer. […]

So, the buffer is not guaranteed to be full. This becomes a problem when we use the OutputStream.write(byte[], int, int)-method to write the read bytes to the output stream. It’s documentation reads:

Writes [exactly] len bytes from the specified byte array starting at offset off to this output stream. […]

Here is a working implementation:

byte buffer[] = new byte[1024];
int read_count = 0;
while ((read_count = input.read(buffer, 0, buffer.length)) != -1) {
    output.write(buffer, 0, read_count); // Now writes the correct amount of bytes
}

Also, the buffer should not be as big as the whole file, since transferring the file in chunks is more efficient. See this older question for an in-detail explanation: How do you determine the ideal buffer size when using FileInputStream?

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • Really thank you for the clear answer. But I tried the way you showed, seems there still some problem with my program... – Do Re Mi Aug 18 '13 at 06:12