Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Unix sockets | Rust by Example Introduction 1. Hello World 2. Formatted print 3. Literals and operators 4. Variables 4.1. Mutability 4.2. Scope and shadowing 4.3. Declare first 5. Types 5.1. Casting 5.2. Literals 5.3. Inference 5.4. Alias 6. Expressions 7. Flow Control 7.1. if/else 7.2. loop 7.2.1. Nesting and labels 7.3. while 7.4. for and range 7.5. match 7.5.1. Destructuring 7.5.1.1. tuples 7.5.1.2. enums 7.5.1.3. pointers/ref 7.5.1.4. structs 7.5.2. Guards 7.5.3. Binding 7.6. if let 7.7. while let 8. Functions 8.1. Unused 9. Modules 9.1. Visibility 9.2. The `use` import 9.3. `super` and `self` 9.4. File hierarchy 10. Crates 10.1. Library 10.2. `extern crate` 11. Attributes 11.1. Crates 11.2. `cfg` 11.2.1. Custom 12. Tuples 13. Structures 13.1. Visibility 14. Generics 14.1. Implementation 14.2. Phantom types 14.2.1. Unit conversions 15. Box, stack and heap 16. RAII 17. Ownership and moves 17.1. Mutability 18. Borrowing 18.1. Mutability 18.2. Freezing 18.3. Aliasing 18.4. The ref pattern 19. Lifetimes 19.1. The borrow checker 19.2. Functions 19.3. Structs 20. Global constants 21. Methods 22. Enums 22.1. C-like 23. `panic!` 24. `Option` 25. Arrays and Slices 26. Traits 26.1. Derive 27. Operator Overloading 28. Bounds 29. Drop 30. Iterators 31. Closures 32. Higher Order Functions 33. Vectors 34. Strings 35. Clone 36. Threads 37. Channels 38. Timers 39. Unix sockets 40. `Result` 40.1. `try!` 41. Path 42. File I/O 42.1. `open` 42.2. `create` 43. Child processes 43.1. Pipes 43.2. Wait 44. Filesystem Operations 45. Benchmarking 46. Comments 46.1. Doc Comments 47. Foreign Function Interface 48. macro_rules! 48.1. Designators 48.2. Overload 48.3. Repeat 48.4. DRY 49. Program arguments 49.1. Argument parsing 49.2. `getopts` 50. SIMD 51. Testing 52. Unsafe operations 53. Formatting 54. HashMap 54.1. Alternate/custom key types 54.2. HashSet Published using GitBook A A Serif Sans White Sepia Night Twitter Google Facebook Weibo Instapaper Rust by Example 39 Unix sockets Inter-Process Communication (IPC) for client-server applications can be accomplished using UNIX sockets. Both client and server need to use the same path for the socket. // common.rs pub static SOCKET_PATH: &'static str = "loopback-socket"; The client program: // client.rs #![feature(io)] #![feature(core)] #![feature(path)] #![feature(env)] use std::env; use common::SOCKET_PATH; use std::old_io::net::pipe::UnixStream; mod common; fn main() { // `args` returns the arguments passed to the program let args: Vec = env::args().map(|x| x.to_string()) .collect(); let socket = Path::new(SOCKET_PATH); // First argument is the message to be sent let message = match args.as_slice() { [_, ref message] => message.as_slice(), _ => panic!("wrong number of arguments"), }; // Connect to socket let mut stream = match UnixStream::connect(&socket) { Err(_) => panic!("server is not running"), Ok(stream) => stream, }; // Send message match stream.write_str(message) { Err(_) => panic!("couldn't send message"), Ok(_) => {} } } The server program: // server.rs #![feature(io)] #![feature(path)] use common::SOCKET_PATH; use std::old_io::fs; use std::old_io::fs::PathExtensions; use std::old_io::net::pipe::UnixListener; use std::old_io::{Acceptor,Listener}; mod common; fn main() { let socket = Path::new(SOCKET_PATH); // Delete old socket if necessary if socket.exists() { fs::unlink(&socket).unwrap(); } // Bind to socket let stream = match UnixListener::bind(&socket) { Err(_) => panic!("failed to bind socket"), Ok(stream) => stream, }; println!("Server started, waiting for clients"); // Iterate over clients, blocks if no client available for mut client in stream.listen().incoming() { println!("Client said: {}", client.read_to_string().unwrap()); } } Let's test the programs $ rustc client.rs; rustc server.rs # Terminal 1 $ ./server Server started, waiting for clients # Terminal 2 $ ./client hello # Terminal 1 Server started, waiting for clients Client said: hello