windows select pipe
来源:互联网 发布:赚钱宝监控软件 编辑:程序博客网 时间:2024/05/17 06:47
转载自:http://blog.csdn.net/chief1985/article/details/5064998
在进行socket编程时,经常会采用select或epoll来接受并发请求。在有些系统里面(例如嵌入式设备),是不支持epoll的,这时只能用select。
Select定义如下:
int select(int n, fd_set *rd_fds, fd_set *wr_fds, fd_set *ex_fds, struct timeval *timeout);
在进行select的时候,如果等待的句柄没有发生变化和没有超时,select是会一直等待在那里的。但在某些情况下,你需要唤醒select,你会怎么办呢?在linux上,你可以将管道的句柄传给select,然后在要唤醒select的时候向管道里面写东西就可以了(palm pre手机里面就用了这种技巧,我是通过反汇编知道的)。但在windows平台下,你就不能将管道传给select了,这时你就需要用socket来模拟管道了。在windows pipe的模拟(http://blog.chinaunix.net/u2/69656/showart_705924.html)里面,作者提供了一种在windows上模拟管道的方法,代码如下:
- int pipe(int fildes[2])
- {
- int tcp1, tcp2;
- sockaddr_in name;
- memset(&name, 0, sizeof(name));
- name.sin_family = AF_INET;
- name.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- int namelen = sizeof(name);
- tcp1 = tcp2 = -1;
- int tcp = socket(AF_INET, SOCK_STREAM, 0);
- if (tcp == -1){
- goto clean;
- }
- if (bind(tcp, (sockaddr*)&name, namelen) == -1){
- goto clean;
- }
- if (listen(tcp, 5) == -1){
- goto clean;
- }
- if (getsockname(tcp, (sockaddr*)&name, &namelen) == -1){
- goto clean;
- }
- tcp1 = socket(AF_INET, SOCK_STREAM, 0);
- if (tcp1 == -1){
- goto clean;
- }
- if (-1 == connect(tcp1, (sockaddr*)&name, namelen)){
- goto clean;
- }
- tcp2 = accept(tcp, (sockaddr*)&name, &namelen);
- if (tcp2 == -1){
- goto clean;
- }
- if (closesocket(tcp) == -1){
- goto clean;
- }
- fildes[0] = tcp1;
- fildes[1] = tcp2;
- return 0;
- clean:
- if (tcp != -1){
- closesocket(tcp);
- }
- if (tcp2 != -1){
- closesocket(tcp2);
- }
- if (tcp1 != -1){
- closesocket(tcp1);
- }
- return -1;
- }
在这段代码里面,有一个技巧是:将port设为0,然后进行bind,再接着通过getsockname来获取port。这可以满足获取随机端口的情况。
0 0
- windows select pipe
- select:windows pipe的模拟
- select pipe
- windows pipe
- Select模型(PIPE)
- pipe unblock select in linux
- windows select
- pipe()
- pipe
- pipe
- pipe
- pipe
- PIPE
- pipe
- pipe
- pipe
- pipe
- pipe
- 当一个Activity加多个Fragment的时候,点击导航返回按钮无效或者点击back键就把Activity关闭了的问题
- (读书笔记)mybatis (三)-----environments详解
- Android之ViewPager.getChildCount()无限自增问题解析
- android procrank
- 与nginx性能相关的linux内核参数
- windows select pipe
- iOS发布--静态库bitCode问题
- 判断String是否为空
- 今日头条 --新闻阅读器
- UVA-10387 Billiard
- ME2N标准报表中加EKKO/EKPO中没显示的字段
- 嵌入式学习--work7 AFE4490 学习
- Failed to start LSB: Enable AMQP service provided by RabbitMQ broker.
- python Tkinter之Button