3

I'm writing a proxy using .NET and C#. I haven't done much Socket programming, and I am not sure the best way to go about it. What would be the best way to implement this? Should I use Synchronous Sockets, Asynchronous sockets? Please help!

It must...

  • Accept Connections from the client on two different ports, and be able to receive data on both ports at the same time.
  • Connect to the server on two different ports, and be able to send data on both ports as the same time.

  • Immediately connect to the server and start forwarding packets as soon as a client connection is made.

  • Forward packets in the same order they were received.

  • Be as low latency as possible.

  • I don't need the ability for multiple clients to connect to the proxy, but it would be a nice feature if its easy to implement.

Client --------- Proxy ------- Server
---|-----------------|----------------|
Port <--------> Port <-------> Port
Port <--------> Port <-------> Port

Kin
  • 1,407
  • 20
  • 24

2 Answers2

3

I've tinkered with this source code before. It was done well, recommended.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This is quite easy to follow, thanks! It's not compiling with 3.5 though, and it is quite old. Is there anything specific I should avoid doing (outdated, or better way) that is used in this? – Kin Apr 12 '10 at 15:48
  • 1
    @kin - compile issues are easily fixed by compiling as .NET 1.1 (not tried) or by casting the expression inside the offending Math.Floor as double. – Frederik Aug 07 '14 at 21:12
  • Address compiler issue: Use BitConverter.ToInt64(IP.GetAddressBytes(), 0) vs IP.Address. – Benj Sanders Sep 15 '15 at 23:53
1

Please check out PortFusion! It is a complete, actively-used and -developed distributed reverse proxy solution for all TCP-based traffic. It is written for .NET 4.0 and comes with full source code.

I have tested ReceiveAsync and SendAsync methods introduced in .NET 3.5 but have not found them to reach as high a throughput as is possible with synchronous ones - Async ones should scale better in case of a great number of concurrent clients though.

Maximum throughput for a controlled number clients: use synchronous methods Reaching maximum number of concurrent connections: async

Cetin Sert
  • 4,497
  • 5
  • 38
  • 76