socket_addr.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. use wasmedge_wasi_socket::{SocketAddr, ToSocketAddrs};
  2. fn main() -> std::io::Result<()> {
  3. // ip + port
  4. let mut addr = ("127.0.0.1", 3000).to_socket_addrs().unwrap();
  5. assert_eq!(addr.next(), Some(SocketAddr::from(([127, 0, 0, 1], 3000))));
  6. assert!(addr.next().is_none());
  7. // ip
  8. let mut addr = ("127.0.0.1:3000").to_socket_addrs().unwrap();
  9. assert_eq!(addr.next(), Some(SocketAddr::from(([127, 0, 0, 1], 3000))));
  10. assert!(addr.next().is_none());
  11. // from slice
  12. let addr1 = SocketAddr::from(([0, 0, 0, 0], 80));
  13. let addr2 = SocketAddr::from(([127, 0, 0, 1], 443));
  14. let addrs = vec![addr1, addr2];
  15. let mut addrs_iter = (&addrs[..]).to_socket_addrs().unwrap();
  16. assert_eq!(Some(addr1), addrs_iter.next());
  17. assert_eq!(Some(addr2), addrs_iter.next());
  18. assert!(addrs_iter.next().is_none());
  19. // missing port
  20. let err = "127.0.0.1".to_socket_addrs().unwrap_err();
  21. assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
  22. // unable to resolve
  23. assert!("foo:443".to_socket_addrs().is_err());
  24. // dns
  25. let mut addr = ("localhost:3000").to_socket_addrs().unwrap();
  26. assert_eq!(addr.next(), Some(SocketAddr::from(([127, 0, 0, 1], 3000))));
  27. Ok(())
  28. }