create rust socket server
This commit is contained in:
@@ -1,3 +1,14 @@
|
|||||||
|
use std::net::TcpStream;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") {
|
||||||
}
|
println!("Connected to the server!");
|
||||||
|
let ip_result = stream.peer_addr();
|
||||||
|
if ip_result.is_ok() {
|
||||||
|
println!("{}", ip_result.unwrap());
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Couldn't connect to server...");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,29 @@
|
|||||||
fn main() {
|
use std::io::prelude::*;
|
||||||
println!("Hello, world!");
|
use std::net::{TcpListener, TcpStream};
|
||||||
|
|
||||||
|
const IP: &str = "127.0.0.1:8080";
|
||||||
|
|
||||||
|
// https://doc.rust-lang.org/std/net/struct.TcpListener.html
|
||||||
|
fn handle_client(mut stream: TcpStream) {
|
||||||
|
let mut buffer = [0; 1024];
|
||||||
|
|
||||||
|
let read_result = stream.read(&mut buffer);
|
||||||
|
println!("read_result: {}", read_result.is_ok());
|
||||||
|
|
||||||
|
let shutdown_result = stream.shutdown(std::net::Shutdown::Both);
|
||||||
|
|
||||||
|
println!("shutdown_result: {}", shutdown_result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://doc.rust-lang.org/std/net/struct.TcpListener.html
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let listener = TcpListener::bind(IP)?;
|
||||||
|
|
||||||
|
println!("Listening on {}", IP);
|
||||||
|
|
||||||
|
// accept connections and process them serially
|
||||||
|
for stream in listener.incoming() {
|
||||||
|
handle_client(stream?);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user