前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >项目实战|C#Socket通讯方式改造(一)--Socket实现Ftp的上传和下载

项目实战|C#Socket通讯方式改造(一)--Socket实现Ftp的上传和下载

作者头像
Vaccae
发布2020-06-09 15:40:58
7210
发布2020-06-09 15:40:58
举报
文章被收录于专栏:微卡智享微卡智享

这篇我们就来先实现Socket对Ftp的文件通讯,先看一下实现的效果

实现效果

代码实现

01

核心类(FTPFactory)

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SocketDemo
{
    public class FTPFactory
    {
        private string remoteHost, remotePath, remoteUser, remotePass, mes;
        private int remotePort, bytes;
        private Socket clientSocket;
        private int retValue;
        private Boolean debug;
        private Boolean logined;
        private string reply;
        private static int BLOCK_SIZE = 512;
        Byte[] buffer = new Byte[BLOCK_SIZE];
        Encoding ASCII = Encoding.ASCII;

        /// <summary>
        /// 构造函数
        /// </summary>
        public FTPFactory()
        {
            remoteHost = "localhost";
            remotePath = ".";
            remoteUser = "anonymous";
            remotePass = "";
            remotePort = 21;
            debug = false;
            logined = false;
        }

        /// <summary>
        /// 设置服务器地址
        /// </summary>
        /// <param name="remoteHost"></param>
        public void setRemoteHost(string remoteHost)
        {
            this.remoteHost = remoteHost;
        }
        /// <summary>
        /// 获取服务器地址
        /// </summary>
        /// <returns></returns>
        public string getRemoteHost()
        {
            return remoteHost;
        }

        /// <summary>
        /// 设置端口号
        /// </summary>
        /// <param name="remotePort"></param>
        public void setRemotePort(int remotePort)
        {
            this.remotePort = remotePort;
        }

        /// <summary>
        /// 获取端口号
        /// </summary>
        /// <returns></returns>
        public int getRemotePort()
        {
            return remotePort;
        }
        
        /// <summary>
        /// 设置远程路径
        /// </summary>
        /// <param name="remotePath"></param>
        public void setRemotePath(string remotePath)
        {
            this.remotePath = remotePath;
        }
        
        /// <summary>
        /// 获取远程路径
        /// </summary>
        /// <returns></returns>
        public string getRemotePath()
        {
            return remotePath;
        }
        
        /// <summary>
        /// 设置用户名
        /// </summary>
        /// <param name="remoteUser"></param>
        public void setRemoteUser(string remoteUser)
        {
            this.remoteUser = remoteUser;
        }
        
        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="remotePass"></param>
        public void setRemotePass(string remotePass)
        {
            this.remotePass = remotePass;
        }
       
        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="mask"></param>
        /// <returns></returns>
        public string[] getFileList(string mask)
        {
            if (!logined)
            {
                login();
            }
            Socket cSocket = createDataSocket();
            sendCommand("NLST " + mask);
            if (!(retValue == 150 || retValue == 125))
            {
                throw new IOException(reply.Substring(4));
            }
            mes = "";
            while (true)
            {
                int bytes = cSocket.Receive(buffer, buffer.Length, 0);
                mes += ASCII.GetString(buffer, 0, bytes);
                if (bytes < buffer.Length)
                {
                    break;
                }
            }

            string[] mess = Regex.Split(mes, "\r\n");
            cSocket.Close();
            readReply();
            if (retValue != 226)
            {
                throw new IOException(reply.Substring(4));
            }
            return mess;
        }
        
        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public long getFileSize(string fileName)
        {
            if (!logined)
            {
                login();
            }
            sendCommand("SIZE " + fileName);
            long size = 0;
            if (retValue == 213)
            {
                size = Int64.Parse(reply.Substring(4));
            }
            else
            {
                throw new IOException(reply.Substring(4));
            }
            return size;
        }
        
        /// <summary>
        /// 登陆
        /// </summary>
        public void login()
        {
            clientSocket = new
            Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(remoteHost), remotePort);
            try
            {
                clientSocket.Connect(ep);
            }
            catch (Exception)
            {
                throw new IOException("连接不上远程服务器!");
            }
            readReply();
            if (retValue != 220)
            {
                close();
                throw new IOException(reply.Substring(4));
            }
            if (debug)
                Console.WriteLine("USER " + remoteUser);
            sendCommand("USER " + remoteUser);
            if (!(retValue == 331 || retValue == 230))
            {
                cleanup();
                throw new IOException(reply.Substring(4));
            }
            if (retValue != 230)
            {
                if (debug)
                    Console.WriteLine("PASS xxx");
                sendCommand("PASS " + remotePass);
                if (!(retValue == 230 || retValue == 202))
                {
                    cleanup();
                    throw new IOException(reply.Substring(4));
                }
            }
            logined = true;
            Console.WriteLine("地址" + remoteHost + "连接成功!");
            chdir(remotePath);
        }
        
        /// <summary>
        /// 设置通讯模式 true - 二进制模式  false - ASCII码模式
        /// </summary>
        /// <param name="mode"></param>
        public void setBinaryMode(Boolean mode)
        {
            if (mode)
            {
                sendCommand("TYPE I");
            }
            else
            {
                sendCommand("TYPE A");
            }
            if (retValue != 200)
            {
                throw new IOException(reply.Substring(4));
            }
        }
        
        /// <summary>
        /// 下载一个文件
        /// </summary>
        /// <param name="remFileName"></param>
        public void download(string remFileName)
        {
            download(remFileName, "", false);
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remFileName"></param>
        /// <param name="resume"></param>
        public void download(string remFileName, Boolean resume)
        {
            download(remFileName, "", resume);
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remFileName"></param>
        /// <param name="locFileName">本地文件名,没有创建,有则重写</param>
        public void download(string remFileName, string locFileName)
        {
            download(remFileName, locFileName, false);
        }

        /// <summary>
        /// 下载文件 路径必须存在
        /// </summary>
        /// <param name="remFileName"></param>
        /// <param name="locFileName"></param>
        /// <param name="resume"></param>
        public void download(string remFileName, string
        locFileName, Boolean resume)
        {
            if (!logined)
            {
                login();
            }
            setBinaryMode(true);
            Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
            if (locFileName.Equals(""))
            {
                locFileName = remFileName;
            }
            if (!File.Exists(locFileName))
            {
                Stream st = File.Create(locFileName);
                st.Close();
            }
            FileStream output = new
            FileStream(locFileName, FileMode.Open);
            Socket cSocket = createDataSocket();
            long offset = 0;
            if (resume)
            {
                offset = output.Length;
                if (offset > 0)
                {
                    sendCommand("REST " + offset);
                    if (retValue != 350)
                    {
                        offset = 0;
                    }
                }
                if (offset > 0)
                {
                    if (debug)
                    {
                        Console.WriteLine("seeking to " + offset);
                    }
                    long npos = output.Seek(offset, SeekOrigin.Begin);
                    Console.WriteLine("new pos=" + npos);
                }
            }
            sendCommand("RETR " + remFileName);
            if (!(retValue == 150 || retValue == 125))
            {
                throw new IOException(reply.Substring(4));
            }
            while (true)
            {
                bytes = cSocket.Receive(buffer, buffer.Length, 0);
                output.Write(buffer, 0, bytes);
                if (bytes <= 0)
                {
                    break;
                }
            }
            output.Close();
            if (cSocket.Connected)
            {
                cSocket.Close();
            }
            Console.WriteLine("");
            readReply();
            if (!(retValue == 226 || retValue == 250))
            {
                throw new IOException(reply.Substring(4));
            }
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileName"></param>
        public void upload(string fileName)
        {
            upload(fileName, false);
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="resume"></param>
        public void upload(string fileName, Boolean resume)
        {
            if (!logined)
            {
                login();
            }
            Socket cSocket = createDataSocket();
            long offset = 0;
            if (resume)
            {
                try
                {
                    setBinaryMode(true);
                    offset = getFileSize(fileName);
                }
                catch (Exception)
                {
                    offset = 0;
                }
            }
            if (offset > 0)
            {
                sendCommand("REST " + offset);
                if (retValue != 350)
                {
                    offset = 0;
                }
            }
            sendCommand("STOR " + Path.GetFileName(fileName));
            if (!(retValue == 125 || retValue == 150))
            {
                throw new IOException(reply.Substring(4));
            }

            FileStream input = new
            FileStream(fileName, FileMode.Open);
            if (offset != 0)
            {
                if (debug)
                {
                    Console.WriteLine("seeking to " + offset);
                }
                input.Seek(offset, SeekOrigin.Begin);
            }
            Console.WriteLine("Uploading file " + fileName + " to " + remotePath);
            while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                cSocket.Send(buffer, bytes, 0);
            }
            input.Close();
            Console.WriteLine("");
            if (cSocket.Connected)
            {
                cSocket.Close();
            }
            readReply();
            if (!(retValue == 226 || retValue == 250))
            {
                throw new IOException(reply.Substring(4));
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName"></param>
        public void deleteRemoteFile(string fileName)
        {
            if (!logined)
            {
                login();
            }
            sendCommand("DELE " + fileName);
            if (retValue != 250)
            {
                throw new IOException(reply.Substring(4));
            }
        }

        /// <summary>
        /// 重命名文件
        /// </summary>
        /// <param name="oldFileName"></param>
        /// <param name="newFileName"></param>
        public void renameRemoteFile(string oldFileName, string
        newFileName)
        {
            if (!logined)
            {
                login();
            }
            sendCommand("RNFR " + oldFileName);
            if (retValue != 350)
            {
                throw new IOException(reply.Substring(4));
            }

            sendCommand("RNTO " + newFileName);
            if (retValue != 250)
            {
                throw new IOException(reply.Substring(4));
            }
        }
        
        /// <summary>
        /// 创建一个远程路径
        /// </summary>
        /// <param name="dirName"></param>
        public void mkdir(string dirName)
        {
            if (!logined)
            {
                login();
            }
            sendCommand("MKD " + dirName);
            if (retValue != 257)
            {
                throw new IOException(reply.Substring(4));
            }
        }

        /// <summary>
        /// 删除远程路径
        /// </summary>
        /// <param name="dirName"></param>
        public void rmdir(string dirName)
        {
            if (!logined)
            {
                login();
            }
            sendCommand("RMD " + dirName);
            if (retValue != 250)
            {
                throw new IOException(reply.Substring(4));
            }
        }

        /// <summary>
        /// 改变当前远程路径
        /// </summary>
        /// <param name="dirName"></param>
        public void chdir(string dirName)
        {
            if (dirName.Equals("."))
            {
                return;
            }
            if (!logined)
            {
                login();
            }
            sendCommand("CWD " + dirName);
            if (retValue != 250)
            {
                throw new IOException(reply.Substring(4));
            }
            this.remotePath = dirName;
            Console.WriteLine("远程服务器工作路径" + remotePath);
        }

        /// <summary>
        /// 关闭链接
        /// </summary>
        public void close()
        {
            if (clientSocket != null)
            {
                sendCommand("QUIT");
            }
            cleanup();
            Console.WriteLine("Closing...");
        }
        
        /// <summary>
        /// 设置是不是测试模式
        /// </summary>
        /// <param name="debug"></param>
        public void setDebug(Boolean debug)
        {
            this.debug = debug;
        }

        private void readReply()
        {
            mes = "";
            reply = readLine();
            retValue = Int32.Parse(reply.Substring(0, 3));
        }

        private void cleanup()
        {
            if (clientSocket != null)
            {
                clientSocket.Close();
                clientSocket = null;
            }
            logined = false;
        }
        private string readLine()
        {
            while (true)
            {
                bytes = clientSocket.Receive(buffer, buffer.Length, 0);
                mes += ASCII.GetString(buffer, 0, bytes);
                if (bytes < buffer.Length)
                {
                    break;
                }
            }

            string[] mess = Regex.Split(mes, "\r\n");
            if (mes.Length > 2)
            {
                mes = mess[mess.Length - 2];
            }
            else
            {
                mes = mess[0];
            }
            if (!mes.Substring(3, 1).Equals(" "))
            {
                return readLine();
            }
            if (debug)
            {
                for (int k = 0; k < mess.Length - 1; k++)
                {
                    Console.WriteLine(mess[k]);
                }
            }
            return mes;
        }
        private void sendCommand(String command)
        {
            Byte[] cmdBytes =
            //Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());

            Encoding.Default.GetBytes((command + "\r\n").ToCharArray());
            clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
            readReply();
        }

        private Socket createDataSocket()
        {
            sendCommand("PASV");
            if (retValue != 227)
            {
                throw new IOException(reply.Substring(4));
            }
            int index1 = reply.IndexOf('(');
            int index2 = reply.IndexOf(')');
            string ipData =
            reply.Substring(index1 + 1, index2 - index1 - 1);
            int[] parts = new int[6];
            int len = ipData.Length;
            int partCount = 0;
            string buf = "";
            for (int i = 0; i < len && partCount <= 6; i++)
            {
                char ch = Char.Parse(ipData.Substring(i, 1));
                if (Char.IsDigit(ch))
                    buf += ch;
                else if (ch != ',')
                {
                    throw new IOException("Malformed PASV reply: " +
                    reply);
                }
                if (ch == ',' || i + 1 == len)
                {
                    try
                    {
                        parts[partCount++] = Int32.Parse(buf);
                        buf = "";
                    }
                    catch (Exception)
                    {
                        throw new IOException("Malformed PASV reply: " +
                        reply);
                    }
                }
            }
            string ipAddress = parts[0] + "." + parts[1] + "." +
            parts[2] + "." + parts[3];
            int port = (parts[4] << 8) + parts[5];
            Socket s = new
            Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ep = new
            IPEndPoint(IPAddress.Parse(ipAddress), port);
            try
            {
                s.Connect(ep);
            }
            catch (Exception)
            {
                throw new IOException("无法链接远程服务器");
            }
            return s;
        }
    }
}

02

界面代码(Form)

Form的布局里央加入了三个textbox(Ftp的地址,Ftp的端口号和显示状态的文本框),三个button(生成文件,上传文件,下载文件的按钮),如下图:

Form的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //文件路径
        string filePath = Directory.GetCurrentDirectory() + "\\Data";
        //FTP操作
        private FTPFactory _ftp;


        //定义委托,用于线程操作时同步UI
        public TextFunction TextShow;
        public delegate void TextFunction(string Msg);
        public void TextShowFunction(string Msg)
        {
            //主界面textbox显示信息
            tbMsg.AppendText(Msg + "\r\n");
        }

        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateFile_Click(object sender, EventArgs e)
        {
            Random rd = new Random();
            int count = rd.Next(0, 200);

            //判断路径是否存在,不存在创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            //将循环完后拼接好的字符串保存到txt文件里,文件名为用户控件名称
            string filename = filePath + "\\file" + count + ".txt";

            StringBuilder sb = new StringBuilder();
            //写入数据
            for(int i = 0; i < count; ++i)
            {
                sb.Append("data:" + i + "|" + DateTime.Now.ToString());
            }
            File.WriteAllText(filename, sb.ToString(), Encoding.UTF8);
            TextShowFunction("文件" + filename + "生成成功!");
        }

        /// <summary>
        /// 往FTP上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUploadFile_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> files = Directory.GetFiles(filePath).ToList();
                if (files.Count <= 0)
                {
                    TextShowFunction("未获取到可上传文件!");
                }
                //初始化FTP
                InitFtp();

                foreach (string file in files)
                {
                    //发送文件
                    _ftp.upload(file);
                    TextShowFunction(file + "上传成功!");
                }

                _ftp.close();
            }
            catch (Exception ex)
            {
                TextShowFunction(ex.Message);                
            }
        }

        /// <summary>
        /// 初始化FTP
        /// </summary>
        private void InitFtp()
        {
            string ipadress = tbSocketIp.Text;
            int port = Convert.ToInt32(tbSocketPort.Text);

            _ftp = new FTPFactory();
            _ftp.setRemoteHost(ipadress);
            _ftp.setRemotePort(port);
            _ftp.setRemoteUser("此处输入用户");
            _ftp.setRemotePass("此处输入密码");
            _ftp.login();
        }

        private void btnDownloadfile_Click(object sender, EventArgs e)
        {
            try
            {
                InitFtp();

                string[] strs = _ftp.getFileList(_ftp.getRemotePath());
                foreach(string file in strs)
                {
                    if (file == "") continue;
                    string localfile = filePath + "\\" + file;
                    _ftp.download(file, localfile);
                    TextShowFunction(file + "下载成功!");
                }

                _ftp.close();
            }
            catch(Exception ex)
            {
                TextShowFunction(ex.Message);
            }
        }
    }
}

这样就可以实现Ftp的通讯了。相对来说还是很简单的,下一篇我们就来看看实现定时任务处理的方式。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微卡智享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档