/* Copyright (c) [2023] [Syswonder Community] * [Ruxos] is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * http://license.coscl.org.cn/MulanPSL2 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include #include #include #include #include #include const char header[] = "\ HTTP/1.1 200 OK\r\n\ Content-Type: text/html\r\n\ Content-Length: %u\r\n\ Connection: close\r\n\ \r\n\ "; const char content[] = "\n\ \n\ Hello, Ruxos\n\ \n\ \n\
\n\

Hello, Ruxos

\n\
\n\
\n\
\n\ Powered by Ruxos example HTTP server v0.1.0\n\
\n\ \n\ \n\ "; int main() { puts("Hello, Ruxos C HTTP server!"); struct sockaddr_in local, remote; int addr_len = sizeof(remote); local.sin_family = AF_INET; if (inet_pton(AF_INET, "0.0.0.0", &(local.sin_addr)) != 1) { perror("inet_pton() error"); return -1; } local.sin_port = htons(5555); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1) { perror("socket() error"); return -1; } if (bind(sock, (struct sockaddr *)&local, sizeof(local)) != 0) { perror("bind() error"); return -1; } if (listen(sock, 0) != 0) { perror("listen() error"); return -1; } puts("listen on: http://0.0.0.0:5555/"); char buf[1024] = {}; int client; char response[1024] = {}; snprintf(response, 1024, header, strlen(content)); strcat(response, content); for (;;) { client = accept(sock, (struct sockaddr *)&remote, (socklen_t *)&addr_len); if (client == -1) { perror("accept() error"); return -1; } printf("new client %d\n", client); if (recv(client, buf, 1024, 0) == -1) { perror("recv() error"); return -1; } ssize_t l = send(client, response, strlen(response), 0); if (l == -1) { perror("send() error"); return -1; } if (close(client) == -1) { perror("close() error"); return -1; } printf("client %d close: %ld bytes sent\n", client, l); } if (close(sock) == -1) { perror("close() error"); return -1; } return 0; }