Click here to Skip to main content
12,064,656 members (23,491 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked

CAsyncProxySocket - CAsyncSocket derived class to connect through proxies

, 18 Dec 2001
Rate this:
Please Sign up or sign in to vote.
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:

CMyClass::OnReceive(int nErrorCode)
//Add you own implementation below:

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

void SetProxy(int nProxyType);
void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort);
void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort,CString 
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:


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.

int GetProxyType()

Returns the used proxy type.

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

You may also be interested in...

Comments and Discussions

GeneralRe: Design problems Pin
M...18-Jan-04 4:12
sussM...18-Jan-04 4:12 
GeneralNon-MFC use Pin
Anonymous15-Sep-02 19:35
sussAnonymous15-Sep-02 19:35 
Anonymous7-Sep-02 3:15
sussAnonymous7-Sep-02 3:15 
Mr. Anonymous30-Oct-02 10:11
sussMr. Anonymous30-Oct-02 10:11 
Generalneed help ..urgent Pin
Anonymous30-Aug-02 5:23
sussAnonymous30-Aug-02 5:23 
GeneralRe: need help ..urgent Pin
Tim Kosse30-Aug-02 6:13
memberTim Kosse30-Aug-02 6:13 
GeneralRe: need help ..urgent Pin
Anonymous31-Aug-02 1:03
sussAnonymous31-Aug-02 1:03 
GeneralIf Client and Server are both behind proxies and using this class Pin
Anonymous30-Aug-02 5:04
sussAnonymous30-Aug-02 5:04 
Questionhow to send UDP message by SOCKET5 Pin
zsty12-Aug-02 23:21
memberzsty12-Aug-02 23:21 
GeneralWhere Pin
Alex22427-Jun-02 7:58
memberAlex22427-Jun-02 7:58 
GeneralRe: Where Pin
Tim Kosse27-Jun-02 9:20
memberTim Kosse27-Jun-02 9:20 
GeneralRe: Where Pin
Anonymous21-Sep-02 21:25
sussAnonymous21-Sep-02 21:25 
Generali couldn't create socket..pls... help... Pin
min chan-ki6-Jun-02 23:52
membermin chan-ki6-Jun-02 23:52 
GeneralRe: i couldn't create socket..pls... help... Pin
Tim Kosse15-Jun-02 9:23
memberTim Kosse15-Jun-02 9:23 
Generalhttp 1.1 Pin
José Leandro Massada29-Apr-02 14:52
memberJosé Leandro Massada29-Apr-02 14:52 
GeneralRe: http 1.1 Pin
Tim Kosse31-May-02 11:17
memberTim Kosse31-May-02 11:17 
GeneralCasyncProxysocket problem Pin 1:17 1:17 
GeneralRe: CasyncProxysocket problem Pin
Tim Kosse15-Jun-02 9:29
memberTim Kosse15-Jun-02 9:29 
GeneralDetermining Proxy type Pin
Anonymous20-Mar-02 7:07
memberAnonymous20-Mar-02 7:07 
GeneralRe: Determining Proxy type Pin
Tim Kosse20-Mar-02 7:51
memberTim Kosse20-Mar-02 7:51 
GeneralRe: Determining Proxy type Pin
jaf30-Aug-02 2:34
memberjaf30-Aug-02 2:34 
GeneralUDP Pin
Anonymous16-Feb-02 15:00
memberAnonymous16-Feb-02 15:00 
GeneralRe: UDP Pin
Anonymous7-Apr-02 6:03
memberAnonymous7-Apr-02 6:03 
QuestionHow can I modify CAsyncProxySocket to use in None-MFC program? Pin
Bui Huy Kien13-Jan-02 17:39
memberBui Huy Kien13-Jan-02 17:39 
AnswerRe: How can I modify CAsyncProxySocket to use in None-MFC program? Pin
Tim Kosse14-Jan-02 3:00
memberTim Kosse14-Jan-02 3:00 
GeneralRe: How can I modify CAsyncProxySocket to use in None-MFC program? Pin
Bui Huy Kien16-Jan-02 19:21
memberBui Huy Kien16-Jan-02 19:21 
GeneralRe: How can I modify CAsyncProxySocket to use in None-MFC program? Pin
Tim Kosse17-Jan-02 3:32
memberTim Kosse17-Jan-02 3:32 
AnswerRe: How can I modify CAsyncProxySocket to use in None-MFC program? Pin
Tim Kosse26-Mar-03 6:21
memberTim Kosse26-Mar-03 6:21 
GeneralRe: Refresh web page problem Pin
VCLayman11-Dec-01 19:21
memberVCLayman11-Dec-01 19:21 
GeneralRe: Refresh web page problem Pin
Tim Kosse12-Dec-01 4:14
memberTim Kosse12-Dec-01 4:14 
GeneralRe: Refresh web page problem Pin
VCLayman13-Dec-01 20:47
memberVCLayman13-Dec-01 20:47 
GeneralRe: Refresh web page problem Pin
Tim Kosse14-Dec-01 2:58
memberTim Kosse14-Dec-01 2:58 
GeneralRequest Pin
Giles11-Dec-01 1:39
memberGiles11-Dec-01 1:39 
GeneralRe: Request Pin
Tim Kosse11-Dec-01 2:07
memberTim Kosse11-Dec-01 2:07 
GeneralRe: Request Pin
Giles12-Dec-01 0:17
memberGiles12-Dec-01 0:17 
GeneralRe: Request Pin
Tim Kosse12-Dec-01 3:34
memberTim Kosse12-Dec-01 3:34 
GeneralRe: Request Pin
Giles13-Dec-01 5:25
memberGiles13-Dec-01 5:25 
GeneralRe: Request Pin
Tim Kosse13-Dec-01 6:01
memberTim Kosse13-Dec-01 6:01 
GeneralRe: Request Pin
Brad Bruce14-Dec-01 3:53
memberBrad Bruce14-Dec-01 3:53 
GeneralRe: Request Pin
Tim Kosse14-Dec-01 4:41
memberTim Kosse14-Dec-01 4:41 
General.Great!! Pin
Giles10-Dec-01 11:22
memberGiles10-Dec-01 11:22 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160204.4 | Last Updated 19 Dec 2001
Article Copyright 2001 by Tim Kosse
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid