Link to home
Create AccountLog in
C#

C#

--

Questions

--

Followers

Top Experts

Avatar of gmain
gmain🇺🇸

Remote Desktop port redirecter in C#
I am trying to write an application which will take an incoming connection from a Remote Desktop client and perform a simple port redirection to another computer.  I have attached the code for a sample console application with a greatly simplified version of what I am trying to do.  This sample will listen on a local IP/port and forward any data to the remote RDP server.  The problem is, when I run this program and type in the computer's IP (192.168.1.139) and port 4242 into the RDC interface it connects and sends 4 sets of packets between the RDC and RDP server.  However, when it gets to the point where it asks for the credential which I put in and click OK the RDC client then displays "Configuring remote session" until it finally times out.

If I put in the IP address of the destination server (192.168.1.3 in this case) into the RDC and enter the same credentials it works fine.  Thus verifying that my credentials are fine and that my user has permissions on the remote computer to login via RD.

Please don't respond with suggestions of other products that I should use to accomplish this as I am trying to make a product of my own to do this that does not require 3rd party applications.

In my case the client(192.168.1.139) is Win7(x64) using VS2008 and RDP (102.168.1.3) is running Win2008R2(x64)

Anyone know what I need to do to this code to make it properly pass the data from client to server to enable the Remote Desktop session to complete connection and work properly?

Things I've tried:
    - Increasing buffer size
    - Increasing both sockets send/receive buffer size and disabling DontFragment
    - Using complete rework with Async socket interaction (ended with the same lockup in the same location)
    - Recreating the project in VS2010RC and copying the code (no change in results)
    - adding 1 second delays between all socket operations to rule out timing issues (no change in results)
    - Peeking at socket buffer data and only doing reads when there was data waiting (no change in results)
    - Tried putting both Receive calls into loops to continue trying to retrieve data even when they return 0 bytes
    - Tried only allocating one buffer and using it for all calls
using System;
using System.Net;
using System.Net.Sockets;

namespace PortForwardTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int rdpPortNum = 3389;
            int redirPortNum = 4242;
            int BufferSize = 64 * 1024;
            try
            {
                Console.WriteLine("Starting up...");
                IPEndPoint redirEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.139"), redirPortNum); // Local Machine
                IPEndPoint RDPEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.3"), rdpPortNum); // Remote Machine

                Socket rdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Socket redirListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                Console.WriteLine("Listening for connection ...");
                redirListenSocket.Bind(redirEndpoint); 
                redirListenSocket.Listen(1); // Listen on local port

                Socket clientSocket = redirListenSocket.Accept();
                rdpSocket.Connect(RDPEndpoint); // Connect to remote computer on RDP port

                byte[] Buffer = null;
                int rdpSendSize = 0;
                int rdpReceiveSize = 0;
                int redirSendSize = 0;
                int redirReceiveSize = 0;

                while (true)
                {
                    Buffer = new byte[BufferSize];
                    redirReceiveSize = clientSocket.Receive(Buffer, BufferSize, SocketFlags.None);

                    Console.WriteLine("Received {0} bytes from redir port. Sennding to RDP port.", redirReceiveSize);
                    rdpSendSize = rdpSocket.Send(Buffer, redirReceiveSize, SocketFlags.None);
                    Console.WriteLine("Sent {0} bytes to RDP. Waiting for response...", rdpSendSize);

                    Buffer = null;
                    Buffer = new byte[BufferSize];
                    rdpReceiveSize = rdpSocket.Receive(Buffer, BufferSize, SocketFlags.None);

                    Console.WriteLine("Received {0} bytes from RDP. Sending to redir port", rdpReceiveSize);
                    redirSendSize = clientSocket.Send(Buffer, rdpReceiveSize, SocketFlags.None);
                    Console.WriteLine("Sent {0} bytes to redirPort.  Waiting for response...", redirSendSize);
                    Buffer = null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
            Console.ReadKey();
        }
    }
}

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Ted BouskillTed Bouskill🇨🇦

Why aren't you using Windows built in NAT translation and port forwarding?  It's free, works and that is exactly what you are doing.

ASKER CERTIFIED SOLUTION
Avatar of gmaingmain🇺🇸

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

C#

C#

--

Questions

--

Followers

Top Experts

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).