1 Star 1 Fork 0

micropiggy/raspberryPiOCR

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
socket_server.cpp 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
micropiggy 提交于 2023-06-26 22:01 . add server
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/micropiggy/raspberry-pi-ocr.git
git@gitee.com:micropiggy/raspberry-pi-ocr.git
micropiggy
raspberry-pi-ocr
raspberryPiOCR
master

搜索帮助