Class TcpEndpoint
- All Implemented Interfaces:
Cloneable
TcpEndpoint represents a remote peer's address and port for TCP connections.
In BitTorrent, peers discovered via DHT, trackers, or PEX are identified by their
IPv4 or IPv6 address combined with a TCP port where they're listening for connections.
Understanding TCP Endpoints:
TCP endpoints are used to represent peer locations throughout the BitTorrent protocol:
- Peer Discovery: Trackers and DHT return lists of TcpEndpoints for connecting
- Connection: SessionManager connects to TcpEndpoints to establish peer relationships
- Peer Info: PeerInfo objects contain TcpEndpoints showing connected peers
- Address Format: Supports both IPv4 (192.168.1.1:6881) and IPv6 ([::1]:6881)
Creating TCP Endpoints:
// From IPv4 address and port
TcpEndpoint peer1 = new TcpEndpoint("192.168.1.100", 6881);
System.out.println(peer1); // Output: 192.168.1.100:6881
// From IPv6 address (automatically formatted with brackets)
TcpEndpoint peer2 = new TcpEndpoint("2001:db8::1", 6881);
System.out.println(peer2); // Output: [2001:db8::1]:6881
// From localhost
TcpEndpoint local = new TcpEndpoint("127.0.0.1", 6882);
System.out.println(local); // Output: 127.0.0.1:6882
// From Address object (more efficient for complex operations)
Address addr = new Address("10.0.0.1");
TcpEndpoint peer3 = new TcpEndpoint(addr, 6883);
Querying Endpoints:
TcpEndpoint peer = new TcpEndpoint("tracker.example.com", 6881);
// Get the address component
Address addr = peer.address();
boolean isV4 = addr.isV4(); // true for IPv4
boolean isV6 = addr.isV6(); // true for IPv6
String ipString = addr.toString(); // "192.168.1.100"
// Get the port
int port = peer.port(); // 6881
System.out.println("Port: " + port);
// Get full string representation (with proper IPv6 bracket formatting)
String fullAddr = peer.toString(); // "192.168.1.100:6881"
Usage in Peer Discovery:
SessionManager sm = new SessionManager();
sm.start();
// DHT peer discovery returns list of TcpEndpoints
Sha1Hash infoHash = new Sha1Hash("d8e8fca2dc0f896fd7cb4cb0031ba249");
ArrayList<TcpEndpoint> peers = sm.dhtGetPeers(infoHash, 20);
for (TcpEndpoint peer : peers) {
System.out.println("Found peer: " + peer.toString());
// Connect to: peer.address() on peer.port()
}
Connected Peers from Torrent:
TorrentHandle th = sm.find(infoHash);
TorrentStatus status = th.status();
// Get list of connected peers
java.util.List<PeerInfo> peers = th.peers();
for (PeerInfo peerInfo : peers) {
TcpEndpoint endpoint = peerInfo.endpoint();
System.out.println("Connected to: " + endpoint);
System.out.println(" Download speed: " + peerInfo.downSpeed() + " B/s");
System.out.println(" Upload speed: " + peerInfo.upSpeed() + " B/s");
}
Error Handling:
Creating a TcpEndpoint from an invalid IP address throws IllegalArgumentException:
try {
TcpEndpoint invalid = new TcpEndpoint("not.a.valid.ip", 6881);
} catch (IllegalArgumentException e) {
System.err.println("Invalid IP address: " + e.getMessage());
}
TCP Endpoint vs UDP Endpoint:
TcpEndpoint is for BitTorrent peer connections (TCP port).
UdpEndpoint is for DHT and uTP connections (UDP port).
They may use the same or different ports on the same host.
Performance Notes:
- Creating endpoints from String requires IP parsing; reuse Address objects when possible
- Endpoints are lightweight; safe to create and discard frequently
- String constructor performs validation; invalid IPs throw immediately
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTcpEndpoint(Address address, int port) TcpEndpoint(com.frostwire.jlibtorrent.swig.tcp_endpoint endp) TcpEndpoint(String ip, int port) -
Method Summary
-
Constructor Details
-
TcpEndpoint
public TcpEndpoint(com.frostwire.jlibtorrent.swig.tcp_endpoint endp) - Parameters:
endp- the native object
-
TcpEndpoint
public TcpEndpoint() -
TcpEndpoint
- Parameters:
address- the addressport- the port
-
TcpEndpoint
- Parameters:
ip- the address as an IPport- the port
-
-
Method Details