2013-06-24 28 views
0

即時嘗試通過TCP使用C#發送文件 雖然接收文件 我發現它是0 KB 如何解決它? 下面的代碼 //服務器發送時使用TCP收到的文件爲空

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 


      } 
+0

你能非常明確地發現*會發生什麼嗎?即您發送的內容以及您收到的內容? –

回答

0

最有可能的,你的文件已經在StreamReader的緩衝區消耗,但你從Stream閱讀。坦率地說,使用兩者並不是一個好主意。您可能想要完全丟棄StreamReader,只需使用Stream - 這意味着您需要自己查看文本行的末尾,方法是直到看到一個帶十進制值的字節1013

相關問題