المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : UDP/TCP Port Forwarding [modified]



C# Programming
11-03-2009, 01:50 PM
Hello all.

I'm feeling pretty confused about how the port forwarding is done. My situation is: I have Comp1 with a local IP (192.168.0.2) connected to Comp2 with external IP and internet connection, and some unknown Internet Client (IC), who needs to send data to the Server hosted at Comp1 through UDP port 7777 and then receive a response.

I managed to forward UDP data from Comp2 to Comp1 by simply accepting IC's packets at Comp2's port 7777 and sending them to Comp1's port 7777, but the problem is that Comp1's Server sees sender as Comp2 and sends response to it (192.168.0.1), rather than to IC.

So how do I forward IC's data from Comp2 to Comp1 with Comp1's Server knowing, that response must be sent to IC? I would also like to see the way it's done with TCP.



Here's what I managed to do:


namespace PortForwarder
{
class Program
{
public static UdpClient UDP1 = new UdpClient(7777);

static void Main(string[] args)
{
Console.Title = "Port Forwarder";
Console.WriteLine("-= Port Forwarder started. =-");
Console.WriteLine("UDP port 7777 forwarded");

UDP1.BeginReceive(ReceiveDataUDP1, null);

while (true) { };
}

static void ReceiveDataUDP1(IAsyncResult ar)
{
IPEndPoint IEP = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = UDP1.EndReceive(ar, ref IEP);

// Trying to "lie" about local IEP results in exception
// UdpClient US1 = new UdpClient(IEP);
// US1.Send(receiveBytes, receiveBytes.Length, "192.168.0.2", 7777);

UDP1.Send(receiveBytes, receiveBytes.Length, "192.168.0.2", 7777);
UDP1.BeginReceive(ReceiveDataUDP1, null);
}
}
}




P.S. If it wasn't obvious enough, Comp1 is connected to the internet through Comp2's ICS (Internet Connection Sharing). Comp2 is running Windows Server 2008 and connects to the internet through VPN connection. I tried to set up NAT there, but VPN connection cannot be shared for some reason (and sharing public adapter doesn't help). If, by any chance, anybody knows how it's configured, I would be glad. http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

modified on Monday, November 2, 2009 11:37 AM