Last active
November 27, 2025 03:06
-
-
Save NatanR-dev/f03e5d4b564445e8013824da7210bfb7 to your computer and use it in GitHub Desktop.
c webserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Usar imagem base com GCC pré-instalado | |
| FROM n0madic/alpine-gcc:9.2.0 | |
| # Criar diretório de trabalho | |
| WORKDIR /app | |
| # Copiar o código-fonte para o container | |
| COPY . . | |
| # Compilar o programa | |
| RUN gcc -o server webserver.c | |
| # Comando padrão para executar o servidor | |
| CMD ["./server"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <arpa/inet.h> | |
| #define PORT 8080//porta | |
| #define BUFFER_SIZE 1024//tamanho buffer | |
| int main() { | |
| int socketTCP, clientSocket;//ouvir, conectar | |
| struct sockaddr_in serverAddress;//endereço | |
| socklen_t serverAddressLength = sizeof(serverAddress); | |
| char buffer[BUFFER_SIZE] = {0};//buffer leitura | |
| const char* htmlBody =//saída html | |
| "<html><head><title>SIM!</title></head>" | |
| "<body><h1>Hello World!</h1><p>RECEBA!.</p></body></html>"; | |
| char httpResponse[BUFFER_SIZE];//resposta HTTP | |
| snprintf(httpResponse, sizeof(httpResponse), | |
| "HTTP/1.1 200 OK\r\n" | |
| "Content-Type: text/html\r\n" | |
| "Content-Length: %d\r\n\r\n%s", | |
| (int)strlen(htmlBody), htmlBody); | |
| const char* jsonBody = "{\"message\": \"Receba!!\", \"status\": \"success\"}"; | |
| snprintf(httpResponse, sizeof(httpResponse), | |
| "HTTP/1.1 200 OK\r\n" | |
| "Content-Type: application/json\r\n" | |
| "Content-Length: %d\r\n\r\n%s", | |
| (int)strlen(jsonBody), jsonBody); | |
| int socketOption = 1;//reuso-socket | |
| socketTCP = socket(AF_INET, SOCK_STREAM, 0);//cria socket | |
| setsockopt(socketTCP, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, | |
| &socketOption, sizeof(socketOption));//ativa reuso | |
| serverAddress.sin_family = AF_INET;//IPv4 | |
| serverAddress.sin_addr.s_addr = INADDR_ANY;//qualquer interface | |
| serverAddress.sin_port = htons(PORT);//porta | |
| bind(socketTCP, (struct sockaddr*)&serverAddress, sizeof(serverAddress));// vincula | |
| listen(socketTCP, 3);//escuta (fila 3) | |
| printf("WebServer started on localhost. listening on port: %d\n", PORT); | |
| while (1) { | |
| clientSocket = accept(socketTCP, (struct sockaddr*)&serverAddress, &serverAddressLength); // aceita | |
| read(clientSocket, buffer, sizeof(buffer));// lê | |
| write(clientSocket, httpResponse, strlen(httpResponse));// escreve | |
| close(clientSocket);//fecha conexão | |
| } | |
| close(socketTCP);//fecha socket | |
| printf("Webserver stopped.\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment