《Linux网络开发必学教程》21_智能家居服务发现实现

服务设备软件

《Linux网络开发必学教程》21_智能家居服务发现实现_第1张图片

代码复用

将网络通讯框架移植到开发板,之后,可使用框架中的组件实现 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
#include
#include
#include

#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 
#include 
#include 

#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 
#include 
#include 

#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 的业务逻辑如何实现?客户端如何实现?

你可能感兴趣的:(clinux网络编程)