Trying out the default Ping example using Libp2p rust.
Rust version: 1.70.0
System 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:?}"),
_ => {}
}
}
}
I run the first peer using cargo run and get output in terminal:
Listening on "/ip4/127.0.0.1/tcp/65332"
But on the running the other peer in another terminal using the command:
cargo run /ip4/127.0.0.1/tcp/65332
it doesn't run and provides the following output:
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)
Not sure what the issue is. It might be a system issue. I've seen many tutorials and articles running the same code and they've been able to run it successfully
Update
Meanwhile I was trying the debug(just added a few println statements) and now on running the second peer this is what pops up
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'
The code you provided here works for me on Windows, as you alluded. I'm running it with Rust 1.76 though, so you might have more success with a higher version of Rust. You can update it with
rustup update.If that doesn't work, it can be related to a network issue on your machine. That's going to be harder to find. I was also not able to reproduce your error. The first time running it (on Windows 11) I did get a message that required me to allow the program to run, so you might need to do that again. Resetting your network connection can also help (restart your pc if you haven't already tried that).