简体   繁体   中英

Sending strings and File from Android Client to C# server

I am trying to send files with a bunch of strings from Android client to C# server. The strings, among other things, will also contain details with regards to the file being sent eg: File Size, File Name, etc. The issue I am having is that all the file bytes is not received thus the original file cannot be reconstructed at the destination even though I can properly retrieve all the strings.

My C# Server Code

                        while (true)
        {
            Socket socket = listener.AcceptSocket();
            setStatus(socket.RemoteEndPoint + " Connected");

            try
            {
                // Open the stream
                NetworkStream stream = new NetworkStream(socket);
                System.IO.StreamReader sr = new StreamReader(stream);

                //Get string data
                //********************************************
                Teststr = sr.ReadLine();
                setStatus(Teststr);


                FileSize = sr.ReadLine();
                long fileSizeLong = Convert.ToInt64(FileSize);
                int length = (int)fileSizeLong;
                setStatus("File size: " + length + " bytes");
                //********************************************


                //read bytes to buffer
                byte[] buffer = new byte[length];

                int toRead = (int)length;
                int read = 0;
                while (toRead > 0)
                {
                    int noChars = stream.Read(buffer, read, toRead);
                    read += noChars;
                    toRead -= noChars;
                }

                setStatus("File Recieved. Total bytes: " + Convert.ToString(buffer.Length));
                setStatus("Saving File");
                String recievedPath = "C:\\Test\\";
                BinaryWriter bWrite = new BinaryWriter(File.Open(recievedPath + "Test.png", FileMode.Create));
                bWrite.Write(buffer);
                setStatus("File Saved to: " + recievedPath);
                bWrite.Flush();
                bWrite.Close();
                stream.Flush();
                stream.Close();
            }
            catch (Exception e)
            {
                setStatus(e.Message);
            }

            setStatus("Disconnected");
            socket.Close();
        }

My Android Client Code

                    File file = new File(configstr[2]); //create file instance
            try {

             client = new Socket(configstr[0], Integer.valueOf(configstr[1]));

             //Read file
             fileInputStream = new FileInputStream(file);

             outputStream = client.getOutputStream();

             //Output database details to stream
             //*****************************************

             //Holds the string before conversion to bytes
             String text = "";

             text = "Test\n";
             outputStream.write(text.getBytes());

             text = String.valueOf(file.length()) + "\n";
             outputStream.write(text.getBytes());
             //*****************************************

             outputStream.flush();

             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             byte[] b = new byte[1024];
             int bytesRead = 0;
             while ((bytesRead = fileInputStream.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
             }
             byte[] bytes = bos.toByteArray();
             outputStream.write(bytes);

             outputStream.flush();
              return true;
            } catch (UnknownHostException e) {
             e.printStackTrace();
             return false;
            } catch (IOException e) {
             e.printStackTrace();
             return false;
            }

NOTE: The code works well if I only send one string, eg: If I only send file size.

Any better way of getting this to work as I am trying to send many strings through the stream as well as binary data from the file.

在每个write()末尾都带有flush()的BufferedWriter为我解决了这个问题。

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