簡體   English   中英

從Android客戶端向C#服務器發送字符串和文件

[英]Sending strings and File from Android Client to C# server

我正在嘗試將帶有字符串的文件從Android客戶端發送到C#服務器。 字符串除其他外,還將包含有關正在發送的文件的詳細信息,例如:文件大小,文件名等。我遇到的問題是未接收到所有文件字節,因此無法重建原始文件。即使我可以正確檢索所有字符串也可以到達目的地。

我的C#服務器代碼

                        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();
        }

我的Android客戶端代碼

                    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;
            }

注意:如果我僅發送一個字符串,則代碼運行良好,例如:如果僅發送文件大小。

當我嘗試通過流發送許多字符串以及文件中的二進制數據時,使它起作用的任何更好的方法。

在每個write()末尾都帶有flush()的BufferedWriter為我解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM