标签:
前一章节客户端同时处理两个输入:标准输入和TCP套接字,然而问题在于客户端阻塞于fgets调用期,服务器进程被杀死后,服务器tcp虽然可以正确发送一个fin,但进程正阻塞于标准输入,它无法看到eof,直到从套接字读为止.这样的进程需要一种预先告诉内核的能力,一旦内核发现进程指定的一个或者多个I/O就绪,它就通知进程,这就叫做I/O复用
#include <sys/select.h>
#include<sys/time.h>
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
struct timeval{
long tv_sec;
long tv_usec;
};
void FD_ZERO(fd_set*fdset);
void FD_SET(int fd,fd_set*fdset);
void FD_CLR(int fd,fd_set *fdset);
int FD_ISSET(int fd,fd_set *fdset);
fd_set rset;
FD_ZERO(&rset);
FD_SET(1,&rset);
FD_SET(4,&rset);
FD_SET(5,&rset);
#include "unp.h"
void
str_cli(FILE *fp, int sockfd)
{
int maxfdp1, stdineof;
fd_set rset;
char buf[MAXLINE];
int n;
stdineof = 0;
FD_ZERO(&rset);
for ( ; ; ) {
if (stdineof == 0)
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdp1 = max(fileno(fp), sockfd) + 1;
Select(maxfdp1, &rset, NULL, NULL, NULL);
- if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
if ( (n = Read(sockfd, buf, MAXLINE)) == 0) {
if (stdineof == 1)
return; /* normal termination */
else
err_quit("str_cli: server terminated prematurely");
}
- Write(fileno(stdout), buf, n);
}
- if (FD_ISSET(fileno(fp), &rset)) {
if ( (n = Read(fileno(fp), buf, MAXLINE)) == 0) {
stdineof = 1;
Shutdown(sockfd, SHUT_WR);
FD_CLR(fileno(fp), &rset);
continue;
}
Writen(sockfd, buf, n);
}
}
}
int main(int argc,char *argv[])
{
if(argc<3)
err_quit("please input IP Address : Port");
int connfd=Socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
Inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
Connect(connfd,(SA*)&servaddr,sizeof(servaddr));
str_cli(stdin,connfd);
return 0;
}
#include "unp.h"
static int create_sock(char *ip)
{
int listenfd;
listenfd=Socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(ip));
Inet_pton(AF_INET,"0.0.0.0",&servaddr.sin_addr);
Bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
Listen(listenfd,20);
return listenfd;
}
static void deal_cli(int listenfd)
{
int connfd,i;
fd_set allset,rset;//allset保存的整个描述符集。但并不会直接select.每次select都会使得清0.很不方便。于是当要select的时候把它付给rset
FD_ZERO(&allset);
FD_ZERO(&rset);
char buf[1024];//从客户端读取的缓冲区
int ret=0;
int maxfd=listenfd;//select第一个参数
int allfd[MAXLINE];//连接数组
for(i=0;i<MAXLINE;++i){
allfd[i]=-1;
}
int nready; //select I/O已经就绪
int max=0;//表示已完成连接数组中最大的队列
FD_SET(listenfd,&allset);//将监听连接加入allset集合中
for(;;){
rset=allset;//更新
nready=Select(maxfd+1,&rset,NULL,NULL,NULL);
if(FD_ISSET(listenfd,&rset)){
connfd=Accept(listenfd,NULL,NULL); //从已连接套接字拿取一个出来
for(i=0;i<MAXLINE;++i){ //遍历整个已连接数组。找到alifd[i]等于-1(表示该位可用)
if(allfd[i]<0){
allfd[i]=connfd;
break;
}
}
FD_SET(connfd,&allset);
if(maxfd<connfd) //每次都需要比较maxfd跟新来的描述符值大小
maxfd=connfd;
if(max<i)//还要更新最后可连接数组最后的那个已连接索引
max=i;
if(--nready<=0) //如果该连接已经处理完成。那就
continue;
}
for(i=0;i<max+1;++i){ //遍历以此处理所有已完成连接
if(allfd[i]!=-1)//跳过
if(FD_ISSET(allfd[i],&rset)){
ret=read(allfd[i],buf,1024);
if(ret==0){
close(allfd[i]);
FD_CLR(allfd[i],&allset);
allfd[i]=-1;
}else if(ret<0)
{
perror("error");
}else
Writen(allfd[i],buf,ret);
}
if(--nready<=0) //如果处理最后一个连接。继续返回到select中去
continue;
}
}
}
- }
- int main(int argc,char *argv[])
{
if(argc<2){
err_quit("please input ip aiddress\n");
}
int listenfd=create_sock(argv[1]);//创建一个套接字
deal_cli(listenfd);//开始处理细节
return 0;
}
#include <poll.h>
- int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
/* include fig01 */
#include "unp.h"
//#include <limits.h> /* for OPEN_MAX */
int
main(int argc, char **argv)
{
int i, maxi, listenfd, connfd, sockfd;
int nready;
ssize_t n;
char buf[MAXLINE];
socklen_t clilen;
long OPEN_MAX = sysconf(_SC_OPEN_MAX);
struct pollfd client[OPEN_MAX];
struct sockaddr_in cliaddr, servaddr;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
client[0].fd = listenfd;
client[0].events = POLLRDNORM;
for (i = 1; i < OPEN_MAX; i++)
client[i].fd = -1; /* -1 indicates available entry */
maxi = 0; /* max index into client[] array */
/* end fig01 */
/* include fig02 */
for ( ; ; ) {
nready = Poll(client, maxi+1, INFTIM);
if (client[0].revents & POLLRDNORM) { /* new client connection */
clilen = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
#ifdef NOTDEF
printf("new client: %s\n", Sock_ntop((SA *) &cliaddr, clilen));
#endif
for (i = 1; i < OPEN_MAX; i++)
if (client[i].fd < 0) {
client[i].fd = connfd; /* save descriptor */
break;
}
if (i == OPEN_MAX)
err_quit("too many clients");
client[i].events = POLLRDNORM;
if (i > maxi)
maxi = i; /* max index in client[] array */
if (--nready <= 0)
continue; /* no more readable descriptors */
}
for (i = 1; i <= maxi; i++) { /* check all clients for data */
if ( (sockfd = client[i].fd) < 0)
continue;
if (client[i].revents & (POLLRDNORM | POLLERR)) {
if ( (n = read(sockfd, buf, MAXLINE)) < 0) {
if (errno == ECONNRESET) {
/*4connection reset by client */
#ifdef NOTDEF
printf("client[%d] aborted connection\n", i);
#endif
Close(sockfd);
client[i].fd = -1;
} else
err_sys("read error");
} else if (n == 0) {
/*4connection closed by client */
#ifdef NOTDEF
printf("client[%d] closed connection\n", i);
#endif
Close(sockfd);
client[i].fd = -1;
} else
Writen(sockfd, buf, n);
if (--nready <= 0)
break; /* no more readable descriptors */
}
}
}
}
/* end fig02 */
标签:
原文地址:http://www.cnblogs.com/zengyiwen/p/5755215.html