1 Star 0 Fork 0

邹雪梅/xinan1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
web.c 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
邹雪梅 提交于 2023-12-08 11:57 +08:00 . web ex2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[30000] = {0};
char *hello = "HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 13\n\nHello world!";
// Creating socket file descriptor
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Setting up the address structure
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Bind the socket to the address
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listen for connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// Accept a connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Read the incoming message
read(new_socket, buffer, 30000);
printf("%s\n", buffer);
// Send the response
write(new_socket, hello, strlen(hello));
close(new_socket);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zou-xuemei/xinan1.git
git@gitee.com:zou-xuemei/xinan1.git
zou-xuemei
xinan1
xinan1
summerjam

搜索帮助