c++ 怎么运行socket
时间: 2023-09-23 17:04:51 浏览: 219
要在C++中运行Socket,你需要使用Socket库,如BSD Socket或Winsock。以下是一个简单的示例代码,展示了如何在C++中创建一个基本的TCP客户端和服务器。
首先,让我们看看如何创建一个简单的TCP服务器:
```cpp
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
// 创建socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cout << "无法创建socket!" << std::endl;
return 1;
}
// 绑定地址和端口
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY; // 绑定所有可用的网络接口
serverAddress.sin_port = htons(12345); // 绑定到端口12345
if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
std::cout << "绑定失败!" << std::endl;
return 1;
}
// 监听连接
if (listen(serverSocket, 5) == -1) {
std::cout << "监听失败!" << std::endl;
return 1;
}
std::cout << "等待客户端连接..." << std::endl;
// 接受连接
int clientSocket = accept(serverSocket, nullptr, nullptr);
if (clientSocket == -1) {
std::cout << "无法接受连接!" << std::endl;
return 1;
}
std::cout << "客户端已连接!" << std::endl;
// 发送和接收数据
char buffer[1024];
std::string message;
while (message != "exit") {
memset(buffer, 0, sizeof(buffer));
std::cout << "等待客户端消息..." << std::endl;
// 接收消息
int bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesRead == -1) {
std::cout << "接收消息失败!" << std::endl;
break;
}
std::cout << "接收到消息:" << buffer << std::endl;
std::cout << "请输入要发送给客户端的消息(输入“exit”退出):";
std::getline(std::cin, message);
// 发送消息
if (send(clientSocket, message.c_str(), message.size(), 0) == -1) {
std::cout << "发送消息失败!" << std::endl;
break;
}
}
// 关闭socket
close(clientSocket);
close(serverSocket);
return 0;
}
```
接下来,让我们看看如何创建一个简单的TCP客户端:
```cpp
#include <iostream>
#include <string>
#include <sys/types.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::cout << "无法创建socket!" << std::endl;
return 1;
}
// 设置服务器地址和端口
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(12345); // 服务器端口
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
// 连接服务器
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
std::cout << "连接服务器失败!" << std::endl;
return 1;
}
std::cout << "已连接到服务器!" << std::endl;
// 发送和接收数据
std::string message;
while (message != "exit") {
std::cout << "请输入要发送给服务器的消息(输入“exit”退出):";
std::getline(std::cin, message);
// 发送消息
if (send(clientSocket, message.c_str(), message.size(), 0) == -1) {
std::cout << "发送消息失败!" << std::endl;
break;
}
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
std::cout << "等待服务器消息..." << std::endl;
// 接收消息
int bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesRead == -1) {
std::cout << "接收消息失败!" << std::endl;
break;
}
std::cout << "接收到消息:" << buffer << std::endl;
}
// 关闭socket
close(clientSocket);
return 0;
}
```
这只是一个基本的示例,用于演示如何在C++中运行Socket。你可以根据自己的需求和具体场景进行修改和扩展。记得包含正确的头文件并链接相应的库,以确保代码可以正常编译和运行。
阅读全文