在使用TCP发送时接收到的文件为空

本文关键字:文件 TCP | 更新日期: 2023-09-27 18:19:34

im尝试使用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

            }

在使用TCP发送时接收到的文件为空

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