psocket

Crates.iopsocket
lib.rspsocket
version0.1.5
sourcesrc
created_at2017-10-25 09:15:18.537213
updated_at2018-04-13 08:02:34.72548
descriptionthe socket for Rust
homepage
repositoryhttps://github.com/tickbh/psocket-rs
max_upload_size
id36845
size296,957
问蒙框架服务 (tickbh)

documentation

https://docs.rs/psocket

README

socket for rust

Build Status Crates.io

A Rust library for socket.

Usage

Add this to your Cargo.toml:

[dependencies]
psocket = "0.1"

and this to your crate root:

extern crate psocket;

How to use

use psocket::TcpSocket;
use std::io::prelude::*;
use std::time::{Duration, Instant};
use std::io::ErrorKind;

let listener = TcpSocket::bind("127.0.0.1:1234").unwrap();
let addr = listener.local_addr().unwrap();

let mut stream = TcpSocket::connect(&("localhost", addr.port())).expect("connect error");
stream.set_read_timeout(Some(Duration::from_millis(1000))).expect("set read timeout error");

let mut other_end = listener.accept().unwrap().0;
other_end.write_all(b"hello world").expect("write error");

let mut buf = [0; 11];
stream.read(&mut buf).expect("read error");
assert_eq!(b"hello world", &buf[..]);

let start = Instant::now();
let kind = stream.read(&mut buf).err().expect("expected error").kind();
assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut);
assert!(start.elapsed() > Duration::from_millis(400));
drop(listener);

Diff with Rust TcpStream

provide more fuction about socket

// The func connect remote host asyn
pub fn connect_asyn(addr: &SocketAddr) -> io::Result<TcpSocket>;
// Get the socket fd
pub fn as_raw_socket(&self) -> SOCKET;
// Moves this TCP stream into or out of liner mode.
pub fn set_liner(&self, enable: bool, time: u16) -> io::Result<()>;
// The Method is set stream recv size kernel cache size.
pub fn set_recv_size(&self, size: u32) -> io::Result<()>;
// The Method is set stream send size kernel cache size.
pub fn set_send_size(&self, size: u32) -> io::Result<()>;
// The Method is set tcp stream SO_REUSEADDR.
pub fn set_reuse_addr(&self) -> io::Result<()>;
/// Provide func dtor the object but will not close socket fd. 
pub fn unlink(self) -> io::Result<()>;

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 14

cargo fmt