基于UDP的网络编程

UDP服务端

复制代码
#ifdef _WIN32                           
#define _WINSOCK_DEPRECATED_NO_WARNINGS  
                                         
#define close closesocket              
#include <winsock2.h>
#else  
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PORT 8080  
#define MAXLINE 1024

#ifdef _WIN32
void cleanup() { WSACleanup(); }
#endif


int main()
{
#ifdef _WIN32
    WSADATA wsData;
    if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        perror("WSA Startup error: \n");
        return 0;
    }

    atexit(cleanup); 
#endif

    int sockfd;
    char buffer[MAXLINE];
    char *hello = "Hello from server";
    struct sockaddr_in servaddr, cliaddr;

    /* 创建socket */
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&servaddr, 0, sizeof(servaddr));
    memset(&cliaddr, 0, sizeof(cliaddr));

    servaddr.sin_family = AF_INET;  // IPv4
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(PORT);

    /* 绑定服务端地址 */
    if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    unsigned int len;
    int n;
    n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL,
                 (struct sockaddr *)&cliaddr, &len);
    buffer[n] = '\0';
    printf("Client : %s\n", buffer);
    sendto(sockfd, (const char *)hello, strlen(hello), 0,
           (const struct sockaddr *)&cliaddr, len);
    printf("Hello message sent.\n");

    close(sockfd);

    return 0;
}

UDP客户端

复制代码
#ifdef _WIN32                           
#define _WINSOCK_DEPRECATED_NO_WARNINGS  
                                         
#include <winsock2.h>
#define close closesocket 
#else                  
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PORT 8080 
#define MAXLINE 1024

#ifdef _WIN32
void cleanup() { WSACleanup(); }
#endif

int main()
{
#ifdef _WIN32
    WSADATA wsData;
    if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        perror("WSA Startup error: \n");
        return 0;
    }

    atexit(cleanup); 
#endif

    int sockfd;
    char buffer[MAXLINE];
    char *hello = "Hello from client";
    struct sockaddr_in servaddr;

    /* 创建用户数据报类型的socket */
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&servaddr, 0, sizeof(servaddr));

    /* 填充协议信息 */
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = INADDR_ANY;

    int n;
    unsigned int len;

    sendto(sockfd, (const char *)hello, strlen(hello), 0,
           (const struct sockaddr *)&servaddr, sizeof(servaddr));
    printf("Hello message sent.\n");

    n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL,
                 (struct sockaddr *)&servaddr, &len);
    buffer[n] = '\0';
    printf("Server : %s\n", buffer);

    close(sockfd);
    return 0;
}

客户端运行结果:

Hello message sent.

Server : Hello from server

服务端运行结果:

Client : Hello from client

Hello message sent.

相关推荐
00后程序员张9 小时前
移动网页调试的多元路径:WebDebugX 与其他调试工具的组合使用策略
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9160137410 小时前
iOS 应用如何防止源码与资源被轻易还原?多维度混淆策略与实战工具盘点(含 Ipa Guard)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
?!7142 天前
Socket网络编程之UDP套件字
linux·网络·c++·网络协议·udp·php
宾有为2 天前
【Android】如何抓取 Android 设备的 UDP/TCP 数据包?
android·tcp/ip·udp·wireshark·抓包·tcp抓包·udp抓包
00后程序员张3 天前
面对 UI 差异化的调试难题:本地多设备测试中的 WebDebugX 应用实录
websocket·网络协议·tcp/ip·http·网络安全·https·udp
巴巴_羊3 天前
前端八股 tcp 和 udp
网络协议·tcp/ip·udp
中云时代-防御可测试-小余3 天前
怎么选择合适的高防IP
服务器·网络·网络协议·tcp/ip·阿里云·udp·ddos
孤寂大仙v4 天前
【计算机网络】传输层UDP协议
计算机网络·udp
趙卋傑4 天前
网络编程套接字
java·udp·网络编程·tcp
2501_916013744 天前
用 Appuploader,让 iOS 上架流程真正“可交接、可记录、可复用”:我们是这样实现的
websocket·网络协议·tcp/ip·http·网络安全·https·udp