Linux网络编程http通信入门学习,请使用C/C++举例帮我从入门到简单应用来了解http通信原理,在代码中写上必要注释
时间: 2024-06-09 09:05:47 浏览: 95
好的,下面是一个简单的例子,实现了一个基于HTTP协议的服务器和客户端通信:
服务器端代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define PORT 8080
#define MAX_CLIENTS 100
#define BUFFER_SIZE 1024
void handle_request(int client_fd) {
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
read(client_fd, buffer, BUFFER_SIZE);
printf("Received request:\n%s\n", buffer);
char response[BUFFER_SIZE];
bzero(response, BUFFER_SIZE);
snprintf(response, BUFFER_SIZE,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<html><body><h1>Hello, World!</h1></body></html>"
);
write(client_fd, response, strlen(response));
close(client_fd);
}
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Error opening socket");
exit(1);
}
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Error binding socket");
exit(1);
}
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("Error listening on socket");
exit(1);
}
printf("Server started, listening on port %d\n", PORT);
while (1) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_fd < 0) {
perror("Error accepting client connection");
continue;
}
printf("Client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
handle_request(client_fd);
}
return 0;
}
```
客户端代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#define SERVER_HOST "localhost"
#define SERVER_PORT 8080
#define BUFFER_SIZE 1024
int main() {
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("Error opening socket");
exit(1);
}
struct hostent* server = gethostbyname(SERVER_HOST);
if (server == NULL) {
fprintf(stderr, "Error: no such host %s\n", SERVER_HOST);
exit(1);
}
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
bcopy(server->h_addr, &server_addr.sin_addr.s_addr, server->h_length);
server_addr.sin_port = htons(SERVER_PORT);
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Error connecting to server");
exit(1);
}
char request[BUFFER_SIZE];
bzero(request, BUFFER_SIZE);
snprintf(request, BUFFER_SIZE,
"GET / HTTP/1.1\r\n"
"Host: %s:%d\r\n"
"\r\n",
SERVER_HOST, SERVER_PORT
);
write(client_fd, request, strlen(request));
char response[BUFFER_SIZE];
bzero(response, BUFFER_SIZE);
read(client_fd, response, BUFFER_SIZE);
printf("Received response:\n%s\n", response);
close(client_fd);
return 0;
}
```
在这个例子中,我们使用了HTTP/1.1协议来进行通信。服务器端在接收到客户端的请求后,返回了一个包含HTML内容的响应。客户端发送了一个GET请求,并输出了服务器端返回的响应。
在HTTP协议中,请求和响应分别由三部分组成:起始行、头部和实体。起始行包含了请求或响应的方法、URI和协议版本;头部包含了一些元数据,如Content-Type、Content-Length等;实体则包含了请求或响应的具体内容。在这个例子中,我们只使用了简单的起始行和头部,而实体则只包含了一个简单的HTML页面。
代码中的注释应该足够清晰,但如果您有任何疑问,请随时问我。
阅读全文