简体   繁体   中英

Redirecting TCP NetworkStream traffic in a .NET socket server

I need to build a load balancer in C#.NET. I'll need to route requests based upon some criteria to an array of back end .NET Tcp socket servers.

What ways does .NET expose to redirect traffic? Is there some way to dynamically do port forwarding (ie where the target host changes based on the request)?

Any info is much appreciated. Thanks.

EDIT: I'm trying write a custom load balancer, so I should have said programmatic dynamic port forwarding. I'm trying to redirect client requests to back end machines with a customer software/algorithm.

.NET does not include any built-in load balancing or port forwarding capabilities. You will have to write that yourself or find a library that does it.

Win 2008 has NLB .. but questions about that (NLB) are more suited to ServerFault

Building a well working load-balancer is rather similar to building a proxy, although even more complicated - none is a simple task as you will have to understand and handle several protocols etc. in both directions...

I would recommend to use an existing library for that... the following are links about proxies which I think will help alot in building a load balancer:

Another helpful foundation might be SSH-libraries - some include support for tunneling which is part of what you need

IMHO .NET might not be the first choice for a load balancer on TCP-level since that needs maximum performance... depeding on your exact scenario it might even be necessary to build a kernel-module to achieve an acceptable level of performance - building a kernel-module is NOT possible with .NET, only in C / C++ (or for those who really want to get dirty assembler)...

REMARK:

IF you can change the client-side THEN there might much more efficient option to achieve what you want... you would implement a central service doing the "logical load-balancing", the clients ask this central service which backend they should connect to and then connect directly... after finishing they just signal that to the central service... it might even be better to get "load information" from the backend machines/services and calculate based on that...

Some of those products listed above will possibly allow you to do custom redirection based on various attributes of the request.

For example WinGate has several proxies (either http or generic TCP mapping) which you can use jscript or other options to change the destination on a per-request or per-connection basis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM