#include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct dirent *pDirent; DIR *pDir; // Ensure correct argument count. if (argc != 2) { printf("Usage: testprog \n"); return 1; } // Ensure we can open directory. pDir = opendir(argv[1]); if (pDir == NULL) { printf("Cannot open directory '%s'\n", argv[1]); return 1; } // Process each entry. char *filedata = malloc(4096); char *fname = malloc(1024); FILE *file; while ((pDirent = readdir(pDir)) != NULL) { sprintf(fname, "/proc/%s/stat", pDirent->d_name); file = fopen(fname, "rb"); fread(filedata, 1, 4096, file); fclose(file); strcat(fname, "us"); file = fopen(fname, "rb"); fread(filedata, 1, 4096, file); fclose(file); strtok(filedata, "\n"); printf("[%s]\n", filedata); } // Close directory and exit. closedir(pDir); return 0; } #define PORT 8080 #define BUFFER_SIZE 1024 void handle_client(int client_socket) { // HTTP response const char *http_response = "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 13\r\n" "Connection: close\r\n" "\r\n" "Hello, World!"; // Send the response to the client send(client_socket, http_response, strlen(http_response), 0); // Close the client socket close(client_socket); } int main2() { int server_socket, client_socket; struct sockaddr_in server_addr, client_addr; socklen_t addr_len = sizeof(client_addr); // Create the server socket if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket creation failed"); exit(EXIT_FAILURE); } // Configure the server address server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); // Bind the socket to the specified port if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("Bind failed"); close(server_socket); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_socket, 5) == -1) { perror("Listen failed"); close(server_socket); exit(EXIT_FAILURE); } printf("Server is listening on port %d\n", PORT); // Accept and handle incoming connections while (1) { if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &addr_len)) == -1) { perror("Accept failed"); continue; } // Handle the client in a separate function handle_client(client_socket); } // Close the server socket (this won't actually be reached in this example) close(server_socket); return 0; }