I read something about using UDP to open ports. I can't find the original page I read but I did find this SO answer https://stackoverflow.com/a/1539394
I tried running this code. Perhaps I did something wrong? The idea (in link above) was Alice listens to port 5412, sends a UDP packet to bob from 5412 (the tcp port) to 5411. Bob (who doesn't listen) uses TCP port 5411 (the udp port) to connect to Alice 5412. I use the command line on bob to give alice IP address.
Did I do this wrong? When I run locally using my public IP address (and my network address but not 127.0.0.1) I get the exception A socket operation was attempted to an unreachable network
. When I run it on Bob I get a connection timeout exception.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace TcpTest
{
class Program
{
static string localIp = "127.0.0.1";
static string remoteIP = ipaddr;
static void Main(string[] args)
{
//run remotely to connect to you using TCP
if (args.Count() > 0)
{
var t = new TcpClient(new IPEndPoint(IPAddress.Parse(localIp), 5411)); //Force port
t.Connect(args[0], 5412);
return;
}
//Run locally
//Bind TCP port
var l = new TcpListener(5412);
l.Start();
//Send UDP using the listening port to remote address/port
var u = new UdpClient(5412);
u.Connect(remoteIP, 5411);
var buf = new byte[10];
u.Send(buf, buf.Length);
//R
new Thread(SimulateRemote).Start();
//L
var c = l.AcceptTcpClient();
var af=c.Client.RemoteEndPoint;
}
static void SimulateRemote()
{
Thread.Sleep(500);
var t = new TcpClient(new IPEndPoint(IPAddress.Parse(localIp), 5411)); //Force port
t.Connect(myipaddr, 5412);
}
}
}