fnhandle_client(mutstream: TcpStream)-> io::Result<()>{letmutbuf=[0;512];for_in0..1000{letbytes_read=stream.read(&mutbuf)?;ifbytes_read==0{returnOk(());}stream.write(&buf[..bytes_read])?;thread::sleep(Duration::from_secs(1));}Ok(())}fnmain()-> io::Result<()>{letlistener=TcpListener::bind("127.0.0.1:8080")?;println!("TCP Server is running...");forstreaminlistener.incoming(){letstream=stream.expect("failed");thread::spawn(move||{handle_client(stream).unwrap_or_else(|error|eprintln!("{}",error));});}Ok(())}
fnmain()-> io::Result<()>{letmutstream=TcpStream::connect("127.0.0.1:8080")?;println!("TCP Client is running...");for_in0..10{letmutinput=String::new();io::stdin().read_line(&mutinput).expect("Failed to read");stream.write(input.as_bytes()).expect("Failed to write");letmutreader=BufReader::new(&stream);letmutbuffer=vec![];reader.read_until(b'\n',&mutbuffer).expect("Failed to read");println!("Read from server: {}",std::str::from_utf8(&buffer).expect("Failed to accept"));}Ok(())}
fnmain()-> io::Result<()>{letsocket=UdpSocket::bind("127.0.0.1:8080")?;println!("UDP socket is running...");loop{letmutbuf=[0;1500];let(amt,src)=socket.recv_from(&mutbuf)?;letbuf=&mutbuf[..amt];buf.reverse();socket.send_to(buf,src)?;}}