Skip to content

Commit c597f31

Browse files
author
Conor Okus
committed
Adds initializing networking to connect peers page
1 parent 8362d47 commit c597f31

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

docs/tutorials/building-a-node-with-ldk/connect-to-peers.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,57 @@
22

33
In this section you'll learn how to join the lightning network.
44

5-
Firstly we need to have the ability to do high performance I/O operations. LDK provides default implementations for all of your networking needs. If you are using Rust, you can use our simple socket handling library `lightning_net_tokio`. In Java you can use the `NioPeerHandler` which uses Java's NIO I/O interface.
5+
Firstly we need to have the ability to do high performance I/O operations. LDK provides default implementations for initializing all of your networking needs. If you are using Rust, you can use our simple socket handling library `lightning_net_tokio`. In Java you can use the `NioPeerHandler` which uses Java's NIO I/O interface.
6+
7+
**What it's used for**: making peer connections, facilitating peer data to and from LDK
8+
9+
<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}">
10+
<template v-slot:rust>
11+
12+
```rust
13+
use lightning_net_tokio; // use LDK's sample networking module
14+
15+
let listen_port = 9735;
16+
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", listen_port))
17+
.await.unwrap()
18+
loop {
19+
let tcp_stream = listener.accept().await.unwrap().0;
20+
tokio::spawn(async move {
21+
// Use LDK's supplied networking battery to facilitate inbound
22+
// connections.
23+
lightning_net_tokio::setup_inbound(
24+
&peer_manager,
25+
tcp_stream.into_std().unwrap(),
26+
)
27+
.await;
28+
});
29+
}
30+
```
31+
32+
</template>
33+
34+
<template v-slot:java>
35+
36+
```java
37+
final NioPeerHandler peerHandler = channelManagerConstructor.nio_peer_handler;
38+
final int port = 9730;
39+
peerHandler.bind_listener(new InetSocketAddress("0.0.0.0", port));
40+
```
41+
42+
</template>
43+
44+
<template v-slot:kotlin>
45+
46+
```kotlin
47+
val nioPeerHandler = channelManagerConstructor.nio_peer_handler
48+
val port = 9777
49+
nioPeerHandler.bind_listener(InetSocketAddress("127.0.0.1", port))
50+
```
51+
52+
</template>
53+
</CodeSwitcher>
54+
55+
656

757
Connections to other peers are established with `PeerManager`. You'll need to know the pubkey and address of another node that you want as a peer. Once the connection is established and the handshake is complete, `PeerManager` will show the peer's pubkey in its list of peers.
858

0 commit comments

Comments
 (0)