如何通过SocketConnection实现命令与FTP服务器通信

时间:2009-11-16 10:50:17

标签: .net ftp sockets

我正在构建一个应用程序,它向/从我的远程FTP服务器发送/接收大数据。我想通过SocketConnection实现FTP命令。我必须首先打开连接,然后用户,然后密码到服务器,验证所需的响应后,我想开始下载/上传。我知道怎么开始。我只有想法:

public class SimpleFTP
{

    private static boolean DEBUG = false;
    StreamConnectionNotifier connectionNofitier = null;
    InputStreamReader reader = null;
    OutputStreamWriter writer = null;
    StreamConnection conn = null;
    private static String URL = null;
    private String server = "XXX.XXX.XXX.XX";
    private String port = "21";
    private String user = "user";
    private String pwd = "pwd";


    public SimpleFTP()
    {
    }


    public void connect() throws IOException
    {


        try
        {
            URL = "socket://" + server + port + user + pwd+";deviceside=true";
            URL = "socket://" + server + port +";deviceside=true";
            connectionNofitier = (StreamConnectionNotifier) Connector.open(URL); 
            System.out.println(Connector.open(URL));

            if(connectionNofitier!=null)
                System.out.println("StreamConnectionNotifier is already connected..!!");

            conn = (StreamConnection)connectionNofitier.acceptAndOpen(); ;

            if(conn!=null)             //my Simulator hangs here and does nothing....
                System.out.println("StreamConnection is already connected..!!");

            reader = new InputStreamReader( conn.openInputStream() );
            writer = new OutputStreamWriter( conn.openOutputStream() )  ;
        }
        catch(Exception e )
        {
            System.out.println("Exception in Connect : "+e.toString() );
        }
    }
}

我需要专家提供有关如何执行此操作的建议/建议。

2 个答案:

答案 0 :(得分:1)

FTP协议(http://www.ietf.org/rfc/rfc959.txt)比HTTP或Telnet格式示例更复杂。您应该使用现有的FTP库:http://www.google.fr/search?q=.net+ftp+library

答案 1 :(得分:0)

您确定需要构建自己的FTP客户端吗?查看FtpWebRequest,.NET的一部分