Cmakelist.txt之win-c-udp-server

1.cmakelist.txt

复制代码
cmake_minimum_required(VERSION 3.16)
​
project(c_udp_server LANGUAGES C)
​
add_executable(c_udp_server main.c)
​
# link_directories("D:/Environment/mingw64/x86_64-w64-mingw32/lib")
​
target_link_libraries(c_udp_server wsock32)
​
include(GNUInstallDirs)
install(TARGETS c_udp_server
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
​

2.测试代码

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
​
// #pragma comment(lib, "ws2_32.lib")
​
#define PORT 8888
#define BUFFER_SIZE 1024
​
int main() {
    printf("server start---\n");
    WSADATA wsaData;
    int server_socket;
    struct sockaddr_in server_addr, client_addr;
    int client_addr_len = sizeof(client_addr);
    char buffer[BUFFER_SIZE];
​
    // 初始化Winsock库
    if (WSAStartup(MAKEWORD(2, 2), &wsaData)!= 0) {
        perror("WSAStartup failed");
        return 1;
    }
​
    // 创建UDP套接字
    if ((server_socket = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
        perror("Socket creation failed");
        WSACleanup();
        return 1;
    }
​
    // 初始化服务器地址结构
    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)) == SOCKET_ERROR) {
        perror("Bind failed");
        closesocket(server_socket);
        WSACleanup();
        return 1;
    }
    printf("Server is waiting for incoming messages...\n");
    while (1) {
        // 接收来自客户端的数据
        int recv_len = recvfrom(server_socket, buffer, BUFFER_SIZE - 1, 0,
                                (struct sockaddr *)&client_addr, &client_addr_len);
        if (recv_len == SOCKET_ERROR) {
            perror("Receive failed");
            continue;
        }
        buffer[recv_len] = '\0';
​
        printf("Received message from client: %s", buffer);
​
        // 发送响应给客户端---执行实际操作数据处理
        const char *response = "Message received successfully";
        int send_len = sendto(server_socket, response, strlen(response), 0,
                              (struct sockaddr *)&client_addr, client_addr_len);
        if (send_len == SOCKET_ERROR) {
            perror("Send failed");
            continue;
        }
    }
​
    closesocket(server_socket);
    WSACleanup();
    return 0;
}
复制代码

3.结果

相关推荐
西阳未落2 小时前
C++基础(21)——内存管理
开发语言·c++·面试
我的xiaodoujiao2 小时前
Windows系统Web UI自动化测试学习系列2--环境搭建--Python-PyCharm-Selenium
开发语言·python·测试工具
callJJ2 小时前
从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(2)
java·开发语言·后端·spring·ioc·di
超级大福宝2 小时前
使用 LLVM 16.0.4 编译 MiBench 中的 patricia遇到的 rpc 库问题
c语言·c++
hsjkdhs4 小时前
万字详解C++之构造函数析构函数
开发语言·c++
Lin_Aries_04214 小时前
容器化简单的 Java 应用程序
java·linux·运维·开发语言·docker·容器·rpc
techdashen5 小时前
12分钟讲解Python核心理念
开发语言·python
闭着眼睛学算法5 小时前
【华为OD机考正在更新】2025年双机位A卷真题【完全原创题解 | 详细考点分类 | 不断更新题目 | 六种主流语言Py+Java+Cpp+C+Js+Go】
java·c语言·javascript·c++·python·算法·华为od
山海不说话5 小时前
Java后端面经(八股——Redis)
java·开发语言·redis
郝学胜-神的一滴5 小时前
谨慎地迭代函数所收到的参数 (Effective Python 第31条)
开发语言·python·程序人生·软件工程