类似ftp类协议的登录是怎么做的?

来源:互联网 发布:网络主播vip群福利在线 编辑:程序博客网 时间:2024/04/29 05:27
实际上就是new Socket。因为Socket在的同时会去尝试连接远程服务器

 eg:

public String[] connect(String host, int port)
   throws IllegalStateException, IOException,
   FTPIllegalReplyException, FTPException {
  synchronized (lock) {
   // Is this client already connected to any host?
   if (connected) {
    throw new IllegalStateException("Client already connected to "
      + host + " on port " + port);
   }
   // Ok, it's connection time. Let's try!
   Socket connection = null;
   try {
    // Open the connection.
    connection = connector.connectForCommunicationChannel(host, port);
    if (security == SECURITY_FTPS) {
     connection = ssl(connection, host, port);
    }
    // Open the communication channel.
    communication = new FTPCommunicationChannel(connection, pickCharset());
    for (Iterator i = communicationListeners.iterator(); i.hasNext();) {
     communication.addCommunicationListener((FTPCommunicationListener) i.next());
    }
    // Welcome message.
    FTPReply wm = communication.readFTPReply();
    // Does this reply mean "ok"?
    if (!wm.isSuccessCode()) {
     // Mmmmm... it seems no!
     throw new FTPException(wm);
    }
    // Flag this object as connected to the remote host.
    this.connected = true;
    this.authenticated = false;
    this.parser = null;
    this.host = host;
    this.port = port;
    this.username = null;
    this.password = null;
    this.utf8Supported = false;
    this.restSupported = false;
    this.mlsdSupported = false;
    this.modezSupported = false;
    this.dataChannelEncrypted = false;
    // Returns the welcome message.
    return wm.getMessages();
   } catch (IOException e) {
    // D'oh!
    throw e;
   } finally {
    // If connection has failed...
    if (!connected) {
     if (connection != null) {
      // Close the connection, 'cause it should be open.
      try {
       connection.close();
      } catch (Throwable t) {
       ;
      }
     }
    }
   }
  }
 }