C/C++ 工程简单例子——udp通信

一、概述

以Linux环境中的udp通信举例。

工程的目录结构是这样的。

tree

include文件夹存放各类头文件;udp_comm文件夹存放udp_comm.c(udp通道的打开、关闭相关操作);data_process文件夹存放data_process.c(数据的处理相关操作);test文件夹存放udp_recv.c和udp_send.c(测试程序)等文件。

二、工程文件

1、include文件夹

include

  • comm_errno.h文件
//comm_errno.h
#ifndef _COMM_ERRNO_H_
#define _COMM_ERRNO_H_


#define ERR_FREE 0

#define ERR_CREATE_SOCK 0x101
#define ERR_BIND_SOCK   0x102
#define ERR_SET_SOCKOPT 0x103
#define ERR_RECVFROM    0x104


#endif
  • data_process.h文件
//data_process.h
#ifndef _DATA_PROCESS_H_
#define _DATA_PROCESS_H_

//函数声明
//处理数据
int recv_data_frm_pc_process();

#endif
  • dbg_color.h文件
//dbg_color.h
#ifndef _PUBLIC_DEBUG_H_
#define _PUBLIC_DEBUG_H_
#include <stdio.h>

//颜色格式:"\033[背景颜色;字体颜色m字符串\033[0m"

#define OPEN_COLOR                     "\033[1m"

#define BACKGROUND_COLOR_BLACK         "\033[40;37m"
#define BACKGROUND_COLOR_RED           "\033[41;37m"
#define BACKGROUND_COLOR_YELLOW        "\033[43;37m"
#define BACKGROUND_COLOR_GREEN         "\033[42;37m"

#define CLOSE_COLOR                    "\033[0m"

#define INFO_LOG_EN
#define ERROR_LOG_EN
#define WARNING_LOG_EN
#define RIGHT_LOG_EN

#ifdef INFO_LOG_EN
#define info_log(fmt, arg...)  do{
     
      \
	printf(OPEN_COLOR BACKGROUND_COLOR_BLACK "[info_log]" CLOSE_COLOR "%s,%s():%d," fmt "\n",__FILE__, __FUNCTION__, __LINE__, ##arg); \
    fflush(stdout);                \
}while(0)
#else
#define info_log(fmt, arg...)
#endif

#ifdef ERROR_LOG_EN
#define error_log(fmt, arg...)  do{
     
      \
	printf(OPEN_COLOR BACKGROUND_COLOR_RED "[error_log]" CLOSE_COLOR "%s,%s():%d," fmt "\n"