windows环境下udp服务等待监听消息,监听到消息给回复一个提示,并cjson解析对方传过来的json格式字符串,是json就reboot

 udpserver.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。


#include <cstring>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib,"ws2_32.lib")

#define BUF_SIZE 1024

int main()
{
    WSADATA wsa;
    int err = WSAStartup(MAKEWORD(2, 2), &wsa);
    if (err != 0) {
        printf("failed to init wsdata! Error Code : %d", WSAGetLastError());
        return 1;
    }

    SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock == INVALID_SOCKET)
    {
        printf("failed to create socket! Error Code : %d", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // bind socket
    sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(1234);
    if (bind(sock, (sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
    {
        printf("failed to bind sock! Error Code : %d", WSAGetLastError());
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    printf("listening to data from...\n");

    // receive UDP 数据包 and send msg
    char buf[BUF_SIZE] = { 0 };
    sockaddr_in client_addr;
    int len = sizeof(client_addr);
    while (true) {
        int n = recvfrom(sock, buf, BUF_SIZE - 1, 0, (sockaddr*)&client_addr, &len);
        
        if (n == SOCKET_ERROR)
        {
            printf("failed  to recv data! Error Code : %d", WSAGetLastError());
            continue;
        }
        printf("message content: %s\n", buf);
        if (buf[0] == 'q') {
            printf("Exiting...");
            break;
        }

        buf[n] = '\0';

        char ip[16];
        memset(ip, 0, sizeof(ip));
        inet_ntop(AF_INET, &client_addr.sin_addr, ip, sizeof(ip));
        printf("Server listening on [%s:%d]\n", ip, ntohs(server_addr.sin_port));

        const char* reply = "Hello from server!";
        if (sendto(sock, reply, strlen(reply), 0, (sockaddr*)&client_addr, sizeof(client_addr)) == SOCKET_ERROR)
        {
            printf("failed to sendto server! Error Code : %d", WSAGetLastError());
            continue;
        }
        printf("reply sent!\n");
    }

    // cleanup source
    closesocket(sock);
    WSACleanup();
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
假设你有一个名为test.jsonJSON文件,其中包含三个JSON数据,最后一个数据又嵌套了一个JSON,可以按照以下步骤使用cJSON解析: 1. 读取JSON文件内容,并将其存储在一个字符串中。 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include "cJSON.h" int main() { FILE *fp = fopen("test.json", "r"); if (fp == NULL) { printf("Failed to open file!\n"); return 1; } fseek(fp, 0, SEEK_END); long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); char *json_str = (char *)malloc((file_size + 1) * sizeof(char)); memset(json_str, 0, (file_size + 1) * sizeof(char)); fread(json_str, sizeof(char), file_size, fp); fclose(fp); ``` 2. 使用cJSON_Parse函数解析JSON字符串。 ``` cJSON *json = cJSON_Parse(json_str); if (json == NULL) { printf("Failed to parse JSON string!\n"); free(json_str); return 1; } ``` 3. 使用cJSON_GetArrayItem函数获取JSON数组中的元素,并使用cJSON_Print函数将元素的值打印出来。 ``` cJSON *item1 = cJSON_GetArrayItem(json, 0); if (item1 != NULL) { printf("item1: %s\n", cJSON_Print(item1)); } cJSON *item2 = cJSON_GetArrayItem(json, 1); if (item2 != NULL) { printf("item2: %s\n", cJSON_Print(item2)); } cJSON *item3 = cJSON_GetArrayItem(json, 2); if (item3 != NULL) { printf("item3: %s\n", cJSON_Print(item3)); } ``` 4. 对于第三个JSON数据,使用cJSON_GetObjectItem函数获取嵌套的JSON对象,并使用cJSON_Print函数将对象的值打印出来。 ``` cJSON *obj = cJSON_GetObjectItem(item3, "obj"); if (obj != NULL) { printf("obj: %s\n", cJSON_Print(obj)); } ``` 5. 释放内存。 ``` cJSON_Delete(json); free(json_str); return 0; } ``` 注意,使用cJSON解析JSON时需要注意内存分配和释放,以及错误处理等问题。上述代码只是一个简单的示例,实际使用时需要根据具体情况进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHANGα

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值