👉🏻 터미널에서 명령어로 종료하는 로직이 추가됐습니다.
Logic has been added to terminate using a command in the terminal.
👉🏻 ❗️로 표시된 부분은 기존코드에서 바뀐 내용입니다.
The parts marked with ❗️ are the changes made to the existing code.
👉🏻 명령어로 종료하는 로직은 “Main.cpp”파일의 “while” 루프 부분입니다.
The logic for terminating with a command is the “while” loop part of the “Main.cpp” file.
✔️ Server.h
#include <string>
#include <atomic> // Atomic Flag 사용을 위해 추가❗️
class Server {
public:
// 생성자: 서버 초기화 (IP, Port)
// Constructor: Initialize server (IP, Port)
Server(const std::string& ip, int port);
// 소멸자: 객체가 파괴될 때 서버를 안전하게 종료합니다.❗️
// Destructor: Safely shuts down the server when the object is destroyed.
~Server() {
stop();
}
// 서버 소켓 파일 디스크립터를 저장할 멤버 변수
// Member variable to store server socket file descriptors
int server_fd = -1;
// 서버 실행 함수: 리스닝을 시작하고 메인 루프를 돌림
// Server execution function: Starts listening and runs the main loop
bool start();
// 서버 종료 (필요하다면)
// Shut down the server (if necessary)
void stop();
private:
std::string ip_address;
int port_number;
// 서버가 실행 상태인지 확인하는 플래그
// Flag to check if the server is running
std::atomic<bool> running{false};// ❗️
// 실제 리스닝 및 연결 수신 로직 (내부 구현)
// Actual listening and connection reception logic (internal implementation)
void run_listener();
// 연결 처리 로직 (스레드 풀 또는 별도 핸들러에게 위임)
// Connection handling logic (delegated to thread pool or separate handler)
void handle_client(int client_socket);
};
✔️ Server.cpp
#include "Server.h"
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <thread>
#include <vector>
#define BACKLOG 10 // 대기 큐 크기 / wait queue size
Server::Server(const std::string& ip, int port)
: ip_address(ip), port_number(port) {}
void Server::run_listener() {
//std::cout << "[INFO] Server listening on " << ip_address << ":" << port_number << std::endl;
// --- 1. 소켓 생성 (Socket Creation) --- //
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("socket failed");
exit(EXIT_FAILURE);
}
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
// --- 2. 바인딩 (Binding) --- //
struct sockaddr_in address;
address.sin_family = AF_INET; // IPv4
address.sin_addr.s_addr = INADDR_ANY; // 모든 인터페이스 IP 사용 / Use all interface IPs
address.sin_port = htons(port_number); // 포트를 네트워크 바이트 순서로 변환 / Convert ports to network byte order
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
// --- 3. 리스닝 시작 (setsockopt: listen_socket(socket)) --- //
if (listen(server_fd, BACKLOG) < 0) {
perror("listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
std::cout << "Server listening on port " << port_number << "..." << std::endl;
// --- 4. 메인 루프 (Accept loop): 클라이언트 연결을 무한히 기다림 / Infinitely waiting for client connections --- //
//while (true) { //❗️
// 무한루프에서 종료신호를 받으면 루프를 종료 / Exit the loop when the termination signal is received
while (running.load()) {
// 종료신호 여부를 확인하기 위해 0.1초 대기 / Wait for 0.1 seconds to check the termination signal
// 부하 방지를 위해 대기 / Standby to prevent overload❗️
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// IPv4구조체 / Structure for IPv4 address
struct sockaddr_in client_address;
socklen_t addrlen = sizeof(client_address);
// accept() 함수가 블로킹(Blocking) 상태에서 클라이언트 연결을 기다림
// The accept() function waits for client connections in a blocking state.
int client_socket = accept(server_fd, (struct sockaddr *)&client_address, &addrlen);
if (running.load() && client_socket < 0) { // ❗️
perror("accept failed");
continue; // 실패했으면 다음 루프를 돌며 재시도 / Retry in the next loop if it fails
}
// 클라이언트 접속 / Client connected
std::thread client_thread([this, client_socket]() {
this->handle_client(client_socket);
});
client_thread.detach();
}
}
void Server::handle_client(int client_socket) {
char buffer[1024];
std::cout << "[Connection] New client connected." << std::endl;
// 1. 데이터 수신 / receive data (Read)
ssize_t bytes_read = read(client_socket, buffer, sizeof(buffer) - 1);
if (bytes_read > 0) {
buffer[bytes_read] = '\0'; // C-string
std::string request = buffer;
std::cout << "[Received] " << request.substr(0, 30) << "..." << std::endl;
// 2. 응답 전송 / send response(Write)
const char* response = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<h1>👋Hi~ Hi~ Hi~~~!</h1><p>Welcome to the miniWebserver!</p>";
write(client_socket, response, strlen(response));
}
// 3. 연결 종료 / close connection
close(client_socket);
std::cout << "[Connection] Client disconnected." << std::endl;
}
bool Server::start() {
// 서버가 실제로 작동하는 메인 함수 / Main function that actually runs the server
try {
if (!running.load()) {
running.store(true);
run_listener();
return false;
}
} catch (const std::exception& e) {
std::cerr << "[ERROR] Server failed to start: " << e.what() << std::endl;
return false;
}
return true;
}
void Server::stop() {
// 서버를 종료하는 함수 / Function to stop the server
// if (server_fd != -1) {
// // 멤버 변수에 저장된 값을 사용하여 종료합니다. / Use the value stored in the member variable to stop the server
// close(server_fd);
// server_fd = -1; // 닫았으므로 초기화 / Initialize to -1 after closing
// std::cerr << "Server Stop: " << std::endl;
// }
if (running.load() && server_fd != -1) { // ❗️
// 플래그를 false로 설정하여 루프 종료 신호 전송
// Set flag to false to send loop termination signal
running.store(false);
close(server_fd);
// 실제로는 여기서 소켓을 닫거나 리소스 해제를 수행합니다.
// In practice, close the socket or release resources here.
server_fd = -1; // 닫았으므로 초기화 / Initialize to -1 after closing
std::cout << "[Server] Shutdown initiated." << std::endl;
}
}
✔️ Main.cpp
#include "Server.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include <string>
int main() {
// 1. 서버 인스턴스 생성 / Create server instance
Server my_server("127.0.0.1", 5080);
std::thread server_thread(&Server::start, &my_server); // 서버 시작 / Start server
std::cout << "--- Starting Server Application ---" << std::endl;
// 2. 메인 스레드는 사용자 입력 대기 / The main thread waits for user input❗️
// 1)엔터입력시 서버종료하기 / Shut down the server when Enter is pressed
// std::cin.get();
//
// 2)/exit입력시 서버 종료하기 / Shut down the server by entering /exit
std::string command;
while (true) {
std::cout << "CMD> "; // 명령어 프롬프트 표시 / Show command prompt
// std::getline을 사용하여 공백을 포함한 전체 문자열을 한 번에 받습니다.
// Use std::getline to get the entire string including spaces at once.
if (!std::getline(std::cin, command)) {
// EOF (Ctrl+D 등)로 입력을 받을 수 없는 상황이 발생하면 종료
break;
}
// 입력된 명령어를 확인합니다. / Check the input command
if (command.empty()) {
// 아무것도 입력하지 않았으면 그냥 넘어갑니다./ If you don't enter anything, it will just skip.
continue;
} else if (command == "/exit" || command == "exit") {
std::cout << "[INFO] 종료 명령을 받았습니다. 서비스 종료를 시작합니다.\n";
std::cout << "[INFO] Received a shutdown command. Starting service shutdown.\n";
break; // 루프 종료 / Loop End
} else if (command == "/status") {
std::cout << "[STATUS] 서비스는 정상적으로 작동 중입니다.\n";
std::cout << "[STATUS] The service is running normally.\n";
} else {
std::cout << "[ERROR] 알 수 없는 명령어입니다. (/exit, /status)\n";
std::cout << "[ERROR] Unknown command. (/exit, /status)\n";
}
}
// 3. 사용자 요청에 의해 서버를 안전하게 종료 ❗️
// 3. Safely shut down the server at the user's request ❗️
my_server.stop();
// 2. 종료 로직 (서버가 종료되지 않기 떄문에 실행되지 않음,나중을 위한 종료 로직)
// Termination logic (not executed because the server has not terminated, termination logic for later)
server_thread.join();
my_server.stop();
return 0;
}
✔️ 실행 / Run

