请用c++写一段服务端和客户端通信的简单代码并解释
时间: 2024-02-19 17:58:25 浏览: 78
服务端代码:
```cpp
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
int main() {
// 创建 socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
std::cerr << "Failed to create socket\n";
return 1;
}
// 设置地址重用
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
std::cerr << "Failed to set socket option\n";
return 1;
}
// 绑定地址和端口
struct sockaddr_in server_addr{};
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8888);
if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
std::cerr << "Failed to bind socket\n";
return 1;
}
// 开始监听
if (listen(server_fd, 5) == -1) {
std::cerr << "Failed to listen on socket\n";
return 1;
}
std::cout << "Server started, listening on port 8888...\n";
// 接受客户端连接并发送欢迎消息
int client_fd = accept(server_fd, nullptr, nullptr);
const char *welcome_message = "Welcome to the server!";
send(client_fd, welcome_message, strlen(welcome_message), 0);
// 接收客户端消息并回复
char buffer[1024]{};
while (true) {
int bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
if (bytes_received == -1) {
std::cerr << "Failed to receive message\n";
break;
}
if (bytes_received == 0) {
std::cout << "Client disconnected\n";
break;
}
std::cout << "Received message from client: " << buffer << std::endl;
const char *reply_message = "Message received!";
send(client_fd, reply_message, strlen(reply_message), 0);
}
close(client_fd);
close(server_fd);
return 0;
}
```
客户端代码:
```cpp
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
int main() {
// 创建 socket
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd == -1) {
std::cerr << "Failed to create socket\n";
return 1;
}
// 连接到服务端
struct sockaddr_in server_addr{};
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8888);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
if (connect(client_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
std::cerr << "Failed to connect to server\n";
return 1;
}
std::cout << "Connected to server\n";
// 接收欢迎消息
char buffer[1024]{};
int bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
if (bytes_received == -1) {
std::cerr << "Failed to receive message\n";
return 1;
}
std::cout << "Received welcome message from server: " << buffer << std::endl;
// 发送消息并接收回复
const char *message = "Hello, server!";
send(client_fd, message, strlen(message), 0);
bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
if (bytes_received == -1) {
std::cerr << "Failed to receive message\n";
return 1;
}
std::cout << "Received reply message from server: " << buffer << std::endl;
close(client_fd);
return 0;
}
```
这段代码实现了一个简单的服务端和客户端通信,服务端监听端口 8888,并在客户端连接成功后发送欢迎消息。客户端连接到服务端后接收欢迎消息,并发送一条消息给服务端,服务端接收到消息后回复一条消息给客户端。连接结束后关闭 socket。
阅读全文