通过NetworkStream C#识别传入文件

时间:2015-03-21 08:00:29

标签: c# ftp tcpclient tcplistener networkstream

我正在使用TcpClient和TcpListener类创建客户端/服务器聊天应用程序。它应该能够在客户端和服务器之间传输文本消息和文件。我能够通过为每个单独的客户端创建一个线程来处理文本消息,然后创建一个辅助线程来接收传入消息,使主线程保留用于发送消息。现在,如果我能够识别传入的消息是文件而不是文本消息,那么我知道如何使用NetworkStream和FileStream来处理它。但我无法这样做。处理传入文件的代码是here。如果使用NetworkStream for FTP有任何限制,请告诉我。

1 个答案:

答案 0 :(得分:1)

答案:建立自己的协议。

通过构建自己的良好通信协议,您可以控制所有数据/消息流。

例如;
1 - 用户想要将文件发送到服务器
2-Client发送命令通知服务器它将发送文件。喜欢;
@File@filename;filesize;
3服务器将准备好的消息发送回客户端@FileAccepted@ 4-Server开始监听缓冲包,当它接收时将它们写入流中 5-当客户端收到{@FileAccepted@}命令时,开始将包发送到服务器。确保它们的缓冲区大小相同 6-当所有字节完成客户端在不同缓冲区中发送@FileEnd@时(我使用256表示命令,1024表示文件传输)
7-当服务器收到256字节命令时查看其@FileEnd@命令是否为真,刷新文件流并关闭连接。

我建议您使用Async

像这样监听服务器上的连接

server.BeginAcceptTcpClient(ServerAcceptEnd,server);

当存在连接时

public void ServerAcceptEnd(IAsyncResult ar)
{
    if(!ar.IsCompleted)
    {
        //Something went wrong
        AcceptServer();
        return;
    }
    try
    {
        var cli = servertc.EndAcceptTcpClient(ar);
        if(cli.Connected)
        {
            //Get the first Command   
            cli.GetStream().BeginRead(serverredbuffer,0,serverredbuffer.Length,ServerFirstReadEnd,cli);
        }
        else
        {
            //Connection was not successfull log and wait
            AcceptServer();
        }
    }
    catch(Exceiption ex)
    {
        //An error occur log and wait for new connections
        AcceptServer();
    }
 }

收到第一个命令时;

    public void ServerFirstReadEnd(IAsyncResult ar)
    {
    if(!ar.IsCompleted)
    {
        //Something went wrong
        AcceptServer();
        return;
    }
        try
        {
            TcpClient cli = (TcpClient)ar.AsyncState;
            int read = cli.GetStream().EndRead(ar);
            string req = toString(serverredbuffer);  
            if(req.StartsWith("@File@"))
            {
                //File Received
                string filename = req.Replace("@File@","");
                string[] spp = filename.Split(';');
                filename = spp[0];
                serverreceivetotalbytes = Convert.ToInt64(spp[1]);
                cli.GetStream().Write(toByte("@FileAccepted@",256),0,256);
                cli.GetStream().BeginRead(serverreceivebuffer,0,1024,ServerReadFileCyle,cli)   
            }
            else
            {
                //Message Received
            }
        }
        catch(Exception ex)
        {
            //An error occur log and wait for new connections
            AcceptServer();
        }
    }

文件接收方法;

    public void ServerReadFileCyle(IAsyncResult ar)
    {
        TcpClient cli = (TcpClient)ar.AsyncState;
        if(ar.IsCompleted)
        {
            int read = cli.GetStream().EndRead(ar);
            if(read == 256)
            {
                try
                {
                    string res = toString(serverreceivebuffer);
                    if(res == "@FileEnd@")
                        read = 0;
                }
                catch
                {
                }
            }
            if(read > 0)
            {
                serverfile.Write(serverreceivebuffer,0,read);
                cli.GetStream().BeginRead(serverreceivebuffer,0,1024,ServerReadFileCyle,cli);
            }
            else
            {
                serverfile.Flush();
                serverfile.Dispose();
                AcceptServer();
            }
        }
    }

这部分是服务器端。对于客户端;

首先发送文件向服务器发送信息,然后等待服务器的响应。

try
{
    System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
    ofd.Multiselect = false;
    ofd.FileName="";
    if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        filesendpath = ofd.FileName;
        senderfilestream = System.IO.File.Open(filesendpath,System.IO.FileMode.Open);
        sendertotalbytes = senderfilestream.Length;
        filesendcommand = "@File@" + System.IO.Path.GetFileName(filesendpath) + ";" + senderfilestream.Length;
        senderfilestream.Position = 0;
        sendertc.BeginConnect(ip.Address,55502,FileConnect,null);
    }
    else
    {
        //No file selected
    }

}
catch(Exception ex)
{
    //Error connecting log the error
}

如果连接成功,则发送文件命令并等待响应;

    public void FileConnect(IAsyncResult ar)
    {
        if(ar.IsCompleted)
        {
            sender.EndConnect(ar);
            if(sender.Connected)
            {
                sender.GetStream().Write(toByte(filesendcommand,256),0,256);
                sender.GetStream().BeginRead(ComputerNameBuffer,0,256,FileSendCyleStarter,null);

            }
        }
    }

收到回复时看看是否成功接受;

    public void FileSendCyleStarter(IAsyncResult ar)
    {
        if(ar.IsCompleted)
        {
            if(sender.Connected)
            {
                string kabul = toString(ComputerNameBuffer);
                if(kabul == "@FileAccepted@")
                {
                    senderfilestream.BeginRead(filesendbuffer,0,1024,FileSendCyle,null);
                }
            }
        }
    }

发送文件有三个步骤;
1 - 阅读一个块开始
2 - 然后将块发送到server.if完成send @ FileEnd @命令并跳过步骤3 3 - 阅读下一个文件块
4 - 如果文件未完成则返回步骤2

第1步:

senderfilestream.BeginRead(filesendbuffer,0,1024,FileSendCyle,null);

步骤2-4:

    public void FileSendCyle(IAsyncResult ar)
    {
        if(ar.IsCompleted)
        {
            if(sendertc.Connected)
            {
                int  read = senderfilestream.EndRead(ar);
                if(read > 0)
                {
                    sendertc.GetStream().BeginWrite(filesendbuffer,0,read,FileSendCyle2,null);

                }
                else
                {

                    sendertc.GetStream().Write(toByte("@FileEnd@",256),0,256);

                }
            }
        }
    }

第3步:

    public void FileSendCyle2(IAsyncResult ar)
    {
        if(ar.IsCompleted)
        {
            if(sendertc.Connected)
            {
               sendertc.GetStream().EndWrite(ar);
               senderfilestream.BeginRead(filesendbuffer,0,1024,FileSendCyle,null);
            }
        }
    }

在abowe示例中,有两个名为toString()和toByte()的方法。我用它们将字符串转换为字节,将字节转换为字符串。这些是它们;

    public string toString(byte[] buffer)
    {
        return Encoding.UTF8.GetString(buffer).Replace("\0","");
    }
    public byte[] toByte(string str,int bufferlenght)
    {
        byte[] buffer = new byte[256];
        Encoding.UTF8.GetBytes(str,0,str.Length,buffer,0);
        return buffer;
    }

代码abowe示例并不完美,需要大量的错误处理和流量控制。我写了这些给你一个想法和一个快速启动。

希望它的任何部分都能帮助任何人^ _ ^