Click here to Skip to main content
Click here to Skip to main content

A multi-threaded HTTP proxy server based on Win32

By , 5 Dec 2007
 

Introduction

This program demonstrates the basic use of WinSock and multi-threading. Multi-threading is important especially for server-side programming, so an HTTP proxy server is a very good example to show how we can use multi-threading to improve performance.

Using the Code

The code contains only one source file for simplicity, and it's a console application. The proxy port is defined as 6666, and you can easily change the parameters at the beginning of the source code file (myproxy.cpp).

//just define the space character
#define SPACE_CHAR  ' '
// port for proxy server, you can change it to whatever
#define PROXY_PORT  6666
//The buffer size is for reading from the remote host 
//where you are getting the HTTP response
#define RESPONSE_BUFFER_SIZE 8192
#define MAX_THREADS   50  //How many threads you want to have

//global variable, all incoming socket connection is put to the queue
queue<SOCKET> socketBuffer;

HANDLE queueMutex;
//mutex for the queue

Points of Interest

An HTTP proxy server is thought to be easy, because you actually do nothing except pass the request and response around. However, it is not that easy to implement as we think.

With the popularity of AJAX techniques, more and more websites have adopted this technology, such as YouTube, Google Maps, Flickr... So what's the difference?

The difference is, these so called Web 2.0 websites actually generate a lot of concurrent connections than regular sites. In fact, each image file is transferred in an individual channel. Think about Google Maps, how many small images are there in each page? Each of them is going to create a connection (OK, actually the connection is generated from your HTTP client web browser, not from the server).

Another interesting point is the "keep-alive" option in the "Connnection: " field of the HTTP response. It means even if you cannot use recv() to read any data, you should keep the connection open for future use. It's mostly used for websites like YouTube and other streaming video applications so the client could keep the connection open for later data transfer.

Sounds good? No. In my experiments, a lot of "badly-designed" websites send back "Connection: keep-alive" while they never send any data back... In this case, the solution is to set a timeout for the receiving action. Since recv() doesn't have a timeout parameter, we need to use the Winsock extension WSARecv(...).

History

  • First uploaded: 12/5/2007.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

zlike
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Layout  Per page   
Generalprocessor usagememberviliam11 Sep '08 - 2:06 
Cool and simple.
 
But processor usage is awful.
It is because of construction with queueMutex in RequestThread
 
Most of the time the mutex is free and each thread is just loop
 
viliam

GeneralRe: processor usagememberDenis Panov16 Oct '08 - 22:51 
Yes! You must use such functions:
 
1. queueMutex = CreateEvent(NULL, FALSE, FALSE, NULL);
2. WaitForSingleObjectEx(queueMutex,INFINITE, TRUE);
3. SetEvent(queueMutex);
 
And it will work without processor usage Wink | ;)
GeneralRe: processor usagemembermbaocha6 May '09 - 12:29 
I tried using that code but no much difference with processor usage. I didnt expect to see differences anyway. The processor thing is connected to the implementation and nothing much can be done about that.
 

_____________________________________
Digital Map Nigeria | Street Level Map of Lagos Abuja Nigeria |
GIS company in Nigeria
GeneralCoolmemberHexxxxx3 Sep '08 - 19:18 
It's the only source code of the proxy server I could find in the net.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 5 Dec 2007
Article Copyright 2007 by zlike
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid