首页 > 代码库 > C#通过socket判断FTP服务器是否通畅并判断用户名密码是否正确

C#通过socket判断FTP服务器是否通畅并判断用户名密码是否正确

        /// <summary>
        /// 通过socket判断ftp是否通畅
        /// </summary> 
        /// <returns></returns>
        public static bool CheckFtp(string ip, string ftpuser, string ftppas,out string errmsg, int port = 21)
        { 
            #region 输入数据检查
            if (ftpuser.Trim().Length==0)
            {
                errmsg = "FTP用户名不能为空,请检查设置!";
                return false;
            }
            if(ftppas.Trim().Length == 0)
            {
                errmsg = "FTP密码不能为空,请检查设置!";
                return false;
            }
            IPAddress address;
            try
            {
                address = IPAddress.Parse(ip);
            }
            catch
            {
                errmsg =string.Format("FTP服务器IP:{0}解析失败,请检查是否设置正确!",ip);
                return false;
            }
            #endregion

            bool ret = false;
            byte[] result = new byte[1024];
            int pingStatus = 0, userStatus = 0, pasStatus = 0, exitStatus = 0; //连接返回,用户名返回,密码返回,退出返回

            try
            {
                int receiveLength;
                
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.SendTimeout = 2000;
                socket.ReceiveTimeout = 2000;//超时设置成2000毫秒   
                try
                {
                    socket.Connect(new IPEndPoint(address, port)); //以21端口进行连接 
                    pingStatus = 200;
                }
                catch
                {
                    pingStatus = -1;
                }

                if(pingStatus==200) //状态码200 - TCP连接成功
                {  
                    receiveLength = socket.Receive(result);
                    pingStatus = getFtpReturnCode(result, receiveLength); //连接状态
                    if(pingStatus==220)//状态码220 - FTP返回欢迎语
                    {
                        socket.Send(Encoding.Default.GetBytes(string.Format("{0}{1}", "USER " + ftpuser, Environment.NewLine)));
                        receiveLength = socket.Receive(result);
                        userStatus = getFtpReturnCode(result, receiveLength);  
                        if(userStatus==331)//状态码331 - 要求输入密码
                        { 
                            socket.Send(Encoding.Default.GetBytes(string.Format("{0}{1}", "PASS " + ftppas, Environment.NewLine)));
                            receiveLength = socket.Receive(result);
                            pasStatus = getFtpReturnCode(result, receiveLength); 
                            if(pasStatus== 230)//状态码230 - 登入因特网
                            { 
                                errmsg = string.Format("FTP:{0}@{1}登陆成功", ip, port);
                                ret = true;
                                socket.Send(Encoding.Default.GetBytes(string.Format("{0}{1}", "QUIT", Environment.NewLine))); //登出FTP
                                receiveLength = socket.Receive(result);
                                exitStatus = getFtpReturnCode(result, receiveLength);
                            }
                            else
                            { // 状态码230的错误
                                errmsg = string.Format("FTP:{0}@{1}登陆失败,用户名或密码错误({2})", ip, port, pasStatus);
                            }
                        }
                        else
                        {// 状态码331的错误 
                            errmsg = string.Format("使用用户名:‘{0}‘登陆FTP:{1}@{2}时发生错误({3}),请检查FTP是否正常配置!", ftpuser, ip, port, userStatus);
                        }
                    }
                    else
                    {// 状态码220的错误 
                        errmsg = string.Format("FTP:{0}@{1}返回状态错误({2}),请检查FTP服务是否正常运行!", ip, port, pingStatus);
                    }
                }
                else
                {// 状态码200的错误
                    errmsg = string.Format("无法连接FTP服务器:{0}@{1},请检查FTP服务是否启动!", ip, port);
                } 
                socket.Close(); //关闭socket
                socket = null;
            }
            catch(Exception ex)
            { //连接出错 
                errmsg = string.Format("FTP:{0}@{1}连接出错:",ip,port) + ex.Message; 
                ret = false;
            } 
            return ret;
        }
        /// <summary>
        /// 传递FTP返回的byte数组和长度,返回状态码(int)
        /// </summary>
        /// <param name="retByte"></param>
        /// <param name="retLen"></param>
        /// <returns></returns>
        private static int getFtpReturnCode(byte[] retByte,int retLen)
        {
            try
            {
                string str = Encoding.ASCII.GetString(retByte, 0, retLen).Trim();
                return int.Parse(str.Substring(0, 3));
            }
            catch
            {
                return -1;
            }
        }

小弟才疏学浅,如果不足之处请大家多多指导。

 

C#通过socket判断FTP服务器是否通畅并判断用户名密码是否正确