繁体   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