continuously_echo
This commit is contained in:
parent
f9eeea892e
commit
86c5e0c788
1 changed files with 50 additions and 23 deletions
73
src/main.rs
73
src/main.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write, self};
|
||||||
use std::env;
|
use std::{env, thread};
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
enum EnvArg
|
enum EnvArg
|
||||||
|
@ -17,18 +17,26 @@ impl EnvArg
|
||||||
Ok(mut socket) =>
|
Ok(mut socket) =>
|
||||||
{
|
{
|
||||||
println!("Connected");
|
println!("Connected");
|
||||||
socket.write(b"Hello").unwrap();
|
let socket_clone = socket.try_clone().expect("Cloning Failed");
|
||||||
let mut data = [0 as u8; 1024];
|
thread::spawn(move || {Self::c_send(&socket_clone)});
|
||||||
match socket.read(&mut data)
|
let stay = true;
|
||||||
|
while stay
|
||||||
{
|
{
|
||||||
Ok(_) =>
|
let mut data = [0 as u8; 1024];
|
||||||
|
match socket.read(&mut data)
|
||||||
{
|
{
|
||||||
println!("{}", from_utf8(&data).unwrap());
|
Ok(_) =>
|
||||||
}
|
{
|
||||||
Err(e) =>
|
if data[0] != 0
|
||||||
{
|
{
|
||||||
println!("Failed to receive data: {}", e);
|
println!("{}", from_utf8(&data).unwrap());
|
||||||
return false;
|
}
|
||||||
|
}
|
||||||
|
Err(e) =>
|
||||||
|
{
|
||||||
|
println!("Failed to receive data: {}", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +48,14 @@ impl EnvArg
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
fn c_send(mut socket:&TcpStream)
|
||||||
|
{
|
||||||
|
let stay = true;
|
||||||
|
while stay
|
||||||
|
{
|
||||||
|
socket.write(take_string().as_bytes()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn s_connect() -> bool
|
fn s_connect() -> bool
|
||||||
{
|
{
|
||||||
|
@ -50,22 +66,26 @@ impl EnvArg
|
||||||
{
|
{
|
||||||
Ok(mut stream) =>
|
Ok(mut stream) =>
|
||||||
{
|
{
|
||||||
let mut data = [0 as u8; 1024];
|
let stay = true;
|
||||||
match stream.read(&mut data)
|
while stay
|
||||||
{
|
{
|
||||||
Ok(a) =>
|
let mut data = [0 as u8; 1024];
|
||||||
|
match stream.read(&mut data)
|
||||||
{
|
{
|
||||||
if a == 0
|
Ok(a) =>
|
||||||
{
|
{
|
||||||
println!("Connection Closed");
|
if a == 0
|
||||||
|
{
|
||||||
|
println!("Connection Closed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
println!("{}", stream.write(&data).unwrap());
|
||||||
|
}
|
||||||
|
Err(e) =>
|
||||||
|
{
|
||||||
|
println!("Failed to Read: {}", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
println!("{}", stream.write(&data).unwrap());
|
|
||||||
}
|
|
||||||
Err(e) =>
|
|
||||||
{
|
|
||||||
println!("Failed to Read: {}", e);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,6 +126,13 @@ fn take_arg() -> EnvArg
|
||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn take_string() -> String
|
||||||
|
{
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input).expect("Failed to Read from Console");
|
||||||
|
input
|
||||||
|
}
|
||||||
|
|
||||||
fn client()
|
fn client()
|
||||||
{
|
{
|
||||||
println!("Client");
|
println!("Client");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue