C# - 服务器 - 客户端聊天中的文件传输

时间:2014-05-17 10:49:16

标签: c# client-server chat file-transfer

早上好,我一直在尝试根据文件传输找到一些教程,但不幸的是我无法找到一个可以帮助我在聊天期间使其正常工作...有正常的代码,我该如何制作这样,在聊天过程中,其中一个用户可以从服务器端访问下载功能

客户端:

        public static void writer()
        {
            string str;

            byte[] data = new byte[1024];

            nome = Console.ReadLine();
            data = Encoding.ASCII.GetBytes(nome);
            client.Send(data, data.Length, SocketFlags.None);

            do
            {
                str = Console.ReadLine();
                data = Encoding.ASCII.GetBytes(str);
                client.Send(data, data.Length, SocketFlags.None);

            } while (str != "exit");

            client.Shutdown(SocketShutdown.Both);
            tReader.Abort();
            client.Close();
        }

        public static void reader()
        {
            byte[] data = new byte[1024];
            int recv;
            while (true)
            {
                try
                {
                    recv = client.Receive(data);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Erro: " + e.Message);
                    Console.ReadLine();
                    break;
                }

                Console.WriteLine("\n" + Encoding.ASCII.GetString(data, 0, recv));

            }
        }
    }
}

fileTransfer CLass:

class SistemaFicheiros
    {

        public void NomeFicheiro(Socket socket)
        {
            bool validado = false;
            while (!validado)
            {
                string nome, pass = string.Empty;
                byte[] perguntaNome = new byte[1024];
                string introduzNome = "Introduza o nome do ficheiro.";
                perguntaNome = Encoding.ASCII.GetBytes(introduzNome);
                socket.Send(perguntaNome);

                byte[] dataaux = new byte[1024];
                int nBytes;
                nBytes = socket.Receive(dataaux);
                nome = Encoding.ASCII.GetString(dataaux, 0, nBytes);

                if (nome.Equals("test.txt"))
                {
                    validado = true;
                    IniciarDownloadFicheiro(socket, nome);                   
                }
            }
        }

        public void IniciarDownloadFicheiro(Socket socket, string filename)
        {
            try
            {
                string filePath = @"C:\FT\";//Your File Path;

                byte[] fileNameByte = Encoding.ASCII.GetBytes(filename);
                byte[] fileData = File.ReadAllBytes(filePath + filename);
                byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

                fileNameLen.CopyTo(clientData, 0);
                fileNameByte.CopyTo(clientData, 4);
                fileData.CopyTo(clientData, 4 + fileNameByte.Length);
                socket.Send(clientData);

                Console.WriteLine("File:{0} has been sent.", filename);
            }
            catch (Exception ex)
            {
                Console.WriteLine("File Receiving fail." + ex.Message);
            }
        }
    }

如果我试图在客户端上执行此操作,那会不会发生冲突?因为我只想要让文件接收它或发送到服务器的用户

0 个答案:

没有答案
相关问题