使用 Libp2p rust 尝试默认的 Ping 示例。
Rust 版本:1.70.0
系统Windows 10
use futures::prelude::*;
use libp2p::{noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr};
use std::{error::Error, time::Duration};
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_behaviour(|_| ping::Behaviour::default())?
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
.build();
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
if let Some(addr) = std::env::args().nth(1) {
let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
println!("Dialed {addr}")
}
loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
SwarmEvent::Behaviour(event) => println!("{event:?}"),
_ => {}
}
}
}
我使用货物运行运行第一个对等点并在终端中获取输出:
Listening on "/ip4/127.0.0.1/tcp/65332"
但是在另一个终端中使用以下命令运行另一个对等点:
cargo run /ip4/127.0.0.1/tcp/65332
它不会运行并提供以下输出:
Running target\debug\p2pRecipe.exe C:/Program Files/Git/ip4/127.0.0.1/tcp/65332 Error: InvalidMultiaddr error: process didn't exit successfully: target\debug\p2pRecipe.exe C:/Program Files/Git/ip4/127.0.0.1/tcp/65332' (exit code: 1)
不确定问题是什么。 这可能是系统问题。我看过很多运行相同代码的教程和文章,并且他们已经能够成功运行它
更新
同时,我正在尝试调试(只是添加了一些 println 语句),现在运行第二个对等点时,会弹出以下内容
error: linking with link.exe failed: exit code: 1104
note: LINK : fatal error LNK1104: cannot open file 'D:\VSCode Rust\p2pRecipe\target\debug\deps\p2pRecipe.exe'
正如您所提到的,您在此处提供的代码适用于我在 Windows 上的情况。不过,我使用 Rust 1.76 运行它,因此使用更高版本的 Rust 可能会取得更大的成功。您可以使用
rustup update
进行更新。
如果不起作用,可能与您计算机上的网络问题有关。那就更难找到了。我也无法重现您的错误。第一次运行它(在 Windows 11 上)时,我确实收到一条消息,要求我允许该程序运行,因此您可能需要再次执行此操作。重置网络连接也有帮助(如果您还没有尝试过,请重新启动您的电脑)。
同时,我正在尝试调试(只是添加了一些 println 语句),现在运行第二个对等点时,会弹出以下内容
error: linking with link.exe failed: exit code: 1104
note: LINK : fatal error LNK1104: cannot open file 'D:\VSCode Rust\p2pRecipe\target\debug\deps\p2pRecipe.exe'