服务端
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
#define SERVICE_IP "192.168.X.X"
#define SERVICE_PORT (1024 + 256)
#define SERVICE_BUFFER_SIZE 1024
int main() {
// 初始化 WinSocket
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
std::cout << "初始化失败" << std::endl;
WSACleanup();
return -1;
}
// 创建用于监听的 socket
SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if (listen_socket == -1) {
std::cout << "创建 socket 失败" << std::endl;
WSACleanup();
return -1;
}
// 绑定服务器信息和端口
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = SERVICE_PORT;
addr.sin_addr.S_un.S_addr = inet_addr(SERVICE_IP);
if (bind(listen_socket, (sockaddr*)&addr, sizeof(addr)) == -1) {
std::cout << "绑定 socket 失败" << std::endl;
WSACleanup();
return -1;
}
// 监听绑定的端口
if (listen(listen_socket, 5) == -1) {
std::cout << "监听 socket 失败" << std::endl;
WSACleanup();
return -1;
}
// 监听到客户端的连接请求后创建 socket 进行连接
sockaddr client;
int client_len = sizeof(client);
SOCKET client_socket = accept(listen_socket, &client, &client_len);
if (client_socket == -1) {
std::cout << "接收客户端 socket 失败" << std::endl;
WSACleanup();
return -1;
}
// 发送信息到客户端
char service_write_buffer[SERVICE_BUFFER_SIZE] = "服务器已接收客户端请求";
if (send(client_socket, service_write_buffer, sizeof(service_write_buffer), 0) == -1) {
std::cout << "服务端发送响应信息失败" << std::endl;
WSACleanup();
return -1;
}
// 接收客户端发送来的信息
char service_read_buffer[SERVICE_BUFFER_SIZE];
if (recv(client_socket, service_read_buffer, SERVICE_BUFFER_SIZE, 0) <= 0) {
std::cout << "接收客户端发送信息失败" << std::endl;
WSACleanup();
return -1;
}
std::cout << "客户端请求: " << service_read_buffer << std::endl;
WSACleanup();
return 0;
}
客户端
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
#define SERVICE_IP "192.168.X.X"
#define SERVICE_PORT (1024 + 256)
#define CLIENT_BUFFER_SIZE 1024
int main() {
// 初始化 WinSocket
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
std::cout << "初始化失败" << std::endl;
WSACleanup();
return -1;
}
// 创建 socket 用于连接服务端
SOCKET client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
std::cout << "创建 socket 失败" << std::endl;
WSACleanup();
return -1;
}
// 设置服务端地址和端口信息并进行连接
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = SERVICE_PORT;
addr.sin_addr.S_un.S_addr = inet_addr(SERVICE_IP);
if (connect(client_socket, (sockaddr*)&addr, sizeof(addr)) == -1) {
std::cout << "连接服务端失败" << std::endl;
WSACleanup();
return -1;
}
// 接收服务端发送来的响应信息
char client_read_buffer[CLIENT_BUFFER_SIZE];
if (recv(client_socket, client_read_buffer, CLIENT_BUFFER_SIZE, 0) <= 0) {
std::cout << "接收服务端响应失败" << std::endl;
WSACleanup();
return -1;
}
else {
std::cout << "服务端响应: " << client_read_buffer << std::endl;
}
// 发送信息到服务端
char client_write_buffer[CLIENT_BUFFER_SIZE] = "客户端已接收到服务端响应";
if (send(client_socket, client_write_buffer, sizeof(client_write_buffer), 0) == -1) {
std::cout << "向服务端发送信息失败" << std::endl;
WSACleanup();
return -1;
}
WSACleanup();
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-696804.html
文章来源:https://www.toymoban.com/news/detail-696804.html
到了这里,关于C++ Windows Socket 简单示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!