当前位置: 技术问答>linux和unix
请教大家一个问题,关于UNIX网络编程第16章非阻塞I/O中的一段代码
来源: 互联网 发布时间:2016-09-07
本文导语: #include "web.h" int main(int argc, char **argv) { int i, fd, n, maxnconn, flags, error; char buf[MAXLINE]; fd_set rs, ws; if (argc ai_protocol); fptr->f_fd = fd; printf("start_connect for %s, fd %dn", fptr->f_name, fd); /* 4Set socket nonblocking */ flags = F...
#include "web.h"
int
main(int argc, char **argv)
{
int i, fd, n, maxnconn, flags, error;
char buf[MAXLINE];
fd_set rs, ws;
if (argc ai_protocol);
fptr->f_fd = fd;
printf("start_connect for %s, fd %dn", fptr->f_name, fd);
/* 4Set socket nonblocking */
flags = Fcntl(fd, F_GETFL, 0);
Fcntl(fd, F_SETFL, flags | O_NONBLOCK);
/* 4Initiate nonblocking connect to the server. */
if ( (n = connect(fd, ai->ai_addr, ai->ai_addrlen)) f_flags = F_CONNECTING;
FD_SET(fd, &rset); /* select for reading and writing */
FD_SET(fd, &wset);
if (fd > maxfd)
maxfd = fd;
} else if (n >= 0) /* connect is already done */
write_get_cmd(fptr); /* write() the GET command */
}
#include "web.h"
void
write_get_cmd(struct file *fptr)
{
int n;
char line[MAXLINE];
n = snprintf(line, sizeof(line), GET_CMD, fptr->f_name);
Writen(fptr->f_fd, line, n);
printf("wrote %d bytes for %sn", n, fptr->f_name);
fptr->f_flags = F_READING; /* clears F_CONNECTING */
FD_SET(fptr->f_fd, &rset); /* will read server's reply */
if (fptr->f_fd > maxfd)
maxfd = fptr->f_fd;
}
|
是的,使flags & F_CONNECTING为假。
两种状态,前者表示socket正处于链接过程中,后者表示socket处于读写过程中。
两种状态,前者表示socket正处于链接过程中,后者表示socket处于读写过程中。