这个程序可以用来建立点对多点 UDP 通信模型. 在编写 MGCP 等类 似的 UDP 的通信程序的时候, 会碰到一个问题,就是只有一个 socket 和所有的client 通信,每次接收 packet 的时候,都要判断 是谁发过来的 packet ,然后把packet 分发到不同的处理程序中. UDP Server 解决这个问题, Server 端程序中对每一个 client 的 处理函数有单独的 file descriptor or socket 用来和 client 通 信.
源代码中包含一个简单的测试程序,作为客户端. 他也是一个简单的 UDP 程序的例子. main.c 是一个使用 API 的一个例子,提供了一个 服务器端的程序,可以实现简单的会议室功能.
头文件中的 API 接口.
#ifndef _UDPSERVER_H__118637141_ #define _UDPSERVER_H__118637141_ #ifdef __cplusplus extern "C"{ #endif #ifndef UDP_SERVER_MAX_BUFFER_SIZE #define UDP_SERVER_MAX_BUFFER_SIZE 1024 #endif #include <stdint.h> #include <unistd.h> #include <sys/select.h> struct udpserver{ int fd; // the socket fd for the listen socket int maxconn; // then length of array fds int * fds; // the client socket for communication char buffer[UDP_SERVER_MAX_BUFFER_SIZE+1]; // a buffer for the listen. int buffer_len; // received data size in buffer int buffer_owner; // index for the array of client // socket. -1 is the invalid value. }; /* solution 1: 0. select on p->fd, return 1,if data come. 1. recvfrom(MSG_PEAK) p->fd, peek the sender address, 2. create a new connected socket fds[i]. 3. select on fds[i], return 1 4. ???read on fds[i], block, can not get the orignal data. 5. select on p->fd, return 1, immediately, because of orignal data. 6. recvfrom(MSG_PEEK) p->fd, return a other. so I have to do some change. */ // Create a UDP server // addr is a server address. // maxconn is the maximum connection can be connect to the server. struct udpserver * udpserver_create(const char * addr, uint16_t port, int maxconn); // void udpserver_destroy(struct udpserver * p); // write to all of client, return the result of writing to // the last client. int udpserver_write(struct udpserver *p,void * buffer, socklen_t len); // write to a specific client. // index indicates which client should be written to. // return results of system call write. // if write failed, the connection is lost. int udpserver_write_to(struct udpserver * p,int index, void * buffer, socklen_t len); // read from all of client. // from will be the index of the client socket. -1 means // read nothing. // block is true, then calling this function in blocking // mode, otherwise in on block mode. // return 0, if read nothing. // return -1, if failed. if failed, the connection is lost. // return the number of bytes read. int udpserver_read(struct udpserver *p,void * buffer,socklen_t len,int *from,int block); // read from a specific client. index identifies the client socket. int udpserver_read_from(struct udpserver *p,int index,void *buffer,socklen_t len); // no connection request, return -2 otherwise return index // of client. failed return -1; whenever accept retrun a new // client socket index, server buffer has some data for the // client. i.e. when the server receive the first packet, // the packet is regarded as connection request. and the // packet is expected be to read immediately. otherwise the // new coming client will overwrite the data buffer, the // packet will be lost. ptv is the time to be waiting for, // and passed to select function. int udpserver_accept(struct udpserver *p,struct timeval * ptv); // return the peer name like 192.168.0.2:129 const char * udpserver_getpeername(struct udpserver *p,int index); // of course close the connection. int udpserver_closeconn(struct udpserver *p, int index); #ifdef __cplusplus } #endif #endif