0

我在接收文件时尝试使用 C# 通过 TCP 发送文件我发现它是 0 KB 如何修复它?这是代码//服务器

 TcpListener list = new TcpListener(localAddr, port);
            list.Start();
            TcpClient client  = list.AcceptTcpClient();//accepting connection with client when send button is clicked there .. !
            StreamReader s = new StreamReader(client.GetStream());
            Stream st = client.GetStream();
            rd = s.ReadLine();
            //FileStream fileStream = new FileStream(textBox1.Text + "\\" + rd.Substring(0, rd.LastIndexOf('.')), FileMode.Create, FileAccess.Write, FileShare.ReadWrite);//new file stream

            FileStream fileStream = new FileStream(folderBrowserDialog1.SelectedPath , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);//new file stream

            int byteSize = 0;
            byte[] b1 = new byte[2048];



            while ((byteSize = st.Read(b1, 0, b1.Length)) > 0)//if stream read any thing that mean the file didn't finish yet !
            {
              fileStream.Write(b1, 0, byteSize);//write data in file till it finishes


            }
4

1 回答 1

0

最有可能的是,您的文件已在StreamReader的缓冲区中使用,但您正在从Stream. 坦率地说,同时使用两者并不是一个好主意。您可能想要StreamReader完全放弃并只使用Stream- 这只是意味着您需要自己查找文本行的末尾,通过阅读直到您看到一个带有十进制值的字节1013.

于 2013-06-24T06:45:47.273 回答