Skip to content

Commit 3cd94b0

Browse files
committed
Support both ipv4 and ipv6
1 parent 825cafa commit 3cd94b0

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/shared_socket.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22

33
use log::debug;
44
use socket2::{Domain, Protocol, Socket, Type};
5-
use std::net::SocketAddr;
5+
use std::net::{IpAddr, SocketAddr};
66

77
#[pyclass]
88
#[derive(Debug)]
@@ -13,9 +13,14 @@ pub struct SocketHeld {
1313
#[pymethods]
1414
impl SocketHeld {
1515
#[new]
16-
pub fn new(address: String, port: i32) -> PyResult<SocketHeld> {
17-
let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?;
18-
let address: SocketAddr = format!("{}:{}", address, port).parse()?;
16+
pub fn new(ip: String, port: u16) -> PyResult<SocketHeld> {
17+
let ip: IpAddr = ip.parse()?;
18+
let socket = if ip.is_ipv4() {
19+
Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?
20+
} else {
21+
Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP))?
22+
};
23+
let address = SocketAddr::new(ip, port);
1924
debug!("{}", address);
2025
// reuse port is not available on windows
2126
#[cfg(not(target_os = "windows"))]

0 commit comments

Comments
 (0)