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

时间:2013-07-11 01:26:30

标签: c# android file sockets networking

我正在尝试将带有一堆字符串的文件从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;
            }

注意:如果我只发送一个字符串,代码效果很好,例如:如果我只发送文件大小。

任何更好的方法可以让它工作,因为我试图通过流发送许多字符串以及文件中的二进制数据。

1 个答案:

答案 0 :(得分:2)

每个write()结束时使用flush()的BufferedWriter为我解决了这个问题。