windows下使用标准c语言实现一个web服务器,实现网页打开一个css文件,jpg或者文本文件
时间: 2024-01-27 13:06:04 浏览: 82
C语言实现连接合并两个文件.rar
要实现一个web服务器,需要使用socket编程,可以使用Winsock API来实现。以下是一个简单的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define PORT 80
#define MAX_CLIENTS 30
#define BUFFER_SIZE 1024
int main()
{
WSADATA wsaData;
SOCKET listenSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
char buffer[BUFFER_SIZE];
int clientAddrLen, bytesRead;
FILE *file;
char *contentType;
char *extension;
char *fileName;
// 初始化Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed\n");
return 1;
}
// 创建监听socket
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
printf("socket failed\n");
WSACleanup();
return 1;
}
// 绑定地址和端口号
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);
if (bind(listenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("bind failed\n");
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 监听客户端连接
if (listen(listenSocket, MAX_CLIENTS) == SOCKET_ERROR) {
printf("listen failed\n");
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Server is listening on port %d\n", PORT);
// 进入主循环,等待客户端连接
while (1) {
// 接收客户端连接
clientAddrLen = sizeof(clientAddr);
clientSocket = accept(listenSocket, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
printf("accept failed\n");
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 接收客户端发送的数据
memset(buffer, 0, BUFFER_SIZE);
bytesRead = recv(clientSocket, buffer, BUFFER_SIZE, 0);
if (bytesRead == SOCKET_ERROR) {
printf("recv failed\n");
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 解析HTTP请求
fileName = strtok(buffer + 4, " ");
contentType = "text/plain";
// 根据文件扩展名确定Content-Type
extension = strrchr(fileName, '.');
if (extension != NULL) {
if (strcmp(extension, ".html") == 0 || strcmp(extension, ".htm") == 0)
contentType = "text/html";
else if (strcmp(extension, ".css") == 0)
contentType = "text/css";
else if (strcmp(extension, ".jpg") == 0 || strcmp(extension, ".jpeg") == 0)
contentType = "image/jpeg";
else if (strcmp(extension, ".gif") == 0)
contentType = "image/gif";
else if (strcmp(extension, ".png") == 0)
contentType = "image/png";
}
// 打开文件并发送响应
file = fopen(fileName, "rb");
if (file == NULL) {
printf("open file failed\n");
closesocket(clientSocket);
continue;
}
// 发送HTTP响应头
sprintf(buffer, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\n\r\n", contentType);
send(clientSocket, buffer, strlen(buffer), 0);
// 发送文件内容
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, file)) > 0) {
send(clientSocket, buffer, bytesRead, 0);
}
fclose(file);
closesocket(clientSocket);
}
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
这个示例实现了一个简单的Web服务器,可以处理GET请求,支持打开html、css、jpg、gif、png等文件。当收到GET请求时,根据请求的文件名确定Content-Type,然后打开相应的文件并发送响应。注意,这个示例没有处理POST请求和其他HTTP方法,也没有实现动态网页。如果需要实现更完整的Web服务器,还需要处理这些问题。
阅读全文