关于c:Linux网络开发必学教程21智能家居服务发现实现

34次阅读

共计 3793 个字符,预计需要花费 10 分钟才能阅读完成。

服务设施软件

代码复用

将网络通讯框架移植到开发板,之后,可应用框架中的组件实现 Response TaskServer Task

框架移植注意事项

  • Lwip 是微型 TCP/IP 协定栈(并非残缺 TCP/IP 协定栈)
  • 反对 socket 接口,但一些性能未实现
  • socket 接口所在头文件不同于 Linux 和 Window 平台
  • 实现可执行测试,对测试中呈现的问题及时修复

可执行性测试

  • 通过网络调试助手验证 TCP 数据首发

    • 设施运行服务端,客户端连贯,数据收发性能
  • 通过网络调试助手验证 UDP 数据收发

    • 设施端接管播送,并复原数据

Server Task 要害实现

static void EventListener(TcpClient* client, int evt)
{if( evt == EVT_CONN)
    {printf("%s : CONN - %p...\n", __FUNCTION__, client);
    }
    else if(evt == EVT_DATA)
    {
        Message* msg = NULL;

        printf("%s : DATA - %p...\n", __FUNCTION__, client);

        msg = TcpClient_RecvMsg(client);

        if(msg)
        {printf("%s : msg = %p\n", __FUNCTION__, msg);
            printf("%s : msg->type = %d\n", __FUNCTION__, msg->type);
            printf("%s : msg->cmd = %d\n", __FUNCTION__, msg->cmd);
        }

        free(msg);

    }
    else if(evt == EVT_CLOSE)
    {printf("%s : CLOSE - %p...\n", __FUNCTION__, client);
    }
}

void* Service_Task(const char* arg)
{TcpServer* server = TcpServer_New();

    printf("%s : enter service task...\n", __FUNCTION__);

    if(server)
    {printf("%s : server = %p\n", __FUNCTION__, server);

        TcpServer_SetListener(server, EventListener);
        TcpServer_Start(server, 8888, 10);
        TcpServer_DoWork(server);
    }

    return arg;
}

Respone Task 要害实现

void* Response_Task(const char* arg)
{UdpPoint* udp = UdpPoint_New(9999);

    printf("%s : enter response task...\n", __FUNCTION__);

    if(udp)
    {char remote[16] = {0};
        int port = 0;

        printf("%s : udp = %p\n", __FUNCTION__, udp);

        while(1)
        {Message* msg = UdpPoint_RecvMsg(udp, remote, &port);

            if(msg)
            {printf("%s : msg = %p\n", __FUNCTION__, msg);
                printf("%s : remote = %s\n", __FUNCTION__, remote);
                printf("%s : port = %d\n", __FUNCTION__, port);
                printf("%s : msg->type = %d\n", __FUNCTION__, msg->type);
                printf("%s : msg->cmd = %d\n", __FUNCTION__, msg->cmd);
            }
            else
            {printf("%s : msg is NULL...\n", __FUNCTION__);
            }

            free(msg);
        }
    }

    return arg;
}

编程试验,网络通讯框架移植

main.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

#include "response_task.h"
#include "server_task.h"

int main(void)
{
    int ret;
    pthread_t tid1;
    pthread_t tid2;

    ret = pthread_create(&tid1, NULL, Response_Task, NULL);
    if (ret != 0) {printf("[dt4sw] Failed to create response task!\n");
    }

    ret = pthread_create(&tid2, NULL, Service_Task, NULL);
    if (ret != 0) {printf("[dt4sw] Failed to create service task!\n");
    }

    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

    return 0;
}

response_task.h

#ifndef RESPONSE_TASK_H
#define RESPONSE_TASK_H

void* Response_Task(void* arg);

#endif  // RESPONSE_TASK_H

response_task.c

#include "response_task.h"

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

#include "udp_point.h"

void* Response_Task(void* arg)
{UdpPoint *udp = UdpPoint_New(9999);

    printf("%s : enter response task ...\n", __FUNCTION__);

    if (udp) {char remote[16] = {0};
        int port = 0;

        printf("%s : udp = %p\n", __FUNCTION__, udp);

        while (1) {Message *msg = UdpPoint_RecvMsg(udp, remote, &port);

            if (msg) {printf("%s : msg = %p\n", __FUNCTION__, msg);
                printf("%s : remote = %s\n", __FUNCTION__, remote);
                printf("%s : port = %d\n", __FUNCTION__, port);
                printf("%s : msg->type = %d\n", __FUNCTION__, msg->type);
                printf("%s : msg->cmd = %d\n", __FUNCTION__, msg->cmd);
            } else {printf("%s : msg is NULL ...\n", __FUNCTION__);
            }

            free(msg);
        }
    }

    return arg;
}

server_task.h

#ifndef SERVER_TASK_H
#define SERVER_TASK_H

void* Service_Task(void* arg);

#endif // SERVER_TASK_H

server_task.c

#include "server_task.h"

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

#include "tcp_server.h"

static void EventListener(TcpClient *client, int evt)
{if (evt == EVT_CONN) {printf("%s : CONN - %p ...\n", __FUNCTION__, client);
    } else if (evt == EVT_DATA) {
        Message *msg = NULL;

        printf("%s : data -%p ...\n", __FUNCTION__, client);

        msg = TcpClient_RecvMsg(client);

        if (msg) {printf("%s : msg = %p\n", __FUNCTION__, msg);
            printf("%s : msg->type = %d\n", __FUNCTION__, msg->type);
            printf("%s : msg->cmd = %d\n", __FUNCTION__, msg->cmd);
        }

        free(msg);
    } else if (evt == EVT_CLOSE) {printf("%s : CLOSE - %p ... \n", __FUNCTION__, client);
    }
}

void* Service_Task(void* arg)
{TcpServer *server = TcpServer_New();

    printf("%s : enter server task ...\n", __FUNCTION__);

    if (server) {printf("%s : server = %p\n", __FUNCTION__, server);

        TcpServer_SetListener(server, EventListener);
        TcpServer_Start(server, 8888, 10);
        TcpServer_DoWork(server);
    }

    return arg;
}

课后思考

Response TaskServer Task 的业务逻辑如何实现?客户端如何实现?

正文完
 0