#port #check #free #select

port-selector

port-selector is a rust library that provides port availability checking, port filtering based on conditions and occupy specified ports

6 releases

0.1.6 May 12, 2022
0.1.5 May 11, 2022
0.1.3 Apr 30, 2022

#872 in Network programming

Download history 75/week @ 2023-12-13 212/week @ 2023-12-20 348/week @ 2023-12-27 196/week @ 2024-01-03 239/week @ 2024-01-10 394/week @ 2024-01-17 1072/week @ 2024-01-24 951/week @ 2024-01-31 1127/week @ 2024-02-07 761/week @ 2024-02-14 600/week @ 2024-02-21 1012/week @ 2024-02-28 1142/week @ 2024-03-06 655/week @ 2024-03-13 445/week @ 2024-03-20 509/week @ 2024-03-27

3,027 downloads per month
Used in 11 crates (6 directly)

MIT license

18KB
320 lines

Language : 🇺🇸 English | 🇨🇳 简体中文

port-selector

Build Status Crates Downloads Last Commit

Docs GitHub Actions CI LICENSE

Overview

port-selector is a rust library that provides port availability checking, port filtering based on conditions and occupy specified ports.

Installation

  1. Get the latest version -> https://crates.io/crates/port-selector

  2. Add the dependent

[dependencies]
port-selector = "0.1.5"
  1. use
use port_selector::{is_free, Port};

fn main() {
    let check_port: Port = 3000;
    println!("is_free({}) = {}", check_port, is_free(check_port));
}

Goods

type -> Port · Selector

fn -> is_free_tcp · is_free_udp · is_free · random_free_tcp_port · random_free_udp_port · random_free_port · select_from_given_port · select_free_port · take_up_tcp_port · take_up_udp_port · take_up_port · random_take_up_tcp_port · random_take_up_udp_port · random_take_up_port

Documentation

Port

u16 type alias

pub type Port = u16;

Selector

The select_free_port requires a structure passed in

pub struct Selector {
    // Check whether the port is available on TCP.
    // The default value is true.
    pub check_tcp: bool,
    // Check whether the port is available on UDP.
    // The default value is true.
    pub check_udp: bool,
    // Set the range of generated ports, default (0, 65525)
    pub port_range: (u16, u16),
    // Maximum number of random times. Default value: 100
    // If no available port number is found within the maximum random number of loops, None is returned
    pub max_random_times: u16,
}

is_free_tcp

Check whether the port is not used on TCP

pub fn is_free_udp(port: Port) -> bool

is_free_udp

Check whether the port is not used on UDP

pub fn is_free_udp(port: Port) -> bool

is_free

Check whether the port is not used on TCP and UDP

pub fn is_free(port: Port) -> bool

random_free_tcp_port

The system randomly assigns available TCP ports

pub fn random_free_tcp_port() -> Option<Port>

random_free_udp_port

The system randomly assigns available UDP ports

pub fn random_free_udp_port() -> Option<Port>

random_free_port

The system randomly assigns available TCP and UDP ports

pub fn random_free_port() -> Option<Port>

select_from_given_port

Check from given_port and return the first available port

Return if given_port is available; Otherwise given_port += 1 until the port is available

pub fn select_from_given_port(given_port: Port) -> Option<Port>

select_free_port

Gets a matching port based on the Selector parameter constraint

pub fn select_free_port(selector: Selector) -> Option<Port>

take_up_tcp_port

Occupy port on tcp

fn take_up_tcp_port(port: Port) -> bool

take_up_udp_port

Occupy port on udp

fn take_up_udp_port(port: Port) -> bool

take_up_port

Occupy port on tcp && udp

fn take_up_port(port: Port) -> bool

random_take_up_tcp_port

Randomly occupied port on tcp by the system

fn random_take_up_tcp_port() -> Port

random_take_up_udp_port

Randomly occupied port on udp by the system

fn random_take_up_udp_port() -> Port

random_take_up_port

Randomly occupy tcp && udp ports by the system

fn random_take_up_port() -> Port

Thanks

portpicker-rs

Dependencies

~315KB