87 lines
2.8 KiB
Plaintext
87 lines
2.8 KiB
Plaintext
|
|
int main() {
|
||
|
|
printf("=== Async I/O Demo: File I/O + Async I/O + Socket I/O ===\n\n");
|
||
|
|
|
||
|
|
printf("Step 1: Synchronous File I/O\n");
|
||
|
|
int log_file = fopen("demo_log.txt", "w");
|
||
|
|
fputs(log_file, "Server Log Started\n");
|
||
|
|
fclose(log_file);
|
||
|
|
printf("Created log file\n\n");
|
||
|
|
|
||
|
|
printf("Step 2: Async File Operations\n");
|
||
|
|
log_file = fopen("demo_log.txt", "a");
|
||
|
|
int write_op1 = async_fwrite(log_file, "Initializing server...\n", 24);
|
||
|
|
int write_op2 = async_fwrite(log_file, "Binding to port 8080...\n", 25);
|
||
|
|
|
||
|
|
printf("Multiple async writes queued\n");
|
||
|
|
async_wait(write_op1);
|
||
|
|
async_wait(write_op2);
|
||
|
|
printf("All async writes completed\n\n");
|
||
|
|
|
||
|
|
printf("Step 3: Socket Server Setup\n");
|
||
|
|
int server_socket = socket(AF_INET(), SOCK_STREAM(), 0);
|
||
|
|
if (server_socket < 0) {
|
||
|
|
printf("ERROR: Could not create socket\n");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
printf("Socket created: %d\n", server_socket);
|
||
|
|
|
||
|
|
int bind_result = bind(server_socket, 8080);
|
||
|
|
if (bind_result < 0) {
|
||
|
|
printf("ERROR: Could not bind to port\n");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
printf("Bound to port 8080\n");
|
||
|
|
|
||
|
|
listen(server_socket, 5);
|
||
|
|
printf("Listening for connections...\n\n");
|
||
|
|
|
||
|
|
printf("Step 4: Log async write while waiting for connection\n");
|
||
|
|
int log_op = async_fwrite(log_file, "Waiting for client...\n", 23);
|
||
|
|
|
||
|
|
printf("Accepting client (this will wait for a connection)\n");
|
||
|
|
printf("In another terminal, run: curl http://localhost:8080/\n\n");
|
||
|
|
|
||
|
|
int client = accept(server_socket);
|
||
|
|
async_wait(log_op);
|
||
|
|
printf("Client connected: %d\n\n", client);
|
||
|
|
|
||
|
|
printf("Step 5: Handle client with async socket I/O\n");
|
||
|
|
int buffer[1024];
|
||
|
|
int recv_op = async_recv(client, &buffer, 1024, 0);
|
||
|
|
|
||
|
|
printf("Async receive started...\n");
|
||
|
|
while (async_poll(recv_op) == 0) {
|
||
|
|
printf(".");
|
||
|
|
}
|
||
|
|
printf("\n");
|
||
|
|
|
||
|
|
int bytes = async_result(recv_op);
|
||
|
|
printf("Received %d bytes\n", bytes);
|
||
|
|
|
||
|
|
char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nAsync I/O Demo Success!\n";
|
||
|
|
int send_op = async_send(client, response, strlen(response), 0);
|
||
|
|
async_wait(send_op);
|
||
|
|
printf("Response sent asynchronously\n\n");
|
||
|
|
|
||
|
|
printf("Step 6: Final log entry\n");
|
||
|
|
int final_log = async_fwrite(log_file, "Client served successfully\n", 28);
|
||
|
|
async_wait(final_log);
|
||
|
|
|
||
|
|
close(client);
|
||
|
|
close(server_socket);
|
||
|
|
fclose(log_file);
|
||
|
|
|
||
|
|
printf("\nStep 7: Read log file\n");
|
||
|
|
log_file = fopen("demo_log.txt", "r");
|
||
|
|
char *line = fgets(log_file, 256);
|
||
|
|
while (strlen(line) > 0 && feof(log_file) == 0) {
|
||
|
|
printf("LOG: %s", line);
|
||
|
|
line = fgets(log_file, 256);
|
||
|
|
}
|
||
|
|
fclose(log_file);
|
||
|
|
|
||
|
|
printf("\n=== Demo Complete ===\n");
|
||
|
|
printf("Demonstrated: Sync File I/O, Async File I/O, Socket I/O, and Async Socket I/O\n");
|
||
|
|
return 0;
|
||
|
|
}
|