Solved

In Visual C++, how do I connect to an HTTP server via a firewall.

Posted on 2002-04-04
10
237 Views
Last Modified: 2013-12-14
I am developing Visual C++ software that must be able to connect to its server from behind a firewall.  Can anyone out there explain how to do this for SOCKS4, SOCKS5, and any other types of firewall?  Thanks.
0
Comment
Question by:dmcamens
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
The WinInet functions should handle that transparently by using the local internet settings - AFAIK there's no need to make any special considerations in your program...
0
 

Author Comment

by:dmcamens
Comment Utility
I have tried to use the WinInet classes and the problem with them is that they do not support asynchronous HTTP requests, as far as I could tell.  The problem with that is that I have to wait for a request to be completely processed before making another one.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
I'm talking about the WinInet API, not the MFC classes that are wrapped around them - 'WSARecv()' and 'WSASend()' both can handle asynchronous requests...
0
 

Author Comment

by:dmcamens
Comment Utility
How do I get WSARecv and WSASend to allow me to send/recv the requests through a firewall?  Is there another API function to configure the firewall parameters such as the proxy name, port, and protocol?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>Is there another API function to configure the firewall
>>parameters such as the proxy name, port, and protocol?

You don't have to - these functions use the local computer's settings. IOW, once you have configured your machine to access the Internet through a firewall, the winsock layer takes care of that. BTW, these are low-level functions operating on sockets. The WinInet API is a bit more high-level, and even withthe MFC wrapper classes you can have requests running asynchronously by using multiple threads...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:dmcamens
Comment Utility
I just tried out the WSA functions.  I am testing it out with WSAAsyncGetHostByName.  I cannot seem to get this to work through a firewall (my machine is configured properly).  When I try to use the MFC classes (see code below), it gets through the firewall but I don't think that I can use multiple threads to make it asynchronous.  I believe I tried that before and the different threads were blocking each other as far as I remember.  Note that I supplied the proxy name into the CInternetSession constructor.  Without that proxy name it does not work.

if (IsDlgButtonChecked(IDC_PROXY_CHECK))
{
    m_proxyEdit.GetWindowText(proxyName);
    dwAccessType = INTERNET_OPEN_TYPE_PROXY;
}
         
iSession = new CInternetSession(
NULL,
1,
dwAccessType,
proxyName.GetBuffer(0),
NULL,
0);                                            
hConnection = iSession->GetHttpConnection(server);
         
pHttpFile = hConnection->OpenRequest("GET",filename.GetBuffer(0));
     
pHttpFile->SendRequest();
nBytes = pHttpFile->Read(httpResponse, 999);
httpResponse[nBytes] = '\0';
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
>>firewall but I don't think that I can use multiple threads to make it asynchronous

Yes you can.  It's pretty straightforward.  For simplicity, create, use, and destroy the session and connection all within the thread proc.  Don't try to pass these objects around unless you want to do extra work.

-- Dan
0
 

Author Comment

by:dmcamens
Comment Utility
Ok, I see what you mean now.  I was trying to use the same CInternetSession object in each thread to take advantage of the fact that HTTP can keep a session going instead of connecting and disconnecting for each and every request.  That may be a performance hit but we may be able to live with it.  Do you know a way to still take advantage of the "keep alive" functionality?
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
Comment Utility
I'm not sure about 'keep alive' I think it has to do with server-side handling.

On the client side, if you never close the session, then the session remains 'live' and you can reuse it to go to the same server again and again with OpenURL() et. al.  

-- Dan
0
 

Author Comment

by:dmcamens
Comment Utility
Dan,

Thanks for your help.  I think I will go with the multithreading WinInet classes since the prototype I built seems to be working through the firewall now.

Thanks,
-Doug
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now