并发服务器除了可以用多线程和多进程实现以外,还可以用select实现单线程并发,下面用select实现简单的示例,服务器接收客户端的连接,并将客户发的消息返回,代码如下:
服务器端代码:
main.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
#define BUFF_LEN 1024
int client_sock;
int errexit(const char *format, ...);
int echo(int fd);
int main(){
int ss = create_tcp_server(PORT);
if(-1 == ss)
exit(-1);
struct sockaddr_in fsin;
unsigned int alen;
int fd;
fd_set rfds;/* read file descriptor set */
fd_set afds;/* active file descriptor set */
int nfds = getdtablesize();
FD_ZERO(&afds);
FD_SET(ss, &afds);
while (1) {
memcpy(&rfds, &afds, sizeof(rfds));
if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0){
errexit("select: %s\n", strerror(errno));
}
if (FD_ISSET(ss, &rfds)) {
alen = sizeof(fsin);
int ssock = accept(ss, (struct sockaddr *)&fsin, &alen);
if (ssock < 0)
errexit("accept: %s\n", strerror(errno));
else
printf("accept clinet %d\n", ssock);
FD_SET(ssock, &afds);
}
for (fd = 0; fd < nfds; ++fd){
if (fd != ss && FD_ISSET(fd, &rfds)){
if (echo(fd) <= 0) {
(void) close(fd);
FD_CLR(fd, &afds);
}
}
}
}
}
int echo(int fd){
char buf[BUFF_LEN];
int cc = read(fd, buf, sizeof(buf));
if(cc > 0){
printf("recv msg from client %d : %s\n", fd, buf);
if(write(fd, buf, cc) < 0)
printf("write to client %d error, close!\n", fd);
} else if(cc == 0)
printf("client %d disconnect\n", fd);
else
printf("read from client %d error, close!\n", fd);
return cc;
}
int errexit(const char *format, ...){
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
CreateServer.h//封装了创建服务器代码
#ifndef CREATESERVER_H
#define CREATESERVER_H
int create_tcp_server(int);
int create_udp_server(int);
#endif // CREATESERVER_H
CreateServer.c//封装方法的具体实现
#include
#include
#include
#define LISTEN_SIZE 20
int start_server(int port, int type){
//建立服务器套接字
int ss = socket(AF_INET, type, 0);
if(ss < 0){
printf("create socket error\n");
return -1;
}
//设置服务器地址
struct sockaddr_in server_addr; //服务器地址结构
bzero(&server_addr, sizeof(struct sockaddr_in)); //清零
server_addr.sin_family = AF_INET; //协议族
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //ip地址
server_addr.sin_port = htons(port); //端口
//绑定地址结构到套接字描述符
if(bind(ss, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){
printf("bind error\n");
return -1;
}
//TCP
if(SOCK_STREAM == type){
//设置侦听
if(listen(ss, LISTEN_SIZE) < 0){
printf("listen error\n");
return -1;
}
printf("tcp server start\n");
}
else
printf("udp server start\n");
return ss;
}
int create_tcp_server(int port){
start_server(port, SOCK_STREAM);
}
int create_udp_server(int port){
start_server(port, SOCK_DGRAM);
}
客户端测试代码:
代码同之前的博客,参见:http://blog.csdn.net/wanna_wsl/article/details/53712066
源码下载地址:
https://github.com/Wushaoling/Linux-C-Socket/tree/master/TCP/TCP%E7%AE%80%E5%8D%95%E6%95%B0%E6%8D%AE%E5%8F%91%E9%80%81