網路程式設計 poll

2021-06-10 03:09:54 字數 1540 閱讀 7935

寫下來,怕忘記了

// server

#include #include #include #include #include #include #include #include // gettimeofday()

#include // struct in_addr

#include // inet_aton()

#include // getpid()

#include #include //#define ndebug

#include using namespace std;

#define max_connect 5

void nothing(int i)

int main()

; struct sockaddr_in cli_addr= ;

socklen_t cli_len = sizeof cli_addr;

short port;

int fd = socket(pf_inet,sock_stream,0);

if(fd < 0)

int flags = fcntl(fd,f_getfl,0);

flags |= o_nonblock;

//fcntl(fd,f_setfl,flags);

addr.sin_family = af_inet;

//n = inet_aton("127.0.0.1",&addr.sin_addr);

addr.sin_addr.s_addr = inaddr_any;

addr.sin_port = htons(6219);

n = bind(fd,(struct sockaddr*) &addr,sizeof(addr));

if(n < 0)

n = listen(fd,2);

if(n < 0)

vectorfdvec;

fdvec.push_back(fd);

while(1)

cerr << "poll...index:" << index;

n = poll(fds,index,5000 /*milliseconds -1 */ );

cerr << " done. " << n << endl;

if(n < 0)

else if (0 == n)

else

}else

;int n = read(fds[i].fd,buf,sizeof(buf));

--readyfds;

if(n < 0)

return 6;

}if(0 == n)

continue;

}int m = write(fds[i].fd,buf,n);

if(m < 0)}}

if(readyfds)

cerr << "some fd don't proccess." << endl;

} }return 0;

}

// client

參見:

網路程式設計 21 poll

三 測試 理解 poll 函式 int poll struct ploofd fds,unsigned long nfds,int timeout 引數 返回值 struct pollfd struct pollfdpollserver.c include common.h intmain int ...

C 網路程式設計之poll

poll機制與select機制類似,通過管理檔案描述符來進行輪詢,效率更高,並且處理的連線個數不受核心的限制。include int poll struct pollfd fdarray,unsigned int nfds,int timeout 引數 1 fdarray 可讀套接字,是乙個指向陣列...

Linux網路程式設計 poll函式

作用 監視並等待多個檔案描述符的屬性變化。函式原型 int poll struct pollfd fds,nfds t nfds,int timeout 函式引數意義 fds 指向乙個結構體陣列的第0個元素的指標,每個陣列元素都是乙個struct pollfd結構,用於指定測試某個給定的fd的條件。...