Click here to Skip to main content
6,595,444 members and growing! (18,515 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate

CAsyncProxySocket - CAsyncSocket derived class to connect through proxies

By Tim Kosse

This class allows you to establish connections through SOCKS4/5 and HTTP 1.1 proxies.
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:8 Dec 2001
Updated:18 Dec 2001
Views:162,485
Bookmarked:53 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 5.32 Rating: 4.09 out of 5
1 vote, 10.0%
1

2

3
4 votes, 40.0%
4
5 votes, 50.0%
5

Sample Image - casyncproxysocket\casyncproxysocket_demo.gif

Introduction

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)
{
CAsyncProxySocket::OnReceive(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:

  • PROXYTYPE_NOPROXY
  • PROXYTYPE_SOCKS4
  • PROXYTYPE_SOCKS4A
  • PROXYTYPE_SOCKS5
  • PROXYTYPE_HTTP11
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.

int GetProxyType()

Returns the used proxy type.

const int GetLastProxyError() const;

Returns the last proxy error.

History

  • 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.

License

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


Member

Occupation: Web Developer
Location: Germany Germany

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 66 (Total in Forum: 66) (Refresh)FirstPrevNext
Generaldownload links are down :( PinsussAnonymous17:03 13 Aug '04  
GeneralWhat about SSL 3? PinmemberAmiri2:20 20 May '04  
Generalhttp/1.1 server PinsussAnonymous20:22 26 Apr '04  
GeneralSocksified client PinmemberAnnoymous22:59 1 Mar '04  
GeneralRe: Socksified client PinsussAnonymous16:52 13 Aug '04  
GeneralConnect class PinsussAnonymous15:25 17 Feb '04  
GeneralWhere can i get the latest version of CAsyncProxySocket?? Pinmemberajiva17:46 17 Jan '04  
Generalhelp me with http1.1 proxy!!!! Pinmemberanglely16817:25 11 Nov '03  
GeneralHelp me -Proxy issue Pinmemberdharani0:49 8 Aug '03  
GeneralWhy I can't using CAsyncSocketEx::Send() function? PinmemberDreamsky19:22 26 Apr '03  
GeneralListening thru a proxy PinsussAnonymous3:32 21 Jan '03  
GeneralCProxySocket ?? Pinmemberirekz@yahoo.com4:17 20 Jan '03  
GeneralNon-MFC proxy class PinmemberBui Huy Kien16:00 30 Dec '02  
GeneralRe: Non-MFC proxy class PinsussAnonymous17:41 30 Dec '02  
GeneralRe: Non-MFC proxy class PinmemberBui Huy Kien17:56 30 Dec '02  
GeneralRe: Non-MFC proxy class PinmemberTim Kosse6:18 26 Mar '03  
QuestionRe: Non-MFC proxy class PinmemberMember 111362114:36 9 Oct '08  
GeneralProxy Server(s) To Test With Pinmemberwin32mfc9:26 30 Dec '02  
GeneralRe: Proxy Server(s) To Test With PinmemberTim Kosse10:35 30 Dec '02  
GeneralRe: Proxy Server(s) To Test With Pinmembersctsct21:33 6 Jul '06  
GeneralBug Pinsussallex23:40 28 Oct '02  
GeneralAnd one more Pinsussallex23:43 28 Oct '02  
GeneralRe: And one more PinsussMr. Anonymous10:16 30 Oct '02  
GeneralRe: Bug PinsussMr. Anonymous10:13 30 Oct '02  
GeneralRe: Bug PinmemberTim Kosse8:15 1 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Dec 2001
Editor: Smitha Vijayan
Copyright 2001 by Tim Kosse
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project