以下是一个简单的cs通信模型:
服务器:
// TCPServer.cpp
#include <iostream>
#include <thread>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
const int PORT = 8080;
const int BUFFER_SIZE = 1024;
void receive_data(int server_socket) {
char buffer[BUFFER_SIZE];
while (true) {
int valread = read(server_socket, buffer, BUFFER_SIZE);
if (valread == 0) {
std::cout << "Client disconnected." << std::endl;
break;
} else if (valread > 0) {
std::cout << "Received: " << buffer << std::endl;
} else {
perror("read failed");
break;
}
}
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket failed");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
return 1;
}
if (listen(server_socket, 5) < 0) {
perror("listen failed");
return 1;
}
std::cout << "Server listening on port " << PORT << "..." << std::endl;
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len);
if (client_socket < 0) {
perror("accept failed");
return 1;
}
std::thread receiver_thread(receive_data, client_socket);
// Send data in the main thread
char send_buffer[BUFFER_SIZE] = "Hello from server!";
while (true) {
int valwrite = write(client_socket, send_buffer, strlen(send_buffer));
if (valwrite < 0) {
perror("write failed");
break;
}
sleep(1); // Wait for 1 second before sending another message
}
receiver_thread.join();
close(client_socket);
close(server_socket);
return 0;
}
客户端:
// TCPClient.cpp
#include <iostream>
#include <thread>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
const char* SERVER_IP = "127.0.0.1";
const int PORT = 8080;
const int BUFFER_SIZE = 1024;
void send_data(int client_socket) {
char send_buffer[BUFFER_SIZE] = "Hello from client!";
while (true) {
int valwrite = write(client_socket, send_buffer, strlen(send_buffer));
if (valwrite < 0) {
perror("write failed");
break;
}
sleep(1); // Wait for 1 second before sending another message
}
}
void receive_data(int client_socket) {
char buffer[BUFFER_SIZE];
while (true) {
int valread = read(client_socket, buffer, BUFFER_SIZE);
if (valread == 0) {
std::cout << "Server disconnected." << std::endl;
break;
} else if (valread > 0) {
std::cout << "Received: " << buffer << std::endl;
} else {
perror("read failed");
break;
}
}
}
int main() {
int client_socket;
struct sockaddr_in server_addr;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket failed");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect failed");
return 1;
}
std::thread sender_thread(send_data, client_socket);
std::thread receiver_thread(receive_data, client_socket);
sender_thread.join();
receiver_thread.join();
close(client_socket);
return 0;
}
g++ -o TCPServer TCPServer.cpp
g++ -o TCPClient TCPClient.cpp
# 在一个终端中运行服务器 ./TCPServer
# 在另一个终端中运行客户端 ./TCPClient
运行结果: