DoumanAsh / lazy-socket.rs

Low level Raw Socket API. Deprecated

Home Page:https://crates.io/crates/lazy-socket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lazy-socket

Build status Build Status Crates.io Docs.rs

The library serves as thin wrapper over socket API. It provides necessary minimum amount of safety and easy to use.

Obsolete kinda

It seems I wouldn't need this library anymore as there is socket2 by alexcrichton.

He is definitely more trustworthy and my library might as well be forgotten. So go socket2 instead.

Examples

Create TCP socket and connect in non-blocking mode.

extern crate lazy_socket;

use std::net;
use std::str::FromStr;

use lazy_socket::raw::{
    Socket,
    Family,
    Protocol,
    Type,
    select
};

fn main() {
    let timeout = 1000;
    let socket = match Socket::new(Family::IPv4, Type::STREAM, Protocol::TCP) {
        Ok(socket) => socket,
        Err(error) => {
            println!("Couldn't open socket. Erro: {}", error);
            return;
        }
    };

    let dest = net::SocketAddr::from_str("192.168.0.1:80").unwrap();

    let _ = socket.set_blocking(false);
    let _ = socket.connect(&dest);
    match select(&[], &[&socket], &[&socket], Some(timeout)) {
          Ok(_) => println!("Connected!"),
          Err(error) => println!("Failed to connect. Error:{}", error)
    }
}

About

Low level Raw Socket API. Deprecated

https://crates.io/crates/lazy-socket

License:MIT License


Languages

Language:Rust 100.0%