发送多个文件,通过TCP使用的TcpClient C# [英] send multiple file over TCP with C# using TcpClient

查看:526
本文介绍了发送多个文件,通过TCP使用的TcpClient C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#TcpClient的通过TCP发送多个文件,它的伟大工程的单个文件,但是当我有多个文件时,只发送第一个。

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.

下面是我的代码:

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".

这篇关于发送多个文件,通过TCP使用的TcpClient C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆