简体   繁体   中英

Sending files from Android client to c# server, breaks before reading whole file

c# code

 private MemoryStream Read() {
        NetworkStream clientStream = _client.GetStream();
        MemoryStream messageStream = new MemoryStream();

    var inbuffer = new byte[65535];

    if(clientStream.CanRead) {
        do {
            int bytesRead = clientStream.Read(inbuffer,0,inbuffer.Length);
            messageStream.Write(inbuffer, 0, bytesRead);
            messageStream.Flush();
            Debug.Print(messageStream.Length + " added " + bytesRead);
        } while(clientStream.DataAvailable); // GOES BEYOND HERE, EVEN THOUGH THERE'S MORE DATA
    }

    messageStream.Position = 0;
    return messageStream;
}

android code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                if(resultCode == RESULT_OK){                   
                        if(requestCode == ATTACH_FILE_REQUEST){
                                 writeToSocket("<FILE>generic.jpeg");
                                 InputStream input = null;
                                 String filePath = data.getData().getPath().toString();
                                 try {
                                        input = getContentResolver().openInputStream(data.getData());                  
                                        BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
                                        byte[] bytes = new byte[65535];
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                                while((bufferedInputStream.read(bytes)) != -1){
                                                         byteArray.write(bytes, 0, bytes.length);                                
                                                 }
                                                socketNetworkHelper.writeFile(byteArray.toByteArray());
                                } catch (FileNotFoundException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                        Toast.makeText(this, "Error in file selection", Toast.LENGTH_LONG).show();
                                } catch (IOException e2){
                                        e2.printStackTrace();
                                        Toast.makeText(this, "Input/Output Error", Toast.LENGTH_LONG).show();
                                }        
                        }
                }
}

In SocketNetworkHelper.java which has a socket connected:

public void writeFile(byte[] buffer) {

                try {
                        socket.getOutputStream().write(buffer);

                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

The question is: How can I get the whole file?

You could also send the filesize as a parameter

private MemoryStream Read(int filesize) {
            NetworkStream clientStream = _client.GetStream();
            MemoryStream messageStream = new MemoryStream();
            var inbuffer = new byte[filesize];

            if (clientStream.CanRead) {
                int totalBytesRead = 0;
                do {
                    int bytesRead = clientStream.Read(inbuffer, 0, inbuffer.Length);
                    totalBytesRead += bytesRead;
                    messageStream.Write(inbuffer, 0, bytesRead);
                    messageStream.Flush();
                } while (totalBytesRead < filesize);

            }
            messageStream.Position = 0;
            return messageStream;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM