亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2840 | 回復(fù): 0
打印 上一主題 下一主題

kqueue example [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-08-28 14:04 |只看該作者 |倒序瀏覽

               
               
                #include sys/types.h>
#include netinet/in.h>
#include sys/event.h>
#include sys/time.h>
#include sys/socket.h>
#include sys/types.h>
#include arpa/inet.h>
#include unistd.h>
#include stdlib.h>
#include string.h>
#include stdio.h>
#define err(msg) perror(msg)
#define SA struct sockaddr
struct event
{
        int fd;
        void (*handle)(struct event *);
};
static int tcp_listen(void)
{
        struct sockaddr_in addr;
        int fd;
        if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        {
                err("socket");
                return -1;
        }
        memset(&addr, '\0', sizeof(struct sockaddr_in));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(8888);
        if (inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr) = 0)
        {
                err("inet_pton");
                goto err;
        }
        if (bind(fd, (SA *)&addr, sizeof(struct sockaddr_in)) == -1)
        {
                err("bind");
                goto err;
        }
        if (listen(fd, 10) == -1)
        {
                err("listen");
                goto err;
        }
        return fd;
err:
        close(fd);
        return -1;
}
static void listen_readable(struct event *e)
{
        int connfd;
        connfd = accept(e->fd, NULL, NULL);
        printf("connfd = %d\n", connfd);
        close(connfd);
}
int main(void)
{
        int fd;
        int kq;
        int nfds;
        struct kevent ev, rev[2];
        if ((fd = tcp_listen()) == -1)
        {
                err("tcp_listen");
                return -1;
        }
        if ((kq = kqueue()) == -1)
        {
                err("kqueue");
                goto err;
        }
        struct event e;
        e.fd = fd;
        e.handle = listen_readable;
        EV_SET(&ev, fd, EVFILT_READ, EV_ADD, 0, 0, &e);
        while (1)
        {
                nfds = kevent(kq, &ev, 1, rev, 2, NULL);
                if (nfds = 0)
                {
                        err("kevent");
                        goto kevent_err;
                }
                for (int i = 0; i  nfds; i++)
                {
                        struct event *re = rev.udata;
                        if (re)
                                re->handle(re);
                }
        }
        return 0;
kevent_err:
        close(kq);
err:
        close(fd);
        return -1;
}


本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/63604/showart_2040386.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP