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

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

Comments and Discussions

Generaldownload links are down :( PinsussAnonymous13-Aug-04 16:03 
QuestionWhat about SSL 3? PinmemberAmiri20-May-04 1:20 
Generalhttp/1.1 server PinsussAnonymous26-Apr-04 19:22 
GeneralSocksified client PinmemberAnnoymous1-Mar-04 21:59 
GeneralRe: Socksified client PinsussAnonymous13-Aug-04 15:52 
GeneralConnect class PinsussAnonymous17-Feb-04 14:25 
QuestionWhere can i get the latest version of CAsyncProxySocket?? Pinmemberajiva17-Jan-04 16:46 
Generalhelp me with http1.1 proxy!!!! Pinmemberanglely16811-Nov-03 16:25 
GeneralHelp me -Proxy issue Pinmemberdharani7-Aug-03 23:49 
Hello author/all
I have some trouble with socket programming I request help from anybody . I am listing out my problem below :
I am trying to develope a proxy server for shoutcast server . Usually the DFP Plug In of winAmp enables connection to a shoutcast server and streams the voice data in mp3 format to it . The DSP Pulg In connects to a server bt inputting the server's IP + port number where its listening for requests for connection.So after the connection is established with the server at the IP+port specified the DSP Plug In streams voice data to the server .The shoutcast server in turn streams the incoming voice data to listeners.The listeners connect to the Shoutcast server at its port and ip and listen to the broadcast coming out of the shoutcast server . But as the shoutcast server is designed for radio broadcast there is an acute delay of about 20 seconds for the voice to be heard at the client end . So in order to reduce the latency I am trying to write a proxy server that will listen from the DSP plug In in the place of Shoutcast server and the proxy will in trun redirect the stream to shoutcast server so that now shoutcast server broadcasts it to the clients.I also found that the DSP pulgIn of winamp from the broadcaster side is of type TCP so i created a server socket as below
if ((sSock= socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
AfxMessageBox("server creation failed");
sockaddr.sin_family = AF_INET;
sockaddr.sin_port= htons(m_Port);
bind (sSock, (struct sockaddr*) &sockaddr, sizeof (sockaddr));
listen (sSock, 5);
HWND hwnd= GetSafeHwnd();
here the sSock is the server socket and its initialized to the port + ip and in the WSAAsyncSelect() i have used a CONNECT_MESSAGE to be fired in case of Connection attempts from clients .And In the handler function of the CONNECT_MESSAGE I am call accept() to accept this new client but the DSP is not connecting and if I debug the server appln it is not even receiving any connection attempts .I used ur simple TCP server (console ) also but its not receiving any attempted connections .
So please advise me how i can solve this problem . Help me by ur ideas with regards Dharani babu


QuestionWhy I can't using CAsyncSocketEx::Send() function? PinmemberDreamsky26-Apr-03 18:22 
GeneralListening thru a proxy PinsussAnonymous21-Jan-03 2:32 
QuestionCProxySocket ?? Pinmemberirekz@yahoo.com20-Jan-03 3:17 
GeneralNon-MFC proxy class PinmemberBui Huy Kien30-Dec-02 15:00 
GeneralRe: Non-MFC proxy class PinsussAnonymous30-Dec-02 16:41 
GeneralRe: Non-MFC proxy class PinmemberBui Huy Kien30-Dec-02 16:56 
GeneralRe: Non-MFC proxy class PinmemberTim Kosse26-Mar-03 5:18 
QuestionRe: Non-MFC proxy class PinmemberMember 11136219-Oct-08 13:36 
GeneralProxy Server(s) To Test With Pinmemberwin32mfc30-Dec-02 8:26 
GeneralRe: Proxy Server(s) To Test With PinmemberTim Kosse30-Dec-02 9:35 
GeneralRe: Proxy Server(s) To Test With Pinmembersctsct6-Jul-06 20:33 
GeneralBug Pinsussallex28-Oct-02 22:40 
GeneralAnd one more Pinsussallex28-Oct-02 22:43 
GeneralRe: And one more PinsussMr. Anonymous30-Oct-02 9:16 
GeneralRe: Bug PinsussMr. Anonymous30-Oct-02 9:13 
GeneralRe: Bug PinmemberTim Kosse1-Nov-02 7:15 
GeneralRe: Design problems PinsussM...18-Jan-04 3:12 
GeneralNon-MFC use PinsussAnonymous15-Sep-02 18:35 
GeneralWSAEWOULDBLOCK PinsussAnonymous7-Sep-02 2:15 
GeneralRe: WSAEWOULDBLOCK PinsussMr. Anonymous30-Oct-02 9:11 
Generalneed help ..urgent PinsussAnonymous30-Aug-02 4:23 
GeneralRe: need help ..urgent PinmemberTim Kosse30-Aug-02 5:13 
GeneralRe: need help ..urgent PinsussAnonymous31-Aug-02 0:03 
GeneralIf Client and Server are both behind proxies and using this class PinsussAnonymous30-Aug-02 4:04 
Questionhow to send UDP message by SOCKET5 Pinmemberzsty12-Aug-02 22:21 
GeneralWhere PinmemberAlex22427-Jun-02 6:58 
GeneralRe: Where PinmemberTim Kosse27-Jun-02 8:20 
GeneralRe: Where PinsussAnonymous21-Sep-02 20:25 
Generali couldn't create socket..pls... help... Pinmembermin chan-ki6-Jun-02 22:52 
GeneralRe: i couldn't create socket..pls... help... PinmemberTim Kosse15-Jun-02 8:23 
Generalhttp 1.1 PinmemberJosé Leandro Massada29-Apr-02 13:52 
GeneralRe: http 1.1 PinmemberTim Kosse31-May-02 10:17 
GeneralCasyncProxysocket problem 0:17 
GeneralRe: CasyncProxysocket problem PinmemberTim Kosse15-Jun-02 8:29 
GeneralDetermining Proxy type PinmemberAnonymous20-Mar-02 6:07 
GeneralRe: Determining Proxy type PinmemberTim Kosse20-Mar-02 6:51 
GeneralRe: Determining Proxy type Pinmemberjaf30-Aug-02 1:34 
GeneralUDP PinmemberAnonymous16-Feb-02 14:00 
GeneralRe: UDP PinmemberAnonymous7-Apr-02 5:03 
QuestionHow can I modify CAsyncProxySocket to use in None-MFC program? PinmemberBui Huy Kien13-Jan-02 16:39 
AnswerRe: How can I modify CAsyncProxySocket to use in None-MFC program? PinmemberTim Kosse14-Jan-02 2:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.141015.1 | Last Updated 19 Dec 2001
Article Copyright 2001 by Tim Kosse
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid