socket通信之完整的多路复用

前一篇文章是介绍的select一个极为简单的用法,这篇将select和socket结合使用,完成单线程多用户连接,用户退出后,可用连接自动+1,。

客户端同http://blog.youkuaiyun.com/xluren/article/details/8043484 中的客户端

服务器端代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MYPORT 6666    // the port users will be connecting to

#define BACKLOG 5     // how many pending connections queue will hold

#define BUF_SIZE 1024
#define MAXCLIENT 5
int fd_access[BACKLOG];    // accepted connection fd
int conn_amount;    // current connection amount

void showclient()
{
        int i;
        printf("client amount: %d\n", conn_amount);
        printf("\n");
}

int main(void)
{
        int sock_fd, new_fd;  // listen on sock_fd, new connection on new_fd
        struct sockaddr_in server_addr;    // server address information
        struct sockaddr_in client_addr; // connector's address information
        socklen_t sin_size;
        char buf[BUF_SIZE];
        int ret;
        int i;

        if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
        {
                perror("socket");
                exit(1);
        } 
        server_addr.sin_family = AF_INET;         // host byte order
        server_addr.sin_port = htons(MYPORT);     // short, network byte order
        server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
        memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
        if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) 
        {
                perror("bind");
                exit(1);
        }
        if (listen(sock_fd, BACKLOG) == -1) 
        {
                perror("listen");
                exit(1);
        }
        printf("listen port %d\n", MYPORT);

        fd_set fdsr;
        int maxsock;
        struct timeval tv;

        conn_amount = 0;
        sin_size = sizeof(client_addr);
        maxsock = MAXCLIENT;
        FD_ZERO(&fdsr);
        while (1) 
        {
                FD_SET(sock_fd, &fdsr);

                tv.tv_sec = 3;
                tv.tv_usec = 0;

                for (i = 0; i < BACKLOG; i++) 
                {
                        printf("fd---%d\n",fd_access[i]);
                        if (fd_access[i] != 0)
                        {
                                FD_SET(fd_access[i], &fdsr);
                        }
                }

                ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv);
                if (ret < 0) 
                {
                        perror("select");
                        break;
                } 
                else if (ret == 0) 
                {
                        printf("timeout\n");
                        continue;
                }
                printf("select----ret%d",ret);
                // check every fd in the set
                for (i = 0; i < MAXCLIENT; i++) 
                {
                        if (FD_ISSET(fd_access[i], &fdsr)) 
                        {
                                ret = recv(fd_access[i], buf, sizeof(buf), 0);
                                if (ret <= 0) 
                                {
                                        printf("client[%d] close\n", i);
                                        close(fd_access[i]);
                                        FD_CLR(fd_access[i], &fdsr);
                                        fd_access[i] = 0;
                                        conn_amount--;
                                } 
                                else 
                                {
                                        if (ret < BUF_SIZE)
                                                memset(&buf[ret], '\0', 1);                    
                                        printf("client[%d] send:%s\n", i, buf);
                                }
                        }
                }
                // check whether a new connection comes
                if (FD_ISSET(sock_fd, &fdsr)) 
                {
                        new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size);
                        if (new_fd <= 0) 
                        {
                                perror("accept");
                                continue;
                        }
                        // add to fd queue
                        if (conn_amount < BACKLOG) 
                        {
                                for(i = 0;i < MAXCLIENT;i++)
                                {
                                        if(fd_access[i] == 0)
                                        {
                                                fd_access[i] = new_fd;
                                                break;
                                        }

                                }//for
                                conn_amount++;
                                printf("new connection client[%d] %s:%d\n", 
								conn_amount,inet_ntoa(client_addr.sin_addr), 
								ntohs(client_addr.sin_port));
                                if (new_fd > maxsock)
                                        maxsock = new_fd;
                        }//if
                        else 
                        {
                                printf("max connections arrive, exit\n");
                                send(new_fd, "bye", 4, 0);
                                continue;
                        }
                }//if (FD_ISSET(sock_fd, &fdsr))
                showclient();
        }//while
        // close other connections
        for (i = 0; i < BACKLOG; i++) 
        {
                if (fd_access[i] != 0) 
                {
                        close(fd_access[i]);
                }
        }
        exit(0);
}
(END) 

代码借鉴了很多牛人的博客,最后形成了这个服务器的代码,衷心的感谢各位技术牛人无私的奉献,谢谢~~·
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值