chore: rename
This commit is contained in:
5
tcp/tcp_client/Cargo.lock
generated
Normal file
5
tcp/tcp_client/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "udp_client"
|
||||
version = "0.1.0"
|
||||
9
tcp/tcp_client/Cargo.toml
Normal file
9
tcp/tcp_client/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "udp_client"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
34
tcp/tcp_client/src/main.rs
Normal file
34
tcp/tcp_client/src/main.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::net::TcpStream;
|
||||
use std::io::{ Read, Write };
|
||||
use std::str::from_utf8;
|
||||
|
||||
fn main() {
|
||||
match TcpStream::connect("localhost:3333") {
|
||||
Ok(mut stream) => {
|
||||
println!("Successfully connected to server in port 3333");
|
||||
|
||||
let msg = b"Hello!";
|
||||
stream.write(msg).unwrap();
|
||||
println!("Sent Hello, awaiting reply...");
|
||||
|
||||
let mut data = [0_u8; 6]; // using 6 byte buffer
|
||||
match stream.read_exact(&mut data) {
|
||||
Ok(_) => {
|
||||
if &data == msg {
|
||||
println!("Reply is ok!");
|
||||
} else {
|
||||
let text = from_utf8(&data).unwrap();
|
||||
println!("Unexpected reply: {}", text);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Failed to receive data: {}", e);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Failed to connect: {}", e);
|
||||
}
|
||||
}
|
||||
println!("Terminated.");
|
||||
}
|
||||
Reference in New Issue
Block a user