• 0

Basic winsock proxy


Question

Hi, I am writing an HTTP proxy for a web browser, using Winsock and C++. For now the proxy should behave as if there was no proxy, just redirecting the requests towards the correct server, and the html data towards the browser. Also, it is synchronous.

It works, but performance is quite bad currently. Before I try anything else, I am wondering if at least the basic logic is correct. Here's some pseudocode:

Initialize Winsock
Socket listenSocket
listenSocket.Bind(host, port)
listenSocket.Listen()

while true:
     Socket browserSocket = listenSocket.Accept()
     httpRequest = browserSocket.ReceiveRequest()
     Socket serverSocket
     serverSocket.Connect(getHostName(httpRequest), port 80)
     serverSocket.SendRequest(httpRequest)
     transmitHtml(serverSocket, browserSocket)
     browserSocket.Close()
     serverSocket.Close()

listeSocket.Close()

I am mainly wondering if it is correct to close both browser and server sockets and create new ones everytime. For loading a simple page like Google, it will go 3 times through that loop; more involved pages will go numerous times through it.

Thanks!

Link to comment
https://www.neowin.net/forum/topic/889590-basic-winsock-proxy/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

The slow step with almost every webpage is the number of HTTP requests. Every image, every AJAX call, etc involves a separate round trip HTTP request and response. This is why lots of high-traffic webpages use CSS-sliced images (one image = one HTTP request).

Each HTTP request in your code will block subsequent requests until said request finishes processing. You need to do this asynchronously (multiple requests being handled at the same time) to increase performance.

As for your specific code, use MSDN. Although it has tons of bad programming practices in it, it is hands down the best piece of programming documentation on the planet, especially for the Windows API. Check out the Winsock reference, complete with usage scenarios and examples, some of which will cover your specific problem. Note that multi-threaded sockets programming is not an easy kill; even the experts have trouble with this stuff.

And yes, consider doing this in .NET. Version 3.5 is very nice and version 4.0 coming up has some new high-performance sockets implementations that would eliminate 90% of the headaches you'd get into doing native code.

If you're gonna stick with C++, recognize that someone else has already done what you want to do, except they've done it better than you will. Consider using a library, such as the Boost ASIO library. I hate cliches, but don't reinvent the wheel.

  • 0

Thank you. I was starting to think that the main bottleneck was probably the fact that this is synchronous, but just to be sure. So the logic seems correct to you?

This is an assignment so the language, library and high-level design choices (like making it synchronous) are forced. And yes I got a lot of MSDN code in there. :laugh:

  • 0

Well, it is possible to keep the HTTP connection open for more than one request.

You haven't said what you mean by bad performance though. I am guessing a big bottleneck though is that it is synchronous. In the real world, it's normal to have up to four connections to a 1.0 server and two connections to a 1.1 (which you keep open, or persist, for more than one request.) I'm guessing this is far out of scope for your assignment though, so I wouldn't worry about it. You'll want to understand the synchronous part (even though it's not really used on Windows) before you start venturing into asynchronous I/O and multi-threading.

  • 0
  On 05/04/2010 at 10:43, hdood said:
You haven't said what you mean by bad performance though.
Most sites load approximately 3x slower than normal, but some pages give me bigger issues. A page like http://en.wikipedia.org/wiki/World_War_I takes ages to load, with various 10060 (connection timed out) and 10054 (connection reset by peer) along the way. When the browser finally says "Done", the page is still missing a lot of formatting and pictures.

msnd.com loads fast, but like Wikipedia, when the browser says "Done" (and doesn't make any more connections to the proxy), the page seems to be missing stuff.

sanstitreay.jpg

yahoo.com simply doesn't work. The browser says "Done" and the page is still blank. :blink: But google works flawlessly along with most simple sites.

Also wondering what should I use for receive buffers (second argument for recv). For now I use a local char[30000] and I immediatly forward the result to the browser, and then repeat until the return value is <= 0. Does that make sense?

  • 0

Just wanted to say I asked the above question to my teacher and he said it was normal some pages would be incomplete with a synchronous proxy, given that Firefox tries to make several requests in parallel and will give up if they are not satisfied quickly enough.

After all, the level of performance is what should be expected of such a naïve implementation I think.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • How to reduce the annoying Liquid Glass effects on iOS 26 by Aditya Tiwari Apple announced Liquid Glass at WWDC 2025 in all of its glory. It's a new glass-inspired design language from the iPhone-maker making its way to most of its software platforms, including iOS 26, iPadOS 26, macOS 26, tvOS 26, and watchOS 26. Apple markets Liquid Glass as a new translucent material that "behaves like glass in the real world. Its color is informed by surrounding content and intelligently adapts between light and dark environments." The Cupertino giant isn't wrong. The new glass-inspired design does bring a fresh coat of paint and some beautiful visuals to the iPhone and other Apple-made devices. However, as the dust from the mega arrival settles, people are starting to realize the current drawbacks of having too much glass on their device. There have been reports of users, including those at Neowin, experiencing readability issues, background separation issues, and trouble focusing on the content displayed on the screen due to its transparent nature. While these are small nuances on Apple's part that can be fixed without much hassle, it's degrading the initial experience of what the company calls its biggest visual upgrade since iOS 7. That said, there are some workarounds built into iOS 26 that will help you tone down the shortcomings. If you're running the iOS 25 Developer Beta 1 on your supported iPhone model, you can follow these steps to make things a little better: Go to the Settings app on your iPhone. Scroll down and tap on Accessibility > Display & Text Size. Next, you'll find two toggle options: Reduce Transparency and Increase Contrast. When you enable the Reduce Transparency toggle button, iOS 26 can "improve contrast by reducing transparency and blurs on some backgrounds to increase legibility." The other toggle, Increase Contrast, does what its name suggests. It can "increase color contrast between app foreground and background colors." Enabling each of these toggle buttons individually or simultaneously will have different effects on your iPhone's user interface. Control Center is being criticized for the extra transparency, making the UI look cluttered and difficult to focus. Here, the Reduce Transparency button adds a darker background to make the Control Center UI elements stand out. Liquid Glass transparency issues are also prevalent in the Notification Center and the navigation controls of various apps. In the image above, see how the text of the navigation buttons has become unreadable or hard to focus on with certain backgrounds. It's challenging to determine what can trigger such behavior, as one can have countless color combinations on their device. In the image below, this is how these buttons look when both Reduce Transparency and Increase Contrast are enabled. It's still a hit or miss depending on what colors you are dealing with. Part of the blame also goes to the fact that iOS 26 is still an early beta, and the change doesn't render as intended every time. You can also notice the difference in the look and feel of the Control Center when reduced transparency is turned on. On a side note, you can add the Reduce Transparency and Increase Contrast buttons in the Control Center for quicker access. That said, let's wait to see what changes Apple will implement as it continues to gather feedback through the beta program. Hopefully, the software will become more stable when the first public beta of iOS 26 arrives sometime in July. Interested users can try out the latest iPhone update through the developer beta program, noting that early builds might come with unexpected bugs and issues.
    • Austin residents rally against Tesla's robotaxi launch by Hamid Ganji Tesla's plan to launch its robotaxi service in Austin, Texas, has sparked protests in the city. While the EV maker targets June 22 as the launch date, local residents are raising their voices against the plan due to political disagreements and safety concerns. As reported by CNBC, public safety advocates and political protesters are organizing protests against Tesla's robotaxi launch in Austin. Members of the Dawn Project, Tesla Takedown, and Resist Austin have cited safety issues with Tesla's automated driving systems. Meanwhile, Elon Musk's involvement in Donald Trump's administration and his work in the Department of Government Efficiency (DOGE) has prompted another group of people to join the Austin protests against Tesla. To show Austin citizens the safety problems of Tesla's self-driving system, The Dawn Project brought a Tesla Model Y to the protest, equipped with the company's Full Self-Driving (FSD) software (version 13.2.9). In the demonstration, the Model Y with FSD software reportedly zoomed past a school bus with a stop sign held out and ran over a child-sized mannequin placed in front of the car. The FSD package includes automatic lane-keeping, steering, and parking. It is unclear to what extent this test was conducted under standard conditions or what Tesla's defense is. However, multiple cases of Tesla FSD software malfunction in the past have resulted in collisions or severe accidents, according to data collected by National Highway Traffic Safety Administration. Interestingly, The Dawn Project CEO Dan O'Dowd is the CEO of another company that sells embedded safety and security solutions to carmakers like Ford and Toyota. It remains to be seen whether Tesla could launch a robotaxi service in Austin despite the residents' protests. Earlier this week, Elon Musk demonstrated the new version of Tesla's FSD software in a post on X. Musk's decisions on DOGE fuel public backlash against Tesla. While Musk left the Trump administration after a beef with the president, he's still a target of public criticism, which directly imapcts Tesla. As a result, the slow sales have even caused Tesla to sell its Cybertruck with 0 percent APR to boost sales.
    • I'm sure Denmark would stand to lose a lot if US consumers stopped buying Danish products, whether that's Lurpak butter or hi-fi equipment.
    • JD Vance will be the next President. Who've the Democrats got? Harris again? lol....
    • Microsoft Edge gets new password feature and security fixes by Taras Buria Microsoft has released a new update for the Edge browser in the Stable Channel. Version 137.0.3296.83 introduces a new password feature and fixes security vulnerabilities to make your browsing experience safer. Starting with new features, Microsoft Edge 137 now supports Secure Password Deployment. Microsoft recently announced this for IT admins, allowing them to share encrypted passwords with user groups. This service lets users log into websites without seeing their passwords, thus enhancing the organization's security. You can read more about Microsoft Edge Secure Password Deployment in our recent article here. Security updates in Microsoft Edge 137.0.3296.83 include two fixes for Chromium vulnerabilities: CVE-2025-5958: Use after free in Media in Google Chrome prior to 137.0.7151.103 allowed a remote attacker to potentially exploit heap corruption via a crafted HTML page. (Chromium security severity: High) CVE-2025-5959: Type Confusion in V8 in Google Chrome prior to 137.0.7151.103 allowed a remote attacker to execute arbitrary code inside a sandbox via a crafted HTML page. (Chromium security severity: High) You can update Microsoft Edge to the latest version by heading to edge://settings/help. The browser can also update itself automatically in the background and apply updates between restarts. In case you missed it, Microsoft released Edge 137 by the end of May. The update deprecated quite a lot of existing features, including Wallet, Image Editor, Image Hover, Mini menu, and Video Super Resolution. It also introduced Web Content Filtering and enhancements for the picture-in-picture player and Find on Page in Microsoft Edge for Business. The next feature update for Microsoft Edge, version 138, is expected on the week of June 26, 2025, as part of the standard four-week release cadence.
  • Recent Achievements

    • Week One Done
      LagFighterZ earned a badge
      Week One Done
    • First Post
      ThatGuyOnline earned a badge
      First Post
    • One Month Later
      5i3zi1 earned a badge
      One Month Later
    • Week One Done
      5i3zi1 earned a badge
      Week One Done
    • Week One Done
      julien02 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      546
    2. 2
      ATLien_0
      229
    3. 3
      +FloatingFatMan
      165
    4. 4
      Michael Scrip
      119
    5. 5
      +Edouard
      91
  • Tell a friend

    Love Neowin? Tell a friend!