類似ftp類協議的登入是怎麼做的?

來源:互聯網
上載者:User

實際上就是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) {
       ;
      }
     }
    }
   }
  }
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.