跳转至

TCP服务端

TCP 服务端程序开发流程的介绍

步骤说明:

  1. 创建服务端端套接字对象
  2. 绑定端口号
  3. 设置监听
  4. 等待接受客户端的连接请求
  5. 接收数据
  6. 发送数据
  7. 关闭套接字

项目编写

  1. applications/genkipi/app 下新建 tcp_server 文件夹
  2. tcp_server 下新建 main.c 文件
  3. tcp_server 下新建 BUILD.gn文件

代码部分

main.c 文件内容

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

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "wifi_sta.h"

#include "lwip/sockets.h"

#define WIFI_SSID "itheima"
#define WIFI_PASSWORD "12345678"
#define HOSTNAME "itcast"


static void tcp_task(void) {
    connect_wifi(WIFI_SSID, WIFI_PASSWORD, HOSTNAME);

    int sock_fd;
    int ret;
    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd < 0) {
        perror("[actuator_service]sock_fd create error\r\n");
        return;
    }
    // receive addr config
    struct sockaddr_in recvfrom_addr;
    socklen_t recvfrom_addr_len = sizeof(recvfrom_addr);

    memset((void *) &recvfrom_addr, 0, recvfrom_addr_len);
    recvfrom_addr.sin_family = AF_INET;
    recvfrom_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    recvfrom_addr.sin_port = htons(9090);
    // bind
    ret = bind(sock_fd, (struct sockaddr *) &recvfrom_addr, recvfrom_addr_len);
    if (ret == -1) {
        perror("bind error\r\n");
        return;
    }

    // listen, just listen 1 connect
    ret = listen(sock_fd, 1);
    if (ret == -1) {
        perror("[actuator_service]listen error\r\n");
        return;
    }

    while (1) {
        int client_fd;
        struct sockaddr_in client_addr;
        socklen_t client_addr_len = sizeof(client_addr);

        client_fd = accept(sock_fd, (struct sockaddr *) &client_addr, &client_addr_len);
        if (client_fd < 0) {
            perror("accept client error\r\n");
            continue;
        }

        char recv_buf[1024];
        int recv_len;
        while (1) {
            recv_len = recv(client_fd, recv_buf, sizeof(recv_buf), 0);
            if (recv_len <= 0) {
                break;
            }
            char recv_data[recv_len];
            memcpy(recv_data, recv_buf, recv_len);
            recv_data[recv_len] = '\0';
            printf("len: %d data: %s\r\n", recv_len, recv_data);
        }
    }
}

static void start(void) {
    osThreadAttr_t attr;

    attr.name = "tcp_server";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 4;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t) tcp_task, NULL, &attr) == NULL) {
        printf("Create udp recv task Failed!\r\n");
    }
}

APP_FEATURE_INIT(start);

项目Build.gn

tcp_server 目录下 BUILD.gn 内容为

static_library("tcp_server") {
    sources = [
        "main.c",
        "wifi_sta.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//foundation/communication/wifi_lite/interfaces/wifiservice"
    ]
}

外部Build.gn

tcp_server文件夹上一级目录下BUILD.gn内容为

import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        "udp_sender"
    ]
}