Problem:
Ip! is not available to clients when processing client requests using the thread pool
Reason:
Since the Server_loop note loop only adds the connection word sock to the queue, there is no client IP, so each queue callback function can only get sock connection word, no client IP
Workaround:In the queue sock joins the IP also added to the IPs queue, the IPs queue length and queue, and the data stored in the same subscript, take sock processing also take the IP out, you can get the client IP
steps:Define a variable that holds the IP address globally
[CPP]View Plaincopyprint?
- static unsigned long ips[max_queue];
Static unsigned long ips[max_queue];
Modifying the Enqueue function
[CPP]View Plaincopyprint?
- int enqueue (soap_socket sock,unsigned long IP)//Add IP parameter
- {
- 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 the full queue,
- * Queue is full, return Soap_eom * /
- status = Soap_eom;
- }
- Else
- {
- Queue[tail] = sock;
- Ips[tail] = IP; //Save 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)//Add IP parameter { 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; if (head = = next) {/ * don ' t ' block on full queue, * Queue filled, return Soap_eom */ status = Soap_eom ; } else { Queue[tail] = sock; Ips[tail] = IP; Save 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;}
Add Dequeue_ip () function
[CPP]View Plaincopyprint?
- 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;}
Modifying the queue callback function function
[CPP]View Plaincopyprint?
- void *process_queue (void *soap)
- {
- struct Soap *tsoap = (struct soap*) soap;
- for (;;)
- {
- Tsoap->socket = Dequeue ();
- Tsoap->ip = Dequeue_ip (); //Get the appropriate IP address
- 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 ();//get the corresponding IP address if (!soap_valid_socket (tsoap->socket)) {#ifdef DEBUG fprintf (stderr, "Thread%d terminating\n", (int) (long) tsoap->user); #endif break ; }
Solve!
Test:Testing in the Http_get_handler function
[CPP]View Plaincopyprint?
</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); }
Output:
[Ruby]View Plaincopyprint?
- 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
gSOAP: Obtaining IP to the client when thread pool processing is implemented