c++ socket server
一、示例
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <mutex>
#include <shared_mutex>
std::shared_mutex sm;
int shared_data = 1;
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len;
char buffer[1024];
// 创建套接字
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
std::cerr << "Error: Could not create socket" << std::endl;
return 1;
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(1246);
server_addr.sin_addr.s_addr = INADDR_ANY;
// server_addr.sin_addr.s_addr = inet_addr(server_ip);
memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
// 绑定套接字
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error: Could not bind socket" << std::endl;
return 1;
}
// 监听连接
if (listen(server_fd, 10) < 0) {
std::cerr << "Error: Could not listen on socket" << std::endl;
return 1;
}
// 设置接受超时
struct timeval timeout;
timeout.tv_sec = 5; // 设置超时为5秒
timeout.tv_usec = 0;
if (setsockopt(server_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0) {
std::cerr << "Error: Could not set timeout" << std::endl;
return 1;
}
std::cout << "Server is listening on port 1246..." << std::endl;
while (true) {
addr_len = sizeof(client_addr);
// 接受客户端连接
client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &addr_len);
if (client_fd < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
std::cout << "Accept timeout" << std::endl;
std::shared_lock<std::shared_mutex> lock(sm);
std::cout << "Reader thread " << " reads " << shared_data << std::endl;
lock.unlock();
if (shared_data == 0){
break;
}
continue;
} else {
std::cerr << "Error: Could not accept connection" << std::endl;
break;
}
}
std::cout << "Client connected: " << inet_ntoa(client_addr.sin_addr) << ':' << ntohs(client_addr.sin_port) << std::endl;
// 接收并回显消息
while (true) {
// ssize_t bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
// if (bytes_received < 0) {
// std::cerr << "Error: Could not receive data" << std::endl;
// break;
// } else if (bytes_received == 0) {
// std::cout << "Client disconnected" << std::endl;
// break;
// }
buffer[100] = '\0';
std::cout << "Received: " << buffer << std::endl;
// 将消息发送回客户端
if (send(client_fd, buffer, sizeof(buffer), 0) < 0) {
std::cerr << "Error: Could not send data" << std::endl;
break;
}
}
// 关闭客户端套接字
close(client_fd);
}
// 关闭服务器套接字
close(server_fd);
return 0;
}
二、编译:g++ server.cpp -o server -std=c++17
C++ socket粘包
一、粘包是一个伪命题
1、socket是字节流通信,选择不同的解析方式,合适的拆包方式,就能获取正确的包
二、案例
1、演示为,写一个函数,入参选择,想要加载的byte数
ssize_t recv_exp(int sockfd, void *buf, size_t len) {
// 确保接收到期望的字节数
size_t total_received = 0;
char *buffer = (char *)buf;
cout << "total_received:" << total_received << endl;
cout << "len:" << len << endl;
while (total_received < len) {
ssize_t received = recv(sockfd, buffer, len - total_received, 0);
cout << "received:" << received << endl;
if (received <= 0) {
// Connection closed or error occurred
return received;
}
total_received += received;
buffer += received; // 将缓冲区指针向前移动
}
return total_received;
}