Proxying two connections

Hello everyone,
I'm new to .NET and C# and looking to recieve a little help with a test app that I'm writing. It takes a connection from an incomming client and forwards it to a server, thus acting as a middle man. I'm just looking to learn how to communicate with sockets a little better. However in some of the examples for TcpListener it shows that the code enters a while loop that depends on reading the data from the incomming connection. I'm a bit confused on how i should structure the loop if i'm reading in data from one socket and sending it out another and vice versa should i just enter a loop while(true) and break once either one of the sockets closes I'm basically just looking to implement something that i can connect to my localhost with RDP and have it forward to my terminal server. Any help would greatly be appreciated, and sorry if the question is mundane, i am new to this.
Thanks


Answer this question

Proxying two connections

  • bmellow

    Assuming you have two sockets: ClientToMiddleman and MiddlemanToServer

    You can start an asynchronous read on each of these sockets and if data is received from the client, send it to the server using MiddleManToServer. When data is received on the MiddleManToServer socket, it is then forwarded to the client using ClientToMiddleMan

    If you are not comfortable using the asynchronous APIs, then you can start one thread that will handle reading data from the client and forwarding it to the server and another thread that does the same but in the other direction.

    If you need an example of asynchronous socket usage, take a look at http://blogs.msdn.com/joncole/archive/2006/04/25/583510.aspx



  • Proxying two connections