问题:
在使用线程池处理客户端请求时发现不能获取到客户端的ip!
原因:
static unsigned long ips[MAX_QUEUE];
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; }
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; }
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; }
</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); 。。。。。。 }