Click here to Skip to main content
14,846,621 members
Articles / Desktop Programming / MFC
Posted 8 Dec 2001


79 bookmarked

CAsyncProxySocket - CAsyncSocket derived class to connect through proxies

Rate me:
Please Sign up or sign in to vote.
4.73/5 (12 votes)
18 Dec 20012 min read
This class allows you to establish connections through SOCKS4/5 and HTTP 1.1 proxies.

Sample Image - casyncproxysocket\casyncproxysocket_demo.gif


This class is a CAsyncSocket derived class. With this class, you can connect through SOCKS4/5 and HTTP 1.1 proxies. This class works as semi-transparent layer between your own class and CAsyncSocket. This class is used in FileZilla, a powerful open-source FTP client. It can be found here. For more information about SOCKS4/5, go here. For more information about HTTP 1.1, go here and search for RFC2616 and RFC2617.

How to use?

If you are using CAsyncSocket, you don't have to change much in your already existing code to use CAsyncProxySocket. To use it, you've to derive your own class from CAsyncProxySocket instead from CAsyncSocket, and you've to provide a definition for OnProxyOperationFailed(int nOpID) and OnListenFinished(unsigned long &retProxyIp,int &retProxyPort). When you create an instance of your derived class, you should call SetProxy. If you overload the OnReceive or OnConnect functions in your derived class, you have to call CAsyncProxySocket::OnReceive and CAsyncProxySocket::OnConnect as first instruction in your OnReceive and OnConnect functions.

Sample code:

  1  CMyClass::OnReceive(int nErrorCode)
  2  {
  3  CAsyncProxySocket::OnReceive(nErrorCode);
  4  //Add you own implementation below:
  5  ...
  6  }

If an operation fails, OnProxyOperationFailed is called. There you can handle the error. int nOpID specifies the type of the operation that failed: PROXYOP_CONNECT when a connection attempt failed and PROXYOP_LISTEN when creating a listen socket failed.

If you want to use CAsyncProxySocket to create a listen socket, you have to use this overloaded function: virtual BOOL Listen(unsigned long serverIp). serverIP is the IP of the server you are already connected through the SOCKS proxy. You can't use listen sockets over a SOCKS proxy without a primary connection. Listen sockets are only supported by SOCKS proxies, this won't work with HTTP proxies. When the listen socket is created successfully, OnListenFinished is called. The parameters unsigned long &retProxyIp and int &retProxyPort will tell you the IP and the port of the listen socket. After that, you have to handle the OnAccept message and accept the connection.

Description of the other new functions and their parameters

  1  void SetProxy(int nProxyType);
  2  void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort);
  3  void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort,CString 
  4  ProxyUser, CString ProxyPass);

Call one of these functions to set the proxy type. Parameters:

  • nProxyType specifies the Proxy Type.
  • ProxyHost and nProxyPort specify the address of the proxy.
  • ProxyUser and ProxyPass are only available for SOCKS5 proxies.

Supported proxy types:

  1  GetProxyPeerName

GetProxyPeerName is like GetPeerName of CAsyncSocket, but returns the address of the server connected through the proxy. If using proxies, GetPeerName only returns the address of the proxy.

  1  int GetProxyType()

Returns the used proxy type.

  1  const int GetLastProxyError() const;

Returns the last proxy error.


  • 20 Dec 2001
    • Added basic HTTP 1.1 authentication.
    • Fixed memory leak in SOCKS5 code.
    • OnSocksOperationFailed will be called after Socket has been closed.
    • Fixed some minor bugs.
  • 9 Dec 2001
    • Initial release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Tim Kosse
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

Generaldownload links are down :( Pin
Anonymous13-Aug-04 16:03
MemberAnonymous13-Aug-04 16:03 
QuestionWhat about SSL 3? Pin
Amiri20-May-04 1:20
MemberAmiri20-May-04 1:20 
Generalhttp/1.1 server Pin
Anonymous26-Apr-04 19:22
MemberAnonymous26-Apr-04 19:22 
GeneralSocksified client Pin
Member 9191301-Mar-04 21:59
MemberMember 9191301-Mar-04 21:59 
GeneralRe: Socksified client Pin
Anonymous13-Aug-04 15:52
MemberAnonymous13-Aug-04 15:52 
GeneralConnect class Pin
Member 91913017-Feb-04 14:25
MemberMember 91913017-Feb-04 14:25 
QuestionWhere can i get the latest version of CAsyncProxySocket?? Pin
ajiva17-Jan-04 16:46
Memberajiva17-Jan-04 16:46 
Generalhelp me with http1.1 proxy!!!! Pin
anglely16811-Nov-03 16:25
Memberanglely16811-Nov-03 16:25 
GeneralHelp me -Proxy issue Pin
dharani7-Aug-03 23:49
Memberdharani7-Aug-03 23:49 
QuestionWhy I can't using CAsyncSocketEx::Send() function? Pin
Dreamsky26-Apr-03 18:22
MemberDreamsky26-Apr-03 18:22 
GeneralListening thru a proxy Pin
Anonymous21-Jan-03 2:32
MemberAnonymous21-Jan-03 2:32 
QuestionCProxySocket ?? Pin
Member 16157220-Jan-03 3:17
MemberMember 16157220-Jan-03 3:17 
GeneralNon-MFC proxy class Pin
Bui Huy Kien30-Dec-02 15:00
MemberBui Huy Kien30-Dec-02 15:00 
GeneralRe: Non-MFC proxy class Pin
Anonymous30-Dec-02 16:41
MemberAnonymous30-Dec-02 16:41 
GeneralRe: Non-MFC proxy class Pin
Bui Huy Kien30-Dec-02 16:56
MemberBui Huy Kien30-Dec-02 16:56 
GeneralRe: Non-MFC proxy class Pin
Tim Kosse26-Mar-03 5:18
MemberTim Kosse26-Mar-03 5:18 
QuestionRe: Non-MFC proxy class Pin
Member 11136219-Oct-08 13:36
MemberMember 11136219-Oct-08 13:36 
GeneralProxy Server(s) To Test With Pin
win32mfc30-Dec-02 8:26
Memberwin32mfc30-Dec-02 8:26 
GeneralRe: Proxy Server(s) To Test With Pin
Tim Kosse30-Dec-02 9:35
MemberTim Kosse30-Dec-02 9:35 
GeneralRe: Proxy Server(s) To Test With Pin
sctsct6-Jul-06 20:33
Membersctsct6-Jul-06 20:33 
GeneralBug Pin
allex28-Oct-02 22:40
Memberallex28-Oct-02 22:40 
GeneralAnd one more Pin
allex28-Oct-02 22:43
Memberallex28-Oct-02 22:43 
GeneralRe: And one more Pin
Mr. Anonymous30-Oct-02 9:16
MemberMr. Anonymous30-Oct-02 9:16 
GeneralRe: Bug Pin
Mr. Anonymous30-Oct-02 9:13
MemberMr. Anonymous30-Oct-02 9:13 
GeneralRe: Bug Pin
Tim Kosse1-Nov-02 7:15
MemberTim Kosse1-Nov-02 7:15 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.