Linux系统下UDP通信过程的C语言实现

本文介绍如何使用C语言在Linux系统下实现UDP通信。重点包括对等模式的实现,处理UDP发送和接收过程中的阻塞问题,通过Setsockopt设置阻塞时间和非阻塞模式,并展示了一个简单的数值递增通信示例。
摘要由CSDN通过智能技术生成

需要实现的功能要点如下:

1)通过对等模式实现通信。

2)因为UDP的程序是通过send和recv交替运行的,需要注意阻塞问题。通过Setsockopt,设置阻塞时间;设置read/recv为非阻塞模式。

3)设初值为1,每次+1,直至增加到n。

详细代码如下:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>


/******************************** Locals ************************************/
int           iUdpSockfd;                 // define server sockfd
static int    iIplength    = 15;          // IP address length
const  int    iMax         = 10;          // set the max number

/******************************** Code **************************************/

/*****************************************************************************
* Function   : KillHandle
* Description: receive the ctrl+c, and close the parent's socket and exit proc
* Parameter  :
* Return     : void
* Explain    : NO
***********************************
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值