简体   繁体   English

从Android客户端向C#服务器发送字符串和文件

[英]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. 我正在尝试将带有字符串的文件从Android客户端发送到C#服务器。 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 我的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();
        }

My Android Client Code 我的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;
            }

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为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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