简体   繁体   English

使用TcpClient使用C#通过TCP发送多个文件

[英]send multiple file over TCP with C# using TcpClient

I'm trying to send multiple files over TCP using C# TcpClient, for a single file it works great, but when I have multiple files, it sends only the first one. 我正在尝试使用C#TcpClient通过TCP发送多个文件,对于单个文件来说效果很好,但是当我有多个文件时,它仅发送第一个文件。

Here is my code: 这是我的代码:

SENDING FILES 发送文件

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
{

}

RECEIVING FILES 接收文件

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);
}

Any idea what I am doing wrong? 知道我做错了什么吗?

In your sending code, you've got a loop where you're sending multiple files. 在发送代码中,您有一个循环,在该循环中您将发送多个文件。 On the receiving side, I don't see a corresponding loop. 在接收方,我没有看到相应的循环。

You could send the number of files that are about to be sent, and have the client loop that many times. 您可以发送将要发送的文件数量,并使客户端循环多次。 You could also send something after the end of each file, which would indicate "Here comes another file" or "I'm done, close everything now". 您还可以在每个文件结束后发送一些信息,这表示“这里是另一个文件”或“我已经完成,请立即关闭所有内容”。

you must close the stream after sending a file. 您必须在发送文件后关闭流。

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream%28v=vs.71%29.aspx http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream%28v=vs.71%29.aspx

"Note You must close the NetworkStream when you are through sending and receiving data. Closing TcpClient does not release the NetworkStream." “注意在通过发送和接收数据时必须关闭NetworkStream。关闭TcpClient不会释放NetworkStream。”

简短的答案是,像乒乓球一样,发送第一个文件,让客户端答复,再次发送另一个文件,让客户端答复等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM