10a7ce71fSopenharmony_ci/* 20a7ce71fSopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. 30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License. 50a7ce71fSopenharmony_ci * You may obtain a copy of the License at 60a7ce71fSopenharmony_ci * 70a7ce71fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80a7ce71fSopenharmony_ci * 90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and 130a7ce71fSopenharmony_ci * limitations under the License. 140a7ce71fSopenharmony_ci */ 150a7ce71fSopenharmony_ci 160a7ce71fSopenharmony_ci#include <stdlib.h> 170a7ce71fSopenharmony_ci#include <stdio.h> 180a7ce71fSopenharmony_ci#include <string.h> 190a7ce71fSopenharmony_ci#include <errno.h> 200a7ce71fSopenharmony_ci#include "lwip/netifapi.h" 210a7ce71fSopenharmony_ci 220a7ce71fSopenharmony_ci#define CLIENT_SOCKET_CREATE_FAILED (-1) 230a7ce71fSopenharmony_ci#define UDP_SERVER_PORT (6655) /* TEST Port */ 240a7ce71fSopenharmony_ci#define UDP_CLIENT_PORT (5566) /* TEST Port */ 250a7ce71fSopenharmony_ci 260a7ce71fSopenharmony_ci#define UDP_RECV_BUF_LEN (255) 270a7ce71fSopenharmony_ci 280a7ce71fSopenharmony_ciint UdpClientDemo(void) 290a7ce71fSopenharmony_ci{ 300a7ce71fSopenharmony_ci struct sockaddr_in sin = {0}; 310a7ce71fSopenharmony_ci struct sockaddr_in serverAddr = {0}; 320a7ce71fSopenharmony_ci /* create socket */ 330a7ce71fSopenharmony_ci int sClient = socket(AF_INET, SOCK_DGRAM, 0); 340a7ce71fSopenharmony_ci if (sClient == CLIENT_SOCKET_CREATE_FAILED) { 350a7ce71fSopenharmony_ci printf("create socket Fialed\r\n"); 360a7ce71fSopenharmony_ci close(sClient); 370a7ce71fSopenharmony_ci return CLIENT_SOCKET_CREATE_FAILED; 380a7ce71fSopenharmony_ci } 390a7ce71fSopenharmony_ci // 本地主机ip和端口号 400a7ce71fSopenharmony_ci sin.sin_family = AF_INET; 410a7ce71fSopenharmony_ci sin.sin_port = htons(UDP_SERVER_PORT); /* server端口号6655 */ 420a7ce71fSopenharmony_ci sin.sin_addr.s_addr = inet_addr("XXX.XXX.X.XXX"); 430a7ce71fSopenharmony_ci int ret = bind(sClient, (struct sockaddr*)&sin, sizeof(sin)); 440a7ce71fSopenharmony_ci if (ret < 0) { 450a7ce71fSopenharmony_ci printf("client bind failed %d, %s\r\n", __LINE__, errno); 460a7ce71fSopenharmony_ci } 470a7ce71fSopenharmony_ci // 对方ip和端口号 480a7ce71fSopenharmony_ci serverAddr.sin_family = AF_INET; 490a7ce71fSopenharmony_ci serverAddr.sin_port = htons(UDP_CLIENT_PORT); /* client端口号5566 */ 500a7ce71fSopenharmony_ci serverAddr.sin_addr.s_addr = inet_addr("XXXX.XXX.X.XXX"); 510a7ce71fSopenharmony_ci 520a7ce71fSopenharmony_ci int len = sizeof(sin); 530a7ce71fSopenharmony_ci char *sendData = "这是客户端的消息\r\n"; 540a7ce71fSopenharmony_ci while (1) { 550a7ce71fSopenharmony_ci sendto(sClient, sendData, strlen(sendData), 0, (struct sockaddr*)&serverAddr, len); 560a7ce71fSopenharmony_ci char recvData[UDP_RECV_BUF_LEN] = {0}; 570a7ce71fSopenharmony_ci /* buff大小设置为255 */ 580a7ce71fSopenharmony_ci int recvLen = recvfrom(sClient, recvData, UDP_RECV_BUF_LEN, 0, (struct sockaddr*)&serverAddr, &len); 590a7ce71fSopenharmony_ci if (recvLen) { 600a7ce71fSopenharmony_ci printf("recv_data = %s\r\n", recvData); 610a7ce71fSopenharmony_ci } 620a7ce71fSopenharmony_ci } 630a7ce71fSopenharmony_ci close(sClient); 640a7ce71fSopenharmony_ci return 0; 650a7ce71fSopenharmony_ci}