1. 程式人生 > >select函式(一)

select函式(一)

#include <sys/select.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

引數:

  • nfds:監控的檔案描述符集裡最大檔案描述符加1,因為此引數會告訴核心檢測前多少個檔案檔案描述符
  • readfs:監控有讀資料到達檔案描述符集合,傳入傳出引數
  • writefds:監控寫資料到達檔案描述符集合,傳入傳出引數
  • exceptfds:監控異常發生到達檔案描述符集合,如帶外資料到達異常,傳入傳出引數
  • timeout:定時阻塞監控時間。

 

#include <sys/select.h>                 
void FD_CLR(int fd, fd_set *set);     //把檔案描述符集裡fd位清0
int  FD_ISSET(int fd, fd_set *set);   //測試檔案描述符集裡fd是否置1
void FD_SET(int fd, fd_set *set);     //把檔案描述符集合裡fd位置1
void FD_ZERO(fd_set *set);            //把檔案描述符集合裡所有位清0

二、select函式用法圖解

三、實驗一

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>

void perr_exit(const char *s)
{
    perror(s);
    exit(-1);
}

int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
    int n;

again:
    if ((n = accept(fd, sa, salenptr)) < 0) 
    {
        //ECONNABORTED 發生在重傳(一定次數)失敗後,強制關閉套接字
        //EINTR 程序被訊號中斷
        if ((errno == ECONNABORTED) || (errno == EINTR))
        {
            goto again; 
        }
        else
        {
            perr_exit("accept error");
        }
    }
    return n;
}

int Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
    int n;

    if ((n = bind(fd, sa, salen)) < 0)
    {
        perr_exit("bind error");
    }

    return n;
}

int Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
    int n;
    n = connect(fd, sa, salen);
    if (n < 0) 
    {
        perr_exit("connect error");
    }

    return n;
}

int Listen(int fd, int backlog)
{
    int n;

    if ((n = listen(fd, backlog)) < 0)
    {
        perr_exit("listen error");
    }

    return n;
}

int Socket(int family, int type, int protocol)
{
    int n;

    if ((n = socket(family, type, protocol)) < 0)
    {
        perr_exit("socket error");
    }

    return n;
}

ssize_t Read(int fd, void *ptr, size_t nbytes)
{
    ssize_t n;

again:
    if ( (n = read(fd, ptr, nbytes)) == -1) 
    {
        if (errno == EINTR)
            goto again;
        else
            return -1;
    }

    return n;
}

ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
    ssize_t n;

again:
    if ((n = write(fd, ptr, nbytes)) == -1) 
    {
        if (errno == EINTR)
            goto again;
        else
            return -1;
    }
    return n;
}

int Close(int fd)
{
    int n;
    if ((n = close(fd)) == -1)
        perr_exit("close error");

    return n;
}

/*參三: 應該讀取的位元組數*/                          
//socket 4096  readn(cfd, buf, 4096)   nleft = 4096-1500
ssize_t Readn(int fd, void *vptr, size_t n)
{
    size_t  nleft;              //usigned int 剩餘未讀取的位元組數
    ssize_t nread;              //int 實際讀到的位元組數
    char   *ptr;

    ptr = vptr;
    nleft = n;                  //n 未讀取位元組數

    while (nleft > 0) 
    {
        if ((nread = read(fd, ptr, nleft)) < 0) 
        {
            if (errno == EINTR) //在讀取到資料以前呼叫被訊號所中斷
            {
                nread = 0;
            }
            else
            {
                return -1;
            }
        } 
        else if (nread == 0)
        {
            break;
        }

        nleft -= nread;   //nleft = nleft - nread 
        ptr += nread;
    }
    return n - nleft;
}

ssize_t Writen(int fd, const void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nwritten;
    const char *ptr;

    ptr = vptr;
    nleft = n;
    while (nleft > 0) 
    {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) 
        {
            if (nwritten < 0 && errno == EINTR)
                nwritten = 0;
            else
                return -1;
        }
        nleft -= nwritten;
        ptr += nwritten;
    }
    return n;
}

static ssize_t my_read(int fd, char *ptr)
{
    static int read_cnt;
    static char *read_ptr;
    static char read_buf[100];

    if (read_cnt <= 0) {
again:
        if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0)    //"hello\n"
        {
            if (errno == EINTR)
                goto again;
            return -1;
        } 
        else if (read_cnt == 0)
            return 0;

        read_ptr = read_buf;
    }
    read_cnt--;
    *ptr = *read_ptr++;

    return 1;
}

/*readline --- fgets*/    
//傳出引數 vptr
ssize_t Readline(int fd, void *vptr, size_t maxlen)
{
    ssize_t n, rc;
    char    c, *ptr;
    ptr = vptr;

    for (n = 1; n < maxlen; n++) 
    {
        if ((rc = my_read(fd, &c)) == 1)    //ptr[] = hello\n
        {
            *ptr++ = c;
            if (c == '\n')
                break;
        } 
    else if (rc == 0) 
    {
            *ptr = 0;
            return n-1;
        } 
        else
            return -1;
    }
    *ptr = 0;
    return n;
}

 

#ifndef __WRAP_H_
#define __WRAP_H_

void perr_exit(const char *s);
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);
int Bind(int fd, const struct sockaddr *sa, socklen_t salen);
int Connect(int fd, const struct sockaddr *sa, socklen_t salen);
int Listen(int fd, int backlog);
int Socket(int family, int type, int protocol);
ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *ptr, size_t nbytes);
int Close(int fd);
ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n);
ssize_t my_read(int fd, char *ptr);
ssize_t Readline(int fd, void *vptr, size_t maxlen);

#endif
  • 服務端server.c
//server.c
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/select.h>
#include "wrap.h"

#define SERV_PROT 6666

int main(int argc, char *argv[])
{
    int i, j, n, nread;

    int maxfd = 0;
    int listenfd, connfd;
    char buf[BUFSIZ];

    struct sockaddr_in clie_addr, serv_addr;
    socklen_t clie_addr_len;
    listenfd = Socket(AF_INET, SOCK_STREAM, 0);

    int opt = 1;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(SERV_PROT);
    Bind(listen, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    Listen(listenfd, 128);

    fd_set rset, allset;
    maxfd = listenfd;
    FD_ZERO(&allset);
    FD_SET(listenfd, &allset);
    while (1)
    {
        rset = allset;
        nread = select(maxfd + 1, &rset, NULL, NULL, NULL);  //每次迴圈都從新設定select監控訊號集
        if(nread < 0)
            perr_exit("select error");

        if (FD_ISSET(listenfd, &rset))   //說明有新的客戶端連線請求
        {
            clie_addr_len = sizeof(clie_addr);
            connfd = Accept(listenfd, (struct sockaddr *)&clie_addr, &clie_addr_len);

            FD_SET(connfd, &allset); //向監控檔案描述符集合allset新增新的檔案描述符connfd

            if (maxfd < connfd)
                maxfd = connfd;

            if (0 == --nread)  //說明select只返回一個,並且listenfd,後續執行無須執行
                continue;
        }
        for (i = listenfd + 1; i <= maxfd; i++)
        {
            if (FD_ISSET(i, &rset))
            {
                if ((n = Read(i, buf, sizeof(buf))) == 0)  //當client關閉連結時, 服務端也關閉對應連結
                {
                    Close(i);
                    FD_CLR(i, &allset);  //解除select對此檔案描述符的監控
                }
                else if(n == -1)
                    perr_exit("read error");
                for (j = 0; j < n; ++j)
                    buf[j] = toupper(buf[j]);
                Write(i, buf, n);
            }
        }
    }
    Close(listenfd);
    return 0;
}
  • 客戶端:clent.c
#include <arpa/inet.h>
#include "wrap.h"

#define SERV_IP "192.168.245.139"
#define SERV_PORT 6666

int main(void)
{
    int sfd, len;
    struct sockaddr_in serv_addr;
    char buf[BUFSIZ];
    sfd = Socket(AF_INET, SOCK_STREAM, 0);
    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    inet_pton(AF_INET, SERV_IP, &serv_addr.sin_addr.s_addr);
    serv_addr.sin_port = htons(SERV_PORT);
    Connect(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

    while (1) 
    {
        fgets(buf, sizeof(buf), stdin);
        int r = Write(sfd, buf, strlen(buf));
        printf("Write r ======== %d\n", r);
        len = Read(sfd, buf, sizeof(buf));
        printf("Read len ========= %d\n", len);
        Write(STDOUT_FILENO, buf, len);
    }
    Close(sfd);
    return 0;
}
  • 公共標頭檔案集程式:wrap.h、wrap.c

輸出結果:

客戶端:

服務端: