@@ -14,52 +14,61 @@ fn crate_task(input: i32) {
1414 ) ;
1515}
1616
17- pub fn start_server < A : ToSocketAddrs > (
18- addr : A ,
19- server_finished : Arc < ( Mutex < bool > , Condvar ) > ,
20- ) -> std:: io:: Result < ( ) > {
21- let listener = TcpListener :: bind ( addr) ?;
17+ pub fn start_server < A : ToSocketAddrs > ( addr : A , server_finished : Arc < ( Mutex < bool > , Condvar ) > ) {
18+ let listener = TcpListener :: bind ( addr) . expect ( "start server failed" ) ;
2219 for stream in listener. incoming ( ) {
23- let mut stream = stream? ;
20+ let mut socket = stream. expect ( "accept new connection failed" ) ;
2421 let mut buffer1 = [ 0 ; 256 ] ;
2522 for _ in 0 ..3 {
26- assert_eq ! ( 12 , stream . read( & mut buffer1) ? ) ;
23+ assert_eq ! ( 12 , socket . read( & mut buffer1) . expect ( "recv failed" ) ) ;
2724 println ! ( "Server Received: {}" , String :: from_utf8_lossy( & buffer1) ) ;
28- assert_eq ! ( 256 , stream . write( & buffer1) ? ) ;
25+ assert_eq ! ( 256 , socket . write( & buffer1) . expect ( "send failed" ) ) ;
2926 println ! ( "Server Send" ) ;
3027 }
3128 let mut buffer2 = [ 0 ; 256 ] ;
3229 for _ in 0 ..3 {
3330 let mut buffers = [ IoSliceMut :: new ( & mut buffer1) , IoSliceMut :: new ( & mut buffer2) ] ;
34- assert_eq ! ( 26 , stream. read_vectored( & mut buffers) ?) ;
31+ assert_eq ! (
32+ 26 ,
33+ socket. read_vectored( & mut buffers) . expect( "readv failed" )
34+ ) ;
3535 println ! (
3636 "Server Received Multiple: {}{}" ,
3737 String :: from_utf8_lossy( & buffer1) ,
3838 String :: from_utf8_lossy( & buffer2)
3939 ) ;
4040 let responses = [ IoSlice :: new ( & buffer1) , IoSlice :: new ( & buffer2) ] ;
41- assert_eq ! ( 512 , stream. write_vectored( & responses) ?) ;
41+ assert_eq ! (
42+ 512 ,
43+ socket. write_vectored( & responses) . expect( "writev failed" )
44+ ) ;
4245 println ! ( "Server Send Multiple" ) ;
4346 }
4447 println ! ( "Server Shutdown Write" ) ;
45- stream . shutdown ( Shutdown :: Write ) . map ( | ( ) | {
48+ if socket . shutdown ( Shutdown :: Write ) . is_ok ( ) {
4649 println ! ( "Server Closed Connection" ) ;
47- } ) ?;
48- let ( lock, cvar) = & * server_finished;
49- let mut pending = lock. lock ( ) . unwrap ( ) ;
50- * pending = false ;
51- cvar. notify_one ( ) ;
50+ let ( lock, cvar) = & * server_finished;
51+ let mut pending = lock. lock ( ) . unwrap ( ) ;
52+ * pending = false ;
53+ cvar. notify_one ( ) ;
54+ println ! ( "Server Closed" ) ;
55+ return ;
56+ }
5257 }
53- Ok ( ( ) )
5458}
5559
56- pub fn start_client < A : ToSocketAddrs > ( addr : A ) -> std :: io :: Result < ( ) > {
57- let mut stream = connect_timeout ( addr, Duration :: from_secs ( 3 ) ) ? ;
60+ pub fn start_client < A : ToSocketAddrs > ( addr : A ) {
61+ let mut stream = connect_timeout ( addr, Duration :: from_secs ( 3 ) ) . expect ( "connect failed" ) ;
5862 let mut buffer1 = [ 0 ; 256 ] ;
5963 for i in 0 ..3 {
60- assert_eq ! ( 12 , stream. write( format!( "RequestPart{i}" ) . as_ref( ) ) ?) ;
64+ assert_eq ! (
65+ 12 ,
66+ stream
67+ . write( format!( "RequestPart{i}" ) . as_ref( ) )
68+ . expect( "send failed" )
69+ ) ;
6170 println ! ( "Client Send" ) ;
62- assert_eq ! ( 256 , stream. read( & mut buffer1) ? ) ;
71+ assert_eq ! ( 256 , stream. read( & mut buffer1) . expect ( "recv failed" ) ) ;
6372 println ! ( "Client Received: {}" , String :: from_utf8_lossy( & buffer1) ) ;
6473 }
6574 let mut buffer2 = [ 0 ; 256 ] ;
@@ -70,20 +79,22 @@ pub fn start_client<A: ToSocketAddrs>(addr: A) -> std::io::Result<()> {
7079 IoSlice :: new ( request1. as_ref ( ) ) ,
7180 IoSlice :: new ( request2. as_ref ( ) ) ,
7281 ] ;
73- assert_eq ! ( 26 , stream. write_vectored( & requests) ? ) ;
82+ assert_eq ! ( 26 , stream. write_vectored( & requests) . expect ( "writev failed" ) ) ;
7483 println ! ( "Client Send Multiple" ) ;
7584 let mut buffers = [ IoSliceMut :: new ( & mut buffer1) , IoSliceMut :: new ( & mut buffer2) ] ;
76- assert_eq ! ( 512 , stream. read_vectored( & mut buffers) ?) ;
85+ assert_eq ! (
86+ 512 ,
87+ stream. read_vectored( & mut buffers) . expect( "readv failed" )
88+ ) ;
7789 println ! (
7890 "Client Received Multiple: {}{}" ,
7991 String :: from_utf8_lossy( & buffer1) ,
8092 String :: from_utf8_lossy( & buffer2)
8193 ) ;
8294 }
8395 println ! ( "Client Shutdown Write" ) ;
84- stream. shutdown ( Shutdown :: Write ) . map ( |( ) | {
85- println ! ( "Client Closed" ) ;
86- } )
96+ stream. shutdown ( Shutdown :: Write ) . expect ( "shutdown failed" ) ;
97+ println ! ( "Client Closed" ) ;
8798}
8899
89100fn connect_timeout < A : ToSocketAddrs > ( addr : A , timeout : Duration ) -> std:: io:: Result < TcpStream > {
@@ -111,7 +122,7 @@ pub fn crate_server(
111122 crate_task ( 1 ) ;
112123 let mut data: [ u8 ; 512 ] = unsafe { std:: mem:: zeroed ( ) } ;
113124 data[ 511 ] = b'\n' ;
114- let listener = TcpListener :: bind ( format ! ( " 127.0.0.1:{ port}" ) )
125+ let listener = TcpListener :: bind ( ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , port) )
115126 . unwrap_or_else ( |_| panic ! ( "bind to 127.0.0.1:{port} failed !" ) ) ;
116127 server_started. store ( true , Ordering :: Release ) ;
117128 //invoke by libc::accept
@@ -205,7 +216,7 @@ pub fn crate_co_server(
205216 crate_task ( 11 ) ;
206217 let mut data: [ u8 ; 512 ] = unsafe { std:: mem:: zeroed ( ) } ;
207218 data[ 511 ] = b'\n' ;
208- let listener = TcpListener :: bind ( format ! ( " 127.0.0.1:{ port}" ) )
219+ let listener = TcpListener :: bind ( ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , port) )
209220 . unwrap_or_else ( |_| panic ! ( "bind to 127.0.0.1:{port} failed !" ) ) ;
210221 server_started. store ( true , Ordering :: Release ) ;
211222 //invoke by libc::accept
0 commit comments