#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #define PORT 8080 #define MAXLINE 1024 void handle_client(int client_socket); int main() { int server_socket, client_socket; struct sockaddr_in server_address, client_address; socklen_t client_address_len = sizeof(client_address); // Create socket server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket < 0) { perror("Error creating socket"); exit(EXIT_FAILURE); } // Set up server address memset(&server_address, 0, sizeof(server_address)); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(PORT); // Bind the socket if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) { perror("Error binding socket"); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_socket, 10) < 0) { perror("Error listening for connections"); exit(EXIT_FAILURE); } printf("Server listening on port %d...\n", PORT); while (1) { // Accept a connection client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_len); if (client_socket < 0) { perror("Error accepting connection"); exit(EXIT_FAILURE); } // Handle the client request in a separate function handle_client(client_socket); } close(server_socket); return 0; } void handle_client(int client_socket) { char buffer[MAXLINE]; ssize_t n; // Receive the request from the client n = recv(client_socket, buffer, sizeof(buffer), 0); if (n < 0) { perror("Error reading from socket"); exit(EXIT_FAILURE); } // Print the received request printf("Received request:\n%s\n", buffer); // Simple response for GET request const char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>"; // Send the response back to the client n = send(client_socket, response, strlen(response), 0); if (n < 0) { perror("Error writing to socket"); exit(EXIT_FAILURE); } // Close the client socket close(client_socket); }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #define PORT 8080 #define MAXLINE 1024 int main() { int client_socket; struct sockaddr_in server_address; char request[MAXLINE]; ssize_t n; // Create socket client_socket = socket(AF_INET, SOCK_STREAM, 0); if (client_socket < 0) { perror("Error creating socket"); exit(EXIT_FAILURE); } // Set up server address memset(&server_address, 0, sizeof(server_address)); server_address.sin_family = AF_INET; server_address.sin_port = htons(PORT); inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr); // Connect to the server if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) { perror("Error connecting to server"); exit(EXIT_FAILURE); } // Create a simple GET request const char *get_request = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"; // Send the GET request to the server n = send(client_socket, get_request, strlen(get_request), 0); if (n < 0) { perror("Error writing to socket"); exit(EXIT_FAILURE); } // Receive and print the response from the server while ((n = recv(client_socket, request, sizeof(request), 0)) > 0) { write(STDOUT_FILENO, request, n); } // Close the socket close(client_socket); return 0; }
标签:Web,socket,request,server,client,实验,address,服务器,include From: https://www.cnblogs.com/liTCabcAbc/p/17884904.html