Apache Commons Net

repository·master·Indexed 43 days ago

https://github.com/apache/commons-net

A Java library providing a collection of network utilities and protocol implementations, including support for FTP, SMTP, POP3, Telnet, NNTP, NTP, Echo, Finger, and Whois. Version 3.13.0 provides tools for managing network sessions, such as Telnet option handlers and notification handlers.

Tokens
816
Snippets
4
Records
7
Agent score
5%

What's inside Apache Commons Net

  1. Overview of Apache Commons Net

    master

    Apache Commons Net is a Java library providing a collection of network utilities and protocol implementations. It is designed to help developers implement various network protocols within their applications.

    Supported Protocols:

    • Echo
    • Finger
    • FTP
    • NNTP
    • NTP
    • POP3(S)
    • SMTP(S)
    • Telnet
    • Whois
  2. Install Apache Commons Net via Maven

    master

    To use Apache Commons Net in your Java project, add the following dependency to your pom.xml file. Ensure you use the latest version available in the central Maven repositories.

    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>3.13.0</version>
    </dependency>
  3. Monitor Telnet session activity with a spy stream

    master

    You can observe the data flowing through a Telnet session by registering an OutputStream as a spy stream. This allows you to capture what is being sent and received.

    • Use _registerSpyStream(OutputStream spystream) to start spying.
    • Use _stopSpyStream() to stop spying.

    Note: The spy stream implementation handles character transformations (like adding \r before \n) and avoids echoing certain characters during specific negotiation states.

    OutputStream spy = new FileOutputStream("session_log.txt");
    telnet._registerSpyStream(spy);
  4. Register a TelnetOptionHandler to manage protocol options

    master

    To handle specific Telnet options (like echo or negotiation sub-protocols), you can register a TelnetOptionHandler using addOptionHandler(TelnetOptionHandler opthand).

    If the client is already connected, the handler will immediately trigger the appropriate negotiation requests (e.g., requestWill or requestDo) based on the handler's configuration.

    To remove a handler, use deleteOptionHandler(int optcode) with the corresponding option code.

  5. Send an Are You There (AYT) sequence

    master

    The _sendAYT(Duration timeout) method sends an Are You There (AYT) sequence to the remote server and waits for a response within the specified duration. This is useful for checking if a connection is still alive without sending application-level data.

    Returns true if an AYT response was received, false otherwise.

    // Send AYT and wait up to 5 seconds for a response
    boolean alive = telnet._sendAYT(Duration.ofSeconds(5));
  6. Handle Telnet negotiation notifications

    master

    You can receive notifications about Telnet option negotiation commands (such as RECEIVED_COMMAND, RECEIVED_DO, RECEIVED_DONT, or RECEIVED_WILL) by implementing the TelnetNotificationHandler interface and registering it with the client.

    • Use registerNotifHandler(TelnetNotificationHandler notifhand) to attach a handler.
    • Use unregisterNotifHandler() to remove it.