代码拉取完成,页面将自动刷新
#include <iostream>
#include <vector>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 9000
int main() {
// 创建服务器套接字
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
std::cerr << "Failed to create socket\n";
exit(EXIT_FAILURE);
}
// 准备地址
sockaddr_in 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, (sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Failed to bind address\n";
exit(EXIT_FAILURE);
}
// 开始监听
if (listen(server_fd, 3) < 0) {
std::cerr << "Failed to start listening\n";
exit(EXIT_FAILURE);
}
// 等待连接
int client_fd;
sockaddr_in client_addr {};
socklen_t client_len = sizeof(client_addr);
if ((client_fd = accept(server_fd, (sockaddr*)&client_addr, &client_len)) < 0) {
std::cerr << "Failed to accept client\n";
exit(EXIT_FAILURE);
}
// 接收参数
std::vector<std::string> params;
char buffer[1024];
int bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
while (bytes_received > 0) {
std::string str(buffer, bytes_received);
params.push_back(str);
bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
}
// 输出参数
std::cout << "Received parameters:\n";
for (const auto& param : params) {
std::cout << param << "\n";
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。