[英]send multiple file over TCP with C# using TcpClient
我正在嘗試使用C#TcpClient通過TCP發送多個文件,對於單個文件來說效果很好,但是當我有多個文件時,它僅發送第一個文件。
這是我的代碼:
try
{
TcpClient tcpClient = new TcpClient();
NetworkStream networkStream;
FileStream fileStream = null;
tcpClient.Connect(appUpdateMessage.receiverIpAddress, 12000);
networkStream = tcpClient.GetStream();
byte[] byteSend = new byte[tcpClient.ReceiveBufferSize];
string startupPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);
DirectoryInfo directoriesInfo = new DirectoryInfo(startupPath);
DirectoryInfo[] directories = directoriesInfo.GetDirectories();
FileInfo[] files = directoriesInfo.GetFiles();
for (int iLoop = 0; iLoop < directories.Length; iLoop++)
{
FileInfo[] subdirectoryFiles = directories[iLoop].GetFiles();
foreach (FileInfo fi in subdirectoryFiles)
{
fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
BinaryReader binFile = new BinaryReader(fileStream);
FileUpdateMessage fileUpdateMessage = new FileUpdateMessage();
fileUpdateMessage.fileName = fi.Name;
fileUpdateMessage.fileSize = fi.Length;
fileUpdateMessage.targetDirectory = fi.Directory.Name;
MessageContainer messageContainer = new MessageContainer();
messageContainer.messageType = MessageType.FileProperties;
messageContainer.messageContnet = SerializationManager.XmlFormatterObjectToByteArray(fileUpdateMessage);
byte[] messageByte = SerializationManager.XmlFormatterObjectToByteArray(messageContainer);
networkStream.Write(messageByte, 0, messageByte.Length);
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = fileStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
networkStream.Write(downBuffer, 0, bytesSize);
}
fileStream.Close();
}
}
tcpClient.Close();
networkStream.Close();
return true;
}
catch (Exception ex)
{
//logger.Info(ex.Message);
return false;
}
finally
{
}
try
{
TcpClient tcpClient = c as TcpClient;
NetworkStream networkstream = tcpClient.GetStream();
FileStream fileStream = null;
byte[] _data = new byte[1024];
int _bytesRead = 0;
_bytesRead = networkstream.Read(_data, 0, _data.Length);
MessageContainer messageContainer = new MessageContainer();
messageContainer = SerializationManager.XmlFormatterByteArrayToObject(_data, messageContainer) as MessageContainer;
switch (messageContainer.messageType)
{
case MessageType.FileProperties:
FileUpdateMessage fileUpdateMessage = new FileUpdateMessage();
fileUpdateMessage = SerializationManager.XmlFormatterByteArrayToObject(messageContainer.messageContnet, fileUpdateMessage) as FileUpdateMessage;
string startupPath = @"d:updatefolder";//System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);
DirectoryInfo mainDirectory = new DirectoryInfo(startupPath);
DirectoryInfo targetDirecotry = new DirectoryInfo(startupPath + "\\" + fileUpdateMessage.targetDirectory);
if (!targetDirecotry.Exists)
{
mainDirectory.CreateSubdirectory(fileUpdateMessage.targetDirectory);
}
fileStream = new FileStream(startupPath + "\\" + fileUpdateMessage.targetDirectory + "\\" + fileUpdateMessage.fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
long filezie = fileUpdateMessage.fileSize;
int byteSize = 0;
byte[] downBuffer = new byte[2048];
while ((byteSize = networkstream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
fileStream.Write(downBuffer, 0, byteSize);
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
//progressBar1.Value = Convert.ToInt32((byteSize * 100) / fileUpdateMessage.fileSize);
progressBar1.Value = Convert.ToInt32((fileStream.Length * 100) / fileUpdateMessage.fileSize);
lblFileName.Text = fileUpdateMessage.fileName;
});
}
else
{
//progressBar1.Value = Convert.ToInt32((byteSize * 100) / fileUpdateMessage.fileSize);
lblFileName.Text = fileUpdateMessage.fileName;
}
}
fileStream.Close();
networkstream.Close();
break;
}
}
catch (Exception ex)
{
//logger.Error(ex.Message);
}
知道我做錯了什么嗎?
在發送代碼中,您有一個循環,在該循環中您將發送多個文件。 在接收方,我沒有看到相應的循環。
您可以發送將要發送的文件數量,並使客戶端循環多次。 您還可以在每個文件結束后發送一些信息,這表示“這里是另一個文件”或“我已經完成,請立即關閉所有內容”。
您必須在發送文件后關閉流。
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream%28v=vs.71%29.aspx
“注意在通過發送和接收數據時必須關閉NetworkStream。關閉TcpClient不會釋放NetworkStream。”
簡短的答案是,像乒乓球一樣,發送第一個文件,讓客戶端答復,再次發送另一個文件,讓客戶端答復等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.