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