需要实现的功能要点如下:
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
*****************************

本文介绍如何使用C语言在Linux系统下实现UDP通信。重点包括对等模式的实现,处理UDP发送和接收过程中的阻塞问题,通过Setsockopt设置阻塞时间和非阻塞模式,并展示了一个简单的数值递增通信示例。
最低0.47元/天 解锁文章
1363

被折叠的 条评论
为什么被折叠?



