├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── Screenshot 2024-09-04 060752.png └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "multi-threaded-server" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "multi-threaded-server" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Yash Kamble 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multithreaded HTTP Server in Rust 2 | 3 | This is a simple multithreaded HTTP server written in Rust. It handles multiple client requests concurrently using threads. The server listens on `127.0.0.1:6969` and responds with a basic "Hello, World!" message to any incoming HTTP request. 4 | 5 | The server will start listening on 127.0.0.1:6969. 6 | Testing the Server 7 | 8 | You can test the server by navigating to http://127.0.0.1:6969 in your web browser. You should see a "Hello, World!" message. 9 | Checking if the Server is Multithreaded 10 | 11 | To verify that the server is handling requests in a multithreaded manner: 12 | 13 | Send Multiple Requests Simultaneously: 14 | Open multiple tabs in your web browser pointing to http://127.0.0.1:6969. 15 | Use a command-line tool like curl to send multiple requests simultaneously: 16 | 17 | 18 | ``` 19 | curl http://127.0.0.1:6969 & 20 | curl http://127.0.0.1:6969 & 21 | curl http://127.0.0.1:6969 & 22 | ``` 23 | Use a load-testing tool like ApacheBench (ab): 24 | 25 | 26 | ``` 27 | ab -n 10 -c 5 http://127.0.0.1:6969/ 28 | ``` 29 | Observe the Terminal Output: 30 | 31 | The server will print the thread IDs handling each request. 32 | If different thread IDs are shown for different requests, it confirms that the server is handling requests in a multithreaded manner. 33 | 34 | 35 | ![OutPut](https://github.com/hackice20/multi-threaded-server/blob/master/Screenshot%202024-09-04%20060752.png) 36 | -------------------------------------------------------------------------------- /Screenshot 2024-09-04 060752.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hackice20/multi-threaded-server/9bd51aea9b9fd23b8b6a384b473988b1b030a18d/Screenshot 2024-09-04 060752.png -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::net::{TcpListener, TcpStream}; 2 | use std::io::{Read, Write}; 3 | use std::thread; 4 | use std::time::Duration; 5 | use std::thread::ThreadId; 6 | 7 | fn handle_client(mut stream: TcpStream) { 8 | let thread_id: ThreadId = thread::current().id(); 9 | println!("Handling connection on thread: {:?}", thread_id); 10 | 11 | let mut buffer = [0; 1024]; 12 | stream.read(&mut buffer).unwrap(); 13 | 14 | 15 | thread::sleep(Duration::from_secs(2)); 16 | 17 | let response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, World!"; 18 | stream.write(response.as_bytes()).unwrap(); 19 | stream.flush().unwrap(); 20 | } 21 | 22 | fn main() { 23 | let listener = TcpListener::bind("127.0.0.1:6969").unwrap(); 24 | println!("Server listening on port 6969..."); 25 | 26 | for stream in listener.incoming() { 27 | match stream { 28 | Ok(stream) => { 29 | thread::spawn(|| { 30 | handle_client(stream); 31 | }); 32 | } 33 | Err(e) => { 34 | println!("Error: {}", e); 35 | } 36 | } 37 | } 38 | } 39 | 40 | --------------------------------------------------------------------------------