{"id":5574,"date":"2026-05-04T11:29:21","date_gmt":"2026-05-04T02:29:21","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5574"},"modified":"2026-05-04T11:29:55","modified_gmt":"2026-05-04T02:29:55","slug":"basic-socket","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/04\/basic-socket\/","title":{"rendered":"[Basic]Socket"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uae30\ubcf8\uc801\uc778 \uc18c\ucf13\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.<br>This is an explanation of basic sockets.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Echo Server\ub294 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uc11c \ubcf4\ub0b8 \uba54\uc138\uc9c0\ub97c \uc11c\ubc84\uac00 \ub2e4\uc2dc \ud074\ub77c\uc774\uc5b8\ud2b8\ub85c \ubcf4\ub0b4\ub294 \uac04\ub2e8\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4.<br>Echo Server is simple code that sends a message sent from a client back to the client.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Thread Server\ub294 \uc544\uc8fc \uae30\ubcf8\uc801\uc778 \ucc44\ud305 \uc11c\ubc84 \uad6c\uc870\ub97c \uac00\uc9c0\uace0 \uc788\uc2b5\ub2c8\ub2e4.<br>Thread Server has a very basic chat server structure.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Echo Server<\/p>\n\n\n\n<p>\u2714\ufe0f Server.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;sys\/socket.h&gt; \/\/ \uc18c\ucf13 API \/ Socket API\n#include &lt;netinet\/in.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;cstring&gt;\n\nint main() {\n\n    \/\/ \uc18c\ucf13 \uc0dd\uc131,0\uc740 \ud504\ub85c\ud1a0\ucf5c \uc790\ub3d9 \uc120\ud0dd\n    \/\/ \/\/ Create socket, 0 for automatic protocol selection\n    int server_fd = socket(AF_INET, SOCK_STREAM, 0);\n\n    sockaddr_in address;\n    address.sin_family = AF_INET; \/\/ IPv4\n    address.sin_addr.s_addr = INADDR_ANY; \/\/ \ubaa8\ub4e0 IP \ud5c8\uc6a9 \/ Allow all IPs\n    address.sin_port = htons(5080); \/\/ \ud3ec\ud2b8 8080 \/ Port 8080\n\n    bind(server_fd, (struct sockaddr*)&amp;address, sizeof(address)); \/\/ \uc8fc\uc18c \ubc14\uc778\ub529 \/ Address Binding\n    listen(server_fd, 3); \/\/ \uc811\uc18d \ub300\uae30 \/ Waiting for connection\n\n    std::cout &lt;&lt; \"listening...\" &lt;&lt; std::endl;\n\n    int new_socket = accept(server_fd, nullptr, nullptr); \/\/ \uc5f0\uacb0 \uc218\ub77d \/ Accept Connection\n\n    char buffer&#91;1024] = {0};\n    read(new_socket, buffer, 1024); \/\/ \ub370\uc774\ud130 \uc218\uc2e0 \/ Receive Data\n    std::cout &lt;&lt; \"\uc218\uc2e0\ub41c \uba54\uc2dc\uc9c0 \/ Received Message: \" &lt;&lt; buffer &lt;&lt; std::endl;\n\n    const char* hello = \"Hello from server\";\n    send(new_socket, hello, strlen(hello), 0); \/\/ \uc751\ub2f5 \uc804\uc1a1 \/ Send Response\n\n    close(new_socket);\n    close(server_fd);\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Client.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;arpa\/inet.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;cstring&gt;\n\nint main() {\n    int sock = socket(AF_INET, SOCK_STREAM, 0); \/\/ \uc18c\ucf13 \uc0dd\uc131 \/ create socket\n\n    sockaddr_in serv_addr;\n    serv_addr.sin_family = AF_INET;\n    serv_addr.sin_port = htons(5080);\n    inet_pton(AF_INET, \"127.0.0.1\", &amp;serv_addr.sin_addr); \/\/ \uc11c\ubc84 IP \uc124\uc815 \/ Server IP settings\n\n    connect(sock, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)); \/\/ \uc11c\ubc84 \uc811\uc18d \/ server connection\n\n    const char* message = \"Hello from client\";\n    send(sock, message, strlen(message), 0); \/\/ \uba54\uc138\uc9c0 \uc804\uc1a1 \/ Send Message\n\n    char buffer&#91;1024] = {0};\n    read(sock, buffer, 1024); \/\/ \uc11c\ubc84 \uc751\ub2f5 \uc218\uc2e0 \/ Receive Server Response\n    std::cout &lt;&lt; \"\uc11c\ubc84 \uc751\ub2f5 \/ Server Response: \" &lt;&lt; buffer &lt;&lt; std::endl;\n\n    close(sock); \/\/ \uc18c\ucf13 \uc885\ub8cc \/ Close Socket\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ Server.cpp -o server -std=c++17\ng++ Client.cpp -o client -std=c++17<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/server\n.\/client<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uacb0\uacfc \/ Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># server\nMacBookAir echo % .\/server\nlistening...\n\uc218\uc2e0\ub41c \uba54\uc2dc\uc9c0 \/ Received Message: Hello from client\n\n# client\nMacBookAir echo % .\/client\n\uc11c\ubc84 \uc751\ub2f5 \/ Server Response: Hello from server<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb ThreadServer<\/p>\n\n\n\n<p>\u2714\ufe0fthreadServer.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;thread&gt; \/\/ \uc2a4\ub808\ub4dc \ud5e4\ub354 \/ thread header\n#include &lt;vector&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;netinet\/in.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;cstring&gt;\n\n\/\/ \ud074\ub77c\uc774\uc5b8\ud2b8\uc640 \ud1b5\uc2e0\ud558\uae30 \uc704\ud55c \ud568\uc218\n\/\/ A function to communicate with the client\nvoid handle_client(int client_socket) {\n    char buffer&#91;1024];\n    while (true) {\n\n        \/\/ \ubc84\ud37c\ub97c \ubaa8\ub450 0\uc73c\ub85c \ucd08\uae30\ud654\ud574\uc11c \uae30\uc874 \uc4f0\ub808\uae30 \uac12 \uc9c0\uc6b0\uae30\n        \/\/ Initialize buffer to all 0s to clear existing garbage values\n        memset(buffer, 0, 1024);\n        ssize_t valread = read(client_socket, buffer, 1024);\n\n        if (valread &lt;= 0) {\n            std::cout &lt;&lt; \"\ud074\ub77c\uc774\uc5b8\ud2b8 \uc811\uc18d \uc885\ub8cc \/ Client connection terminated \" &lt;&lt; std::endl;\n            break;\n        }\n\n        std::cout &lt;&lt; \"\uc218\uc2e0\ub41c \uba54\uc2dc\uc9c0 \/ Received message: \" &lt;&lt; buffer &lt;&lt; std::endl;\n        send(client_socket, buffer, valread, 0); \/\/ send\n    }\n    close(client_socket);\n}\n\nint main() {\n    int server_fd = socket(AF_INET, SOCK_STREAM, 0);\n\n    \/\/ \ud3ec\ud2b8\uac00 \uc774\ubbf8 \uc0ac\uc6a9 \uc911\uc774\uc5b4\ub3c4 \uc989\uc2dc \uc7ac\uc0ac\uc6a9 \uac00\ub2a5\ud558\uac8c \uc124\uc815\n    \/\/ Configure port to be immediately reusable even if it is already in use\n    int opt = 1;\n    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));\n\n    sockaddr_in address;\n    address.sin_family = AF_INET;\n    address.sin_addr.s_addr = INADDR_ANY;\n    address.sin_port = htons(5080);\n\n    if (bind(server_fd, (struct sockaddr*)&amp;address, sizeof(address)) &lt; 0) {\n        perror(\"Bind failed\"); \/\/ \uc5d0\ub7ec \ub0b4\uc6a9 \ucd9c\ub825 \/ Error details output\n        return 1;\n    }\n\n    if (listen(server_fd, 5) &lt; 0) {\n        perror(\"Listen failed\");\n        return 1;\n    }\n\n    std::cout &lt;&lt; \"\uc11c\ubc84 \uc815\uc0c1 \uc2dc\uc791, \uc811\uc18d \ub300\uae30 \uc911 \/ Server started normally, waiting for connection ...\" &lt;&lt; std::endl;\n\n    while (true) {\n        int client_sock = accept(server_fd, nullptr, nullptr);\n        if (client_sock &lt; 0) {\n            perror(\"Accept failed\");\n            continue;\n        }\n        std::cout &lt;&lt; \"\uc0c8 \ud074\ub77c\uc774\uc5b8\ud2b8 \uc5f0\uacb0\ub428 \/ New Client Connected: \" &lt;&lt; client_sock &lt;&lt; \")\" &lt;&lt; std::endl;\n        std::thread(handle_client, client_sock).detach();\n    }\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0fClient.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;arpa\/inet.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;cstring&gt;\n\nint main() {\n    int sock = socket(AF_INET, SOCK_STREAM, 0); \/\/ \uc18c\ucf13 \uc0dd\uc131 \/ create socket\n\n    sockaddr_in serv_addr;\n    serv_addr.sin_family = AF_INET;\n    serv_addr.sin_port = htons(5080);\n    inet_pton(AF_INET, \"127.0.0.1\", &amp;serv_addr.sin_addr); \/\/ \uc11c\ubc84 IP \uc124\uc815 \/ Set Server IP\n\n    connect(sock, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)); \/\/ \uc11c\ubc84 \uc811\uc18d \/ Connect to Server\n\n    \/\/ const char* message = \"Hello from client\";\n    \/\/ send(sock, message, strlen(message), 0); \/\/ \uba54\uc2dc\uc9c0 \uc804\uc1a1\n\n    \/\/ char buffer&#91;1024] = {0};\n    \/\/ read(sock, buffer, 1024); \/\/ \uc11c\ubc84 \uc751\ub2f5 \uc218\uc2e0\n    \/\/ std::cout &lt;&lt; \"\uc11c\ubc84 \uc751\ub2f5: \" &lt;&lt; buffer &lt;&lt; std::endl;\n\n\n    while (true) {\n        std::string msg;\n        std::cout &lt;&lt; \"\ubcf4\ub0bc \uba54\uc2dc\uc9c0 (\uc885\ub8cc\ub294 quit): \";\n        std::getline(std::cin, msg); \/\/ \ud0a4\ubcf4\ub4dc \uc785\ub825 \ubc1b\uae30 \/ Receiving keyboard input\n\n        \/\/ quit\uac00 \uc785\ub825\ub418\uba74... \/ When quit is entered...\n        if (msg == \"quit\") break;\n\n        send(sock, msg.c_str(), msg.length(), 0); \/\/ \uba54\uc138\uc9c0\ub97c \uc11c\ubc84\ub85c \uc804\uc1a1 \/ Send message to server\n\n        char buffer&#91;1024] = {0};\n        int valread = read(sock, buffer, 1024); \/\/ \uc11c\ubc84 \uc751\ub2f5 \ub300\uae30 \/ Waiting for server response\n\n        if (valread &lt;= 0) {\n            std::cout &lt;&lt; \"\uc11c\ubc84\uc640\uc758 \uc5f0\uacb0\uc774 \ub04a\uacbc\uc2b5\ub2c8\ub2e4. \/ Server connection closed.\" &lt;&lt; std::endl;\n            break;\n        }\n        std::cout &lt;&lt; \"\uc11c\ubc84 \uc751\ub2f5 \/ Server response:: \" &lt;&lt; buffer &lt;&lt; std::endl;\n    }\n\n\n\n    close(sock); \/\/ \uc18c\ucf13 \uc885\ub8cc \/ Socket close\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ threadServer.cpp -o server -std=c++17 -pthread\ng++ Client.cpp -o client -std=c++17<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/server\n.\/clinet<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f\uacb0\uacfc \/ Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># server\nMacBookAir thread % .\/server\n\uc11c\ubc84 \uc815\uc0c1 \uc2dc\uc791, \uc811\uc18d \ub300\uae30 \uc911 \/ Server started normally, waiting for connection ...\n\uc0c8 \ud074\ub77c\uc774\uc5b8\ud2b8 \uc5f0\uacb0\ub428 \/ New Client Connected: 4)\n\uc218\uc2e0\ub41c \uba54\uc2dc\uc9c0 \/ Received message: hello\n\uc218\uc2e0\ub41c \uba54\uc2dc\uc9c0 \/ Received message: \uc548\ub155\ud558\uc138\uc694\n\uc218\uc2e0\ub41c \uba54\uc2dc\uc9c0 \/ Received message: 8\n\ud074\ub77c\uc774\uc5b8\ud2b8 \uc811\uc18d \uc885\ub8cc \/ Client connection terminated \n\n#client\nMacBookAir thread % .\/client\n\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Message to send (\uc885\ub8cc\/Quit: quit): hello\n\uc11c\ubc84 \uc751\ub2f5 \/ Server response:: hello\n\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Message to send (\uc885\ub8cc\/Quit: quit): \uc548\ub155\ud558\uc138\uc694\n\uc11c\ubc84 \uc751\ub2f5 \/ Server response:: \uc548\ub155\ud558\uc138\uc694\n\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Message to send (\uc885\ub8cc\/Quit: quit): 8\n\uc11c\ubc84 \uc751\ub2f5 \/ Server response:: 8\n\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Message to send (\uc885\ub8cc\/Quit: quit):  quit\nMacBookAir thread % <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uae30\ubcf8\uc801\uc778 \uc18c\ucf13\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.This is an explanation of basic sockets. \ud83d\udc49\ud83c\udffb Echo Server\ub294 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uc11c \ubcf4\ub0b8 \uba54\uc138\uc9c0\ub97c \uc11c\ubc84\uac00 \ub2e4\uc2dc \ud074\ub77c\uc774\uc5b8\ud2b8\ub85c \ubcf4\ub0b4\ub294 \uac04\ub2e8\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4.Echo Server is simple code that sends a message sent from a client back to the client. \ud83d\udc49\ud83c\udffb Thread Server\ub294 \uc544\uc8fc \uae30\ubcf8\uc801\uc778 \ucc44\ud305 \uc11c\ubc84 \uad6c\uc870\ub97c \uac00\uc9c0\uace0 \uc788\uc2b5\ub2c8\ub2e4.Thread Server has a very basic chat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5574","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5574"}],"version-history":[{"count":1,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5574\/revisions"}],"predecessor-version":[{"id":5575,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5574\/revisions\/5575"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}