现在的位置: 首页 > 综合 > 正文

FTP

2012年12月11日 ⁄ 综合 ⁄ 共 5193字 ⁄ 字号 评论关闭

1.配置ftp 服务器 (xp下局域网内)

1.server下,打开设置-->控制面板-->管理工具-->IIS,找到本地计算机下的ftp站点,打开默认ftp站点的属性,做如下配置:
1)ftp站点选项卡
IP地址:指定一IP地址,如:192.168.0.101
端口号:21
2)主目录选项卡
选择内容来源为:此计算机上的目录
ftp站点目录:配置一本机目录,最好为非系统盘,如D:\ftp。选择读取、写入、记录访问

2.server下,网络邻居里的本地连接,属性-->常规-->TCP/IP协议-->属性,取消自动获取IP地址,手动输入如上IP:192.168.0.101

3.client下,网络邻居里的本地连接,属性-->常规-->TCP/IP协议-->属性,取消自动获取IP地址,手动输入IP:192.168.0.102

4:测试:把文件copy到ftp主目录下,即D:\ftp,通过在server端以及client端的IE,输入ftp://192.168.0.101或者ftp://servername (作为服务器的计算机名,可在我的电脑-->属性-->计算机名里获得)可访问到文件,则测试成功。

2.在C#项目下添加ftp类

/**//*
FTPFactory.cs
Better view with tab space=4

Written by Jaimon Mathew (jaimonmathew@rediffmail.com)
Rolander,Dan (Dan.Rolander@marriott.com) has modified the 
download
method to cope with file name with path information. He also 
provided
the XML comments so that the library provides Intellisense 
descriptions.

use the following line to compile
csc /target:library /out:FTPLib.dll /r:System.DLL FTPFactory.cs
*/


using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace WindowsApplication1
{

    
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;

        
public FTPFactory()
        
{

            remoteHost  
= "localhost";
            remotePath  
= ".";
            remoteUser  
= "anonymous";
            remotePass  
= "jaimon@school2000.co.uk";
            remotePort  
= 21;
            debug     
= false;
            logined    
= false;

        }


        
/**////
        
/// Set the name of the FTP server to connect to.
        
///
        
/// Server name

        public void setRemoteHost(string remoteHost)
        
{
            
this.remoteHost = remoteHost;
        }


        
/**////
        
/// Return the name of the current FTP server.
        
///
        
/// Server name

        public string getRemoteHost()
        
{
            
return remoteHost;
        }


        
/**////
        
/// Set the port number to use for FTP.
        
///
        
/// Port number

        public void setRemotePort(int remotePort)
        
{
            
this.remotePort = remotePort;
        }


        
/**////
        
/// Return the current port number.
        
///
        
/// Current port number

        public int getRemotePort()
        
{
            
return remotePort;
        }


        
/**////
        
/// Set the remote directory path.
        
///
        
/// The remote directory path

        public void setRemotePath(string remotePath)
        
{
            
this.remotePath = remotePath;
        }


        
/**////
        
/// Return the current remote directory path.
        
///
        
/// The current remote directory path.

        public string getRemotePath()
        
{
            
return remotePath;
        }


        
/**////
        
/// Set the user name to use for logging into the remote server.
            
///
            
/// Username

            public void setRemoteUser(string remoteUser)
            
{
                
this.remoteUser = remoteUser;
            }


        
/**////
        
/// Set the password to user for logging into the remote server.
            
///
            
/// Password

            public void setRemotePass(string remotePass)
            
{
                
this.remotePass = remotePass;
            }


        
/**////
        
/// Return a string array containing the remote directory's  file list.
            
///
            
///
            
///

            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;
                    }

                }


                
char[] seperator = {' '};
                
string[] mess = mes.Split(seperator);

                cSocket.Close();

                readReply();

                
if(retValue != 226)
                
{
                    
throw new IOException(reply.Substring(4));
                }

                
return mess;

            }


        
/**////
        
/// Return the size of a file.
        
///
        
///
        
///

        public long getFileSize(string fileName)
        
{

            
if(!logined)
            
{
                login();
            }


            sendCommand(
"SIZE " + fileName);
            
long size=0;

            
if(retValue == 213)
            
{
                size 
= Int64.Parse(reply.Substring(4));
            }

            
else

抱歉!评论已关闭.