gsoap:实现线程池处理时获取到客户端的ip

问题:

在使用线程池处理客户端请求时发现不能获取到客户端的ip!

原因:

    由于在server_loop注循环中只把连接字sock加到queue队列中,并没有客户端IP,所以每一次queue回调函数只能取得sock连接字,没有客户端的ip

解决方法:
    在将sock加入queue队列的同时把ip也加入到ips队列中,ips队列的长度和queue相同,而且存的数据下标要相同,取sock处理时也把IP取出来,即可得到了客户端IP

步骤:
    定义全局保存IP地址的变量
  1. static unsigned long ips[MAX_QUEUE];  
    static unsigned long ips[MAX_QUEUE];

    修改 enqueue函数
  1. int enqueue(SOAP_SOCKET sock,unsigned long ip)//添加ip的参数   
  2. {  
  3.         int status = SOAP_OK;  
  4.         int next;  
  5.         int ret;   
  6.         if ((ret = MUTEX_LOCK(queue_cs)))  
  7.                 fprintf(stderr, "MUTEX_LOCK error %d\n", ret);  
  8.         next = (tail + 1) % MAX_QUEUE;  //1000   
  9.         if (head == next)  
  10.         {      
  11.                 /* don't block on full queue, 
  12.                  * 队列已满,return SOAP_EOM */  
  13.                 status = SOAP_EOM;  
  14.         }      
  15.         else   
  16.         {      
  17.                 queue[tail] = sock;  
  18.                 ips[tail] = ip;  //保存ip   
  19.                 tail = next;  
  20.                 if ((ret = COND_SIGNAL(queue_cv)))  
  21.                         fprintf(stderr, "COND_SIGNAL error %d\n", ret);  
  22.         }      
  23.         if ((ret = MUTEX_UNLOCK(queue_cs)))  
  24.                 fprintf(stderr, "MUTEX_UNLOCK error %d\n", ret);  
  25.         return status;  
  26. }  
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()函数
  1. unsigned long  dequeue_ip()  
  2. {  
  3.         unsigned long ip;  
  4.         int num=0;  
  5.         if(head == 0)  
  6.             num = MAX_QUEUE - 1;  
  7.         else   
  8.             num = head -1;  
  9.         ip = ips[num];  
  10.         return ip;  
  11. }  
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回调函数函数
  1. void *process_queue(void *soap)  
  2. {  
  3.         struct soap *tsoap = (struct soap*)soap;  
  4.         for (;;)  
  5.         {  
  6.                 tsoap->socket = dequeue();  
  7.                 tsoap->ip = dequeue_ip();//获取相应的ip地址   
  8.                 if (!soap_valid_socket(tsoap->socket))  
  9.                 {  
  10. #ifdef DEBUG   
  11.                         fprintf(stderr, "Thread %d terminating\n", (int)(long)tsoap->user);  
  12. #endif   
  13.                         break;  
  14.                 }  
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函数中测试
  1.   
</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);
。。。。。。
}


输出:
  1. Request accepts connection from IP 192.168.1.136  
  2. Request accepts connection from IP 192.168.1.136  
  3. Thread 3 finished serving request with failure 404  
  4. Error 404 fault: SOAP-ENV:Client [no subcode]  
  5. "HTTP Error: 404 Not Found"  
  6. Detail: [no detail]  
  7. Request accepts connection from IP 192.168.1.87  
  8. Request accepts connection from IP 192.168.1.87  
  9. Request accepts connection from IP 192.168.1.87  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值