c++socket编程cs架构
时间: 2023-08-31 16:07:57 浏览: 105
C++socket编程
C++中的Socket编程是一种用于创建网络应用程序的常见方法,它基于传输控制协议(TCP)或用户数据报协议(UDP)来实现不同的通信需求。CS架构(Client-Server架构)是一种常见的网络应用程序设计模式,其中服务器端提供服务,而客户端通过Socket连接请求服务。
在C++中进行Socket编程,可以使用操作系统提供的原生Socket API或者一些封装库,如Boost.Asio、Poco、ACE等。下面是一个简单的CS架构的Socket编程示例:
服务器端代码:
```cpp
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
// 创建Socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cerr << "Failed to create socket" << std::endl;
return 1;
}
// 绑定IP地址和端口
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(12345);
if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
std::cerr << "Failed to bind address" << std::endl;
close(serverSocket);
return 1;
}
// 监听连接请求
if (listen(serverSocket, 5) == -1) {
std::cerr << "Failed to listen" << std::endl;
close(serverSocket);
return 1;
}
// 接受客户端连接
sockaddr_in clientAddress{};
socklen_t clientAddressLength = sizeof(clientAddress);
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressLength);
if (clientSocket == -1) {
std::cerr << "Failed to accept client connection" << std::endl;
close(serverSocket);
return 1;
}
// 接收和发送数据
char buffer[1024];
std::memset(buffer, 0, sizeof(buffer));
if (recv(clientSocket, buffer, sizeof(buffer), 0) == -1) {
std::cerr << "Failed to receive data" << std::endl;
close(clientSocket);
close(serverSocket);
return 1;
}
std::cout << "Received message from client: " << buffer << std::endl;
const char* response = "Hello from server!";
if (send(clientSocket, response, std::strlen(response), 0) == -1) {
std::cerr << "Failed to send data" << std::endl;
close(clientSocket);
close(serverSocket);
return 1;
}
// 关闭连接
close(clientSocket);
close(serverSocket);
return 0;
}
```
客户端代码:
```cpp
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
// 创建Socket
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) {
std::cerr << "Failed to create socket" << std::endl;
return 1;
}
// 连接服务器
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(12345);
if (inet_pton(AF_INET, "127.0.0.1", &(serverAddress.sin_addr)) <= 0) {
std::cerr << "Invalid address" << std::endl;
close(clientSocket);
return 1;
}
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
std::cerr << "Failed to connect to server" << std::endl;
close(clientSocket);
return 1;
}
// 发送和接收数据
const char* message = "Hello from client!";
if (send(clientSocket, message, std::strlen(message), 0) == -1) {
std::cerr << "Failed to send data" << std::endl;
close(clientSocket);
return 1;
}
char buffer[1024];
std::memset(buffer, 0, sizeof(buffer));
if (recv(clientSocket, buffer, sizeof(buffer), 0) == -1) {
std::cerr << "Failed to receive data" << std::endl;
close(clientSocket);
return 1;
}
std::cout << "Received message from server: " << buffer << std::endl;
// 关闭连接
close(clientSocket);
return 0;
}
```
上述代码展示了一个简单的CS架构的Socket编程示例,其中服务器端监听连接请求并与客户端进行通信,客户端通过Socket连接服务器并发送消息,服务器接收消息并回复。你可以根据实际需求进行修改和扩展。
阅读全文