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

学更好的别人,

做更好的自己。

——《微卡智享》

本文长度为1869,预计阅读5分钟

前言

好久没写过C#的文章了,主要原因最近也很少动C#项目的代码,所以也没什么可写的,最近是一个老项目的客户通讯这块要改变,原来一直用的Socket实时通讯交互,现在要改为每天定期通过FTP读写文件的方式进行数据的交互。

项目分析

微卡智享

由上面的动图可以看出,原来通讯方式是我们的服务端与与第三方的服务使用Socket实时通讯,现在的项目要求改为不再与第三方服务直接通讯了,采用FTP的方式上传和下载文件,进行数据的交互,这样做的主要原因采用统一数据处理,不再实时通讯,缓解网速不够,影响正常使用因素。

实现新的方式需要的条件

#实现条件
1Socket实现Ftp文件的上传和下载功能
2利用定时任务启动上传和下载操作

Socket实现Ftp通讯

微卡智享

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

实现效果

代码实现

01

核心类(FTPFactory)

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的代码:

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的通讯了。相对来说还是很简单的,下一篇我们就来看看实现定时任务处理的方式。

扫描二维码

获取更多精彩

微卡智享

「 往期文章 」

开源|OpenCV结合A*及JPS算法室内路径规划

OpenCV DNN换新笔记本后的速度对比

LeetCode|560. 和为K的子数组--C++题解

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C#中进行socket网络编程可以实现一个服务器与多个客户端的通信,以下是一个简单的示例: 服务器端代码: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Server { class Program { static void Main(string[] args) { // 创建一个TCP/IP socket对象 Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 绑定IP地址和端口号 IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8888); listener.Bind(localEndPoint); // 开始监听连接请求 listener.Listen(10); Console.WriteLine("等待客户端连接..."); while (true) { // 接受来自客户端的连接请求 Socket handler = listener.Accept(); // 接收客户端发送的数据 byte[] buffer = new byte[1024]; int bytesRead = handler.Receive(buffer); string data = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine($"接收到客户端数据:{data}"); // 向客户端发送响应数据 byte[] responseBytes = Encoding.ASCII.GetBytes("收到你的消息了!"); handler.Send(responseBytes); // 关闭连接 handler.Shutdown(SocketShutdown.Both); handler.Close(); } } } } ``` 客户端代码: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Client { class Program { static void Main(string[] args) { // 创建一个TCP/IP socket对象 Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 连接服务器 IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8888); sender.Connect(remoteEP); Console.WriteLine("连接服务器成功!"); // 向服务器发送数据 string message = "Hello, Server!"; byte[] bytes = Encoding.ASCII.GetBytes(message); sender.Send(bytes); // 接收服务器的响应数据 byte[] buffer = new byte[1024]; int bytesRead = sender.Receive(buffer); string response = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine($"接收到服务器响应:{response}"); // 关闭连接 sender.Shutdown(SocketShutdown.Both); sender.Close(); Console.ReadKey(); } } } ``` 以上代码可以实现一个简单的服务器与客户端通信,如果想实现多个客户端同时连接服务器,可以使用多线程或异步编程来处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vaccae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值