Test to see how readdir formats names
This commit is contained in:
parent
2939999dc4
commit
088ab5547a
@ -2,7 +2,7 @@
|
||||
|
||||
Basic idea:
|
||||
Use the following PS command to gather basic data on processes:
|
||||
ps ax -o pid,ppid,uid,lxc,cuu,rss,times,etimes,stat,command --cols 1000
|
||||
ps ax -o pid,ppid,tgid,uid,lxc,cuu,rss,times,etimes,stat,command --cols 1000
|
||||
|
||||
Use resident size as an approximation of real memory usage.
|
||||
|
||||
|
||||
110
src/server.c
Normal file
110
src/server.c
Normal file
@ -0,0 +1,110 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct dirent *pDirent;
|
||||
DIR *pDir;
|
||||
|
||||
// Ensure correct argument count.
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: testprog <dirname>\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);
|
||||
|
||||
while ((pDirent = readdir(pDir)) != NULL) {
|
||||
printf("[%s]\n", pDirent->d_name);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user