gsoap:实现线程池处理时获取到客户端的ip
来源:互联网 发布:金针软件下载 编辑:程序博客网 时间:2024/06/07 13:25
问题:
在使用线程池处理客户端请求时发现不能获取到客户端的ip!
原因:
由于在server_loop注循环中只把连接字sock加到queue队列中,并没有客户端IP,所以每一次queue回调函数只能取得sock连接字,没有客户端的ip
解决方法:
在将sock加入queue队列的同时把ip也加入到ips队列中,ips队列的长度和queue相同,而且存的数据下标要相同,取sock处理时也把IP取出来,即可得到了客户端IP
步骤:
定义全局保存IP地址的变量
- static unsigned long ips[MAX_QUEUE];
static unsigned long ips[MAX_QUEUE];
修改 enqueue函数
- int enqueue(SOAP_SOCKET sock,unsigned long ip)//添加ip的参数
- {
- int status = SOAP_OK;
- int next;
- int ret;
- if ((ret = MUTEX_LOCK(queue_cs)))
- fprintf(stderr, "MUTEX_LOCK error %d\n", ret);
- next = (tail + 1) % MAX_QUEUE; //1000
- if (head == next)
- {
- /* don't block on full queue,
- * 队列已满,return SOAP_EOM */
- status = SOAP_EOM;
- }
- else
- {
- queue[tail] = sock;
- ips[tail] = ip; //保存ip
- tail = next;
- if ((ret = COND_SIGNAL(queue_cv)))
- fprintf(stderr, "COND_SIGNAL error %d\n", ret);
- }
- if ((ret = MUTEX_UNLOCK(queue_cs)))
- fprintf(stderr, "MUTEX_UNLOCK error %d\n", ret);
- return status;
- }
int enqueue(SOAP_SOCKET sock,unsigned long ip)//添加ip的参数{ int status = SOAP_OK; int next; int ret; if ((ret = MUTEX_LOCK(queue_cs))) fprintf(stderr, "MUTEX_LOCK error %d\n", ret); next = (tail + 1) % MAX_QUEUE; //1000 if (head == next) { /* don't block on full queue, * 队列已满,return SOAP_EOM */ status = SOAP_EOM; } else { queue[tail] = sock; ips[tail] = ip; //保存ip tail = next; if ((ret = COND_SIGNAL(queue_cv))) fprintf(stderr, "COND_SIGNAL error %d\n", ret); } if ((ret = MUTEX_UNLOCK(queue_cs))) fprintf(stderr, "MUTEX_UNLOCK error %d\n", ret); return status;}
添加dequeue_ip()函数
- unsigned long dequeue_ip()
- {
- unsigned long ip;
- int num=0;
- if(head == 0)
- num = MAX_QUEUE - 1;
- else
- num = head -1;
- ip = ips[num];
- return ip;
- }
unsigned long dequeue_ip(){ unsigned long ip; int num=0; if(head == 0) num = MAX_QUEUE - 1; else num = head -1; ip = ips[num]; return ip;}
修改queue回调函数函数
- void *process_queue(void *soap)
- {
- struct soap *tsoap = (struct soap*)soap;
- for (;;)
- {
- tsoap->socket = dequeue();
- tsoap->ip = dequeue_ip();//获取相应的ip地址
- if (!soap_valid_socket(tsoap->socket))
- {
- #ifdef DEBUG
- fprintf(stderr, "Thread %d terminating\n", (int)(long)tsoap->user);
- #endif
- break;
- }
void *process_queue(void *soap){ struct soap *tsoap = (struct soap*)soap; for (;;) { tsoap->socket = dequeue(); tsoap->ip = dequeue_ip();//获取相应的ip地址 if (!soap_valid_socket(tsoap->socket)) {#ifdef DEBUG fprintf(stderr, "Thread %d terminating\n", (int)(long)tsoap->user);#endif break; }
解决!
测试:
在http_get_handler函数中测试
</pre><div class="dp-highlighter bg_cpp"><div class="bar"><div class="tools"><strong>[cpp]</strong> <a target=_blank class="ViewSource" title="view plain" href="http://blog.csdn.net/jk110333/article/details/9445761#">view plain</a><a target=_blank class="CopyToClipboard" title="copy" href="http://blog.csdn.net/jk110333/article/details/9445761#">copy</a><a target=_blank class="PrintSource" title="print" href="http://blog.csdn.net/jk110333/article/details/9445761#">print</a><a target=_blank class="About" title="?" href="http://blog.csdn.net/jk110333/article/details/9445761#">?</a></div></div><ol class="dp-cpp"><li class="alt"><span class="datatypes">int</span> http_get_handler(<span class="keyword">struct</span> soap *soap) </li><li>{ </li><li class="alt"> 。。。。。 </li><li> fprintf(stderr, <span class="string">"Request accepts connection from IP %d.%d.%d.%d\n"</span>, </li><li class="alt"> (<span class="datatypes">int</span>)(soap->ip>>24)&0xFF, (<span class="datatypes">int</span>)(soap->ip>>16)&0xFF,(<span class="datatypes">int</span>) (soap->ip>>8)&0xFF, (<span class="datatypes">int</span>)soap->ip&0xFF); </li><li>。。。。。。 </li><li class="alt">} </li></ol></div><pre style="DISPLAY: none" class="cpp" name="code">int http_get_handler(struct soap *soap){ 。。。。。 fprintf(stderr, "Request accepts connection from IP %d.%d.%d.%d\n", (int)(soap->ip>>24)&0xFF, (int)(soap->ip>>16)&0xFF,(int) (soap->ip>>8)&0xFF, (int)soap->ip&0xFF);。。。。。。}
输出:
- Request accepts connection from IP 192.168.1.136
- Request accepts connection from IP 192.168.1.136
- Thread 3 finished serving request with failure 404
- Error 404 fault: SOAP-ENV:Client [no subcode]
- "HTTP Error: 404 Not Found"
- Detail: [no detail]
- Request accepts connection from IP 192.168.1.87
- Request accepts connection from IP 192.168.1.87
- Request accepts connection from IP 192.168.1.87
0 0
- gsoap:实现线程池处理时获取到客户端的ip
- gsoap:实现线程池处理时获取到客户端的ip
- gsoap:实现线程池处理时获取到客户端的ip
- 获取客户端的IP
- 获取客户端的IP
- 获取客户端的IP
- nginx获取客户端IP实现
- Java获取客户端ip以及根据ip查询到相应的ip地址
- 利用JS获取IE客户端IP及MAC的实现
- 利用JS获取IE客户端IP及MAC的实现
- 利用JS获取IE客户端IP及MAC的实现
- 利用JS获取IE客户端IP及MAC的实现
- 用javascript代码实现对客户端ip地址的获取
- 利用JS获取IE客户端IP及MAC的实现
- javascript获取IE客户端IP及MAC的实现
- 利用JS获取IE客户端IP及MAC的实现
- 利用JS获取IE客户端IP及MAC的实现
- 获取客户端的IP,可以取到代理后的IP
- 运用快排函数
- N - Little Girl and Maximum Sum——贪心
- Hive中不走MapReduce的查询
- Broadcast Intent实现的四个步骤
- 我所理解的设计模式(C++实现)——组合模式(Composite Pattern)
- gsoap:实现线程池处理时获取到客户端的ip
- 我所理解的设计模式(C++实现)——适配器模式(Adapter Pattern)
- CF GYM 100548 The Problem to Make You Happy(2014ACM西安现场赛Problem H)
- iOS开发-手势返回失效的解决方案
- 我所理解的设计模式(C++实现)——原型模式(Prototype Pattern)
- 设计模式名称记忆法则
- 我所理解的设计模式(C++实现)——建造者模式(Builder Pattern)
- MultiByteToWideChar 与 WideCharToMultiByte 准确理解
- 我所理解的设计模式(C++实现)——抽象工厂模式(Abstract Factory Pattern)