我想通過NetworkStream發送文件並在客戶端重新構建它。我可以正確地獲取數據(我認爲),但是當我使用BinaryWriter或FileStream對象來重新創建文件時,無論使用何種方法,文件在一開始都會被切斷。使用NetworkStream傳輸文件然後重建文件失敗
private void ReadandSaveFileFromServer(ref TcpClient clientATF,ref NetworkStream currentStream, string locationToSave)
{
int fileSize = 0;
string fileName = "";
fileName = ReadStringFromServer(ref clientATF,ref currentStream);
fileSize = ReadIntFromServer(ref clientATF,ref currentStream);
byte[] fileSent = new byte[fileSize];
if (currentStream.CanRead && clientATF.Connected)
{
currentStream.Read(fileSent, 0, fileSent.Length);
WriteToConsole("Log Recieved");
}
else
{
WriteToConsole("Log Transfer Failed");
}
FileStream fileToCreate = new FileStream(locationToSave + "\\" + fileName, FileMode.Create);
fileToCreate.Seek(0, SeekOrigin.Begin);
fileToCreate.Write(fileSent, 0, fileSent.Length);
fileToCreate.Close();
//binWriter = new BinaryWriter(File.Open(locationToSave + "\\" + fileName, FileMode.Create));
//binWriter.Write(fileSent);
//binWriter.Close();
}
當我逐步檢查fileName和fileSize時,它們是正確的。字節[]也完全填充。關於我接下來可以做什麼的任何線索?
在此先感謝...
肖恩
編輯!!!:
所以我想通了,發生了什麼事。當我讀取一個字符串,然後從流中讀取Int時,字節數組長度爲256個索引。所以我對字符串的讀取正在接受整數,然後會打斷其他區域。需要弄清楚...
如果您需要進一步的幫助,您需要發佈您的ReadStringFromServer和ReadIntFromServer方法。 – 2010-09-20 19:25:44