Kqueue: تفاوت میان نسخه‌ها

محتوای حذف‌شده محتوای افزوده‌شده
بدون خلاصۀ ویرایش
MahdiBot (بحث | مشارکت‌ها)
خط ۱۲:
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
سطر ۲۱ ⟵ ۲۰:
#include <sys/time.h>
#include <errno.h>
 
void die(const char*);
 
int main(int argc, char *argv[])
{
int sockfd, nev, kq;
 
struct addrinfo hints, *res, *res0;
 
struct kevent change[2], event[2];
 
ssize_t nbytes;
 
char buf[BUFSIZ];
 
const char *cause = NULL;
 
if (3 != argc)
{
سطر ۴۳ ⟵ ۴۲:
exit(EXIT_FAILURE);
}
 
(void)memset(&hints, '\0', sizeof(struct addrinfo));
 
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
 
if (-1 == getaddrinfo(argv[1], argv[2], &hints, &res0))
die("getaddrinfo");
 
sockfd = -1;
 
for (res = res0; res; res = res->ai_next)
{
سطر ۶۲ ⟵ ۶۱:
continue;
}
 
if (-1 == connect(sockfd, res->ai_addr, res->ai_addrlen))
{
سطر ۶۹ ⟵ ۶۸:
continue;
}
 
break;
 
}
 
if (-1 == sockfd)
die(cause);
 
if (-1 == fcntl(sockfd, F_SETFL, O_NONBLOCK))
die("fcntl1()");
 
if (-1 == fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK))
die("fcntl()");
 
if (-1 == (kq = kqueue()))
die("kqueue()");
 
EV_SET(&change[0], STDIN_FILENO, EVFILT_READ, EV_ADD | EV_ENABLE,
0, 0, 0);
EV_SET(&change[1], sockfd, EVFILT_READ, EV_ADD | EV_ENABLE,
0, 0, 0);
 
for (;;)
{
if (-1 == (nev = kevent(kq, change, 2, event, 2, NULL)))
die("kevent()");
 
for (int i = 0; i < nev; i++)
{
if (event[i].ident == STDIN_FILENO)
{
fgets(buf, sizeof(buf), stdin);
 
nbytes = send(sockfd, buf, strlen(buf), 0);
 
if (-1 == nbytes)
if (EWOULDBLOCK != errno)
die("send()");
 
}
else
{
nbytes = recv(sockfd, buf, sizeof(buf), 0);
 
if (-1 == nbytes)
if (EWOULDBLOCK != errno)
die("recv()");
 
buf[nbytes] = '\0';
 
write(STDOUT_FILENO, buf, strlen(buf));
}
 
}
 
}
 
return 0;
}
 
void die(const char *str)
{
سطر ۱۴۴ ⟵ ۱۴۳:
|بازیابی =
}}
 
[[رده:اواس ده]]
[[رده:اوپن‌بی‌اس‌دی]]
[[رده:رویدادها (رایانش)]]
[[رده:فری‌بی‌اس‌دی]]
[[رده:نت‌بی‌اس‌دی]]
[[رده:نرم‌افزارهای بی‌اس‌دی]]
برگرفته از «https://fa.wikipedia.org/wiki/Kqueue»