poll和select一样,管理多个描写叙述符也是进行轮询,依据描写叙述符的状态进行处理,可是poll没有最大文件描写叙述符数量的限制,select is 1024/2048

#include "stdio.h"
#include "stdlib.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netinet/tcp.h"
#include "arpa/inet.h"
#include "string.h"
#include <errno.h>
#include "sys/time.h"
#include "sys/times.h"
#include <unistd.h>
#include "sys/types.h"
#include "poll.h"
#include "sys/stropts.h"
#include <signal.h>
#include <time.h> #define OPEN_MAX 1024
#define MAXLINE 1024 int serverfd,clientfd;
int bindlisten();
int clientconn();
void closemap(int sockfd,int i);
void writeproxy(int i,int n);
int a[65535]={0},b[65535]={0};
char line[MAXLINE]={0};
struct pollfd client[OPEN_MAX];
int maxi;
int
main (int argc ,char **argv) {
int i,listenfd,connfd,sockfd;
int nready;
ssize_t n;
socklen_t clilen;
struct sockaddr_in cliaddr;
int timeout = 1000;
int on; signal (SIGPIPE,SIG_IGN); listenfd=bindlisten();
if (listenfd==-1) {
printf("line46:bind & listen fail\n");
return -1 ;
} client[0].fd=listenfd;
client[0].events=POLLIN; for (i=1;i<OPEN_MAX;i++)
client[i].fd=-1;
maxi=0;//max index into client[] array for ( ; ; ) {
nready=poll(client,maxi+1,timeout);
if (client[0].revents & POLLIN) { //new client connection
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); if (connfd>0) {
printf("Accept a new connection:connfd=%d\n",connfd);
for(i=1;i<OPEN_MAX;i++)
if(client[i].fd<0) {
client[i].fd=connfd;//save descriptor
printf("line68:i=%d,client[i].fd=%d\n",i,client[i].fd);
break;
}
if (i==OPEN_MAX)
perror("too many clients");
client[i].events=POLLIN;
if(i>maxi)
maxi=i; //max index in client[] array clientfd=clientconn();
if (clientfd==-1) {
printf("line98:clientfd fail\n");
return -1;
}
printf("create a new connect to default proxy: %d\n", clientfd); for(i=1;i<OPEN_MAX;i++)
if(client[i].fd<0) {
client[i].fd=clientfd;//save descriptor
printf("line55:i=%d,client[%d].fd=%d\n",i,i,client[i].fd);
break;
}
if (i==OPEN_MAX)
perror("too many clients");
printf("line93:i=%d,nready=%d\n",i,nready);
client[i].events=POLLIN; if(i>maxi)
maxi=i; //max index in client[] array
//map user and default proxy socket
//a keep default proxy,b keep user
a[connfd]=clientfd;
b[clientfd]=connfd; } else
perror("accept");
if (--nready<=0)
continue; //no more readable descriptors
} for (i=1;i<=maxi;i++) {
sockfd=client[i].fd;
if(sockfd<0)
continue;
if (client[i].revents &(POLLIN | POLLERR)) {
printf("line116:i=%d;fd=%d\n",i,client[i].fd);
memset(line,0x00,MAXLINE);
printf("line117:sockfd=%d;nready=%d\n",sockfd,nready);
if((n=read(sockfd,line,MAXLINE)) <=0) {
if (errno==EINTR) {
//don;t remove the socket
continue;
} else {
closemap(sockfd,i);
}
} else
writeproxy(i,n); if (--nready<0)
break; //no more readable descriptors
}
}
}
} int bindlisten() {
struct sockaddr_in tcpaddr;
struct in_addr intfIpAddr;
int tcpaddr_len;
int sockfd;
int client;
int port=8888;
int bReuseaddr=1;
int retVal;
int ret;
int buf,optlen;
int on,errno; memset( &tcpaddr, 0, sizeof(tcpaddr) ); if ( (sockfd= socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
printf ("socket create fail\n");
return -1;
}
// intitalize to hold given restrictions
tcpaddr.sin_family = AF_INET;
tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY);
tcpaddr.sin_port = htons(port); tcpaddr_len = sizeof(tcpaddr); on=1;
errno=0;
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)(&on),sizeof(on))<0)
{
printf("so_resueadd failed,error %d:%s\n",errno,strerror(errno));
return -2;
} // make bind call
if ((retVal = bind(sockfd, (struct sockaddr *)(&tcpaddr), sizeof(tcpaddr)))< 0 )
{
printf("bind() call failed. Error: %d, %s,port: %d\n ", errno, ( strerror(errno) ), port);
} //listen have 5 queue
if (listen(sockfd, 5) < 0 )
{
printf("Error: Listen socket returned %d %s\n", errno, strerror(errno) );
return -1;
}
return sockfd;
} int clientconn() {
struct sockaddr_in tcpaddr;
char ipHost[10]={0};
int ipPort=8000;
int ret;
int sockfd;
int bReuseaddr=1; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("socket create fail\n");
return -1;
}
printf("line202:sockfd=%d\n",sockfd);
/* Connect to the socket */
memset(&tcpaddr, 0, sizeof(tcpaddr));
//135.245.1.1 is default default proxy proxy ip
strcpy(ipHost, "135.245.1.1");
/* local host, processes must be running on the same machine ... */
/* intitalize to given address */
tcpaddr.sin_family = AF_INET;
tcpaddr.sin_addr.s_addr = inet_addr((const char *)ipHost);
tcpaddr.sin_port = htons(ipPort);
printf("line212:sockfd=%d\n",sockfd);
//basic connect is block, after timeout it return fail
if ((ret = connect(sockfd,(struct sockaddr *)(&tcpaddr),sizeof(tcpaddr))) < 0 ) {
printf("connect fail,retVal=%d,%s\n",errno,strerror(errno));
return 1;
}
return sockfd; } void writeproxy(int i,int n) {
int fdmap;
int ret;
int fd; fd=client[i].fd; if (a[fd]!=0) {
fdmap=a[fd];
// if is in a, it come from default proxy,so write to user
//forward message to ;ucent
printf("Write 1:from %d to %d\n",fd, fdmap);
ret=write(fdmap,line,n);
if (ret <=0) {
if (errno!=EINTR)
closemap(fdmap,i);
} else
printf("write to default proxy\n"); } else if (b[fd]!=0) {
fdmap=b[fd];
//is in b, it come from user
//forward message to user
printf("Write 2 from %d to %d\n",fd, fdmap);
ret=write(fdmap,line,n); if (ret <=0) {
if (errno!=EINTR)
closemap(fdmap,i);
} else
printf("write to user\n");
}
} void closemap (int fd,int i) {
int temp;
int j;
//The socket is wrong or closed.
close(fd);
client[i].fd=-1;
printf("line260 :close %d\n",fd);
//remove the socketfd
//don;t judge from user or default proxy ,set all kind to 0
//one sockfd close ,peer sockfd close
if (a[fd]!=0) {
temp=a[fd];
close (a[fd]);
printf("client close %d\n",a[fd]);
a[fd]=0;
b[temp]=0;
} else if (b[fd]!=0) {
temp=b[fd];
close(b[fd]);
printf("server close %d\n",b[fd]);
b[fd]=0;
a[temp]=0;
} for (j=1;j<=maxi;j++)
if (client[j].fd==temp)
{
client[j].fd=-1;
break;
}
}

代理server poll version的更多相关文章

  1. bug--java访问hdfs (Server IPC version 9 cannot communicate with client version 4 错误)

    1. 今天想做一个hdfs的java工具类,但是在连接hdfs的时候,报如下错误: Exception in thread "main" org.apache.hadoop.ipc ...

  2. Java通过代理server上网

    完整代码 package com.proj.net; //导入编码的jar文件 import it.sauronsoftware.base64.Base64; import java.io.Buffe ...

  3. windows平台HTTP代理server搭建(CCproxy)

    HTTP代理(CCproxy) 一.拓扑图 二.CCproxy的安装和配置 1.安装CCproxy (1)下载CCproxy无线破解版(没破解的都仅仅支持最多三个用户同一时候连接). (2)按说明安装 ...

  4. 计算机网络--http代理server的设计与实现

    一.Socket编程的client和服务端的主要步骤: Java Socket编程:对于http传输协议 client: 1.创建新的socket,绑定serverhost和port号 2.Socke ...

  5. 国外代理server

    这里有几个国外的代理server 另外在网上能够找到很多这种 不能用的时候就在网上搜搜 稳定代理server 有非常多的 IP port 显示地址 24.245.58.130:32167 美国 新泽西 ...

  6. [C++] 获取IE代理server的账号password

    非常多程序须要使用'浏览器设置'的代理server,IE设置的代理server有可能是须要账号password的.如何编程获取浏览器设置的代理server的账号password呢? InternetQ ...

  7. docker报Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.19)

    docker version Client: Version: 17.05.0-ce API version: 1.24 (downgraded from 1.29) Go version: go1. ...

  8. Android SDK代理server解决国内不能更新下载问题

    读者须知:本篇文章中最靠谱的是第三种方式,近期有读者反映第三种方式也不行了,以下提供一点其它途径的开源镜像网站: 国内高校的开源镜像站 中国科学技术大学(debian.ustc.edu.cn) 上海交 ...

  9. 记一次安装kolla遇到DockerException: Error while fetching server API version: Timeout value connect was Timeout的问题

    1)环境信息: docker版本:17.09,当docker的版本是12.06时,也会报这个错误 [root@localhost ~]# docker --version Docker version ...

随机推荐

  1. C# 上位机的USB设备拔插检测

    我们做USB通信时,通信成功后,往往要检测USB设备的拔插状态,这里就USB拔插进行一下说明. 参考:https://www.imooc.com/article/17438 先说明一下,我这里只是用C ...

  2. hbase的几种访问方式

    Hbase的访问方式 1.Native Java API:最常规和高效的访问方式: 2.HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用: 3.Thrift Gat ...

  3. php获取当前url地址的方法小结

    js 获取: top.location.href //顶级窗口的地址 this.location.href //当前窗口的地址 php获取当前url地址: #测试网址: http://localhos ...

  4. Android之——自己定义TextView

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47082241 在这一篇博文中,将向大家介绍怎样以最简单的方式,来自己定义Andro ...

  5. linux下面增加磁盘空间

    1.先看看情况 [root@localhost tmp]# fdisk -l Disk /dev/sda: 3221 MB, 3221225472 bytes 255 heads, 63 sector ...

  6. javax.validation注解使用

    @Pattern(regexp = "^[0-9]*$",message = "完成比例(进度)必须是数字")

  7. JQuery中的find、filter和each方法学习

    find() 概述 搜索所有与指定表达式匹配的元素.这个函数是找出正在处理的元素的后代元素的好方法. 所有搜索都依靠jQuery表达式来完成.这个表达式可以使用CSS1-3的选择器语法来写. 参数 e ...

  8. avalon过滤

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  9. SQL 中多个 and or 的组合运算

    sql关系型运算符优先级高到低为:not >and> or AND.OR运算符的组合使用 在WHERE子句中,通过AND.OR运算符可以同时连接多个条件,当然AND.OR运算符也可以同时使 ...

  10. layero和index

    zIndex:layer.zIndex, success:function(index,layero){ //layero 为当前层的DOM对象 var zIndex = layer.index; $ ...