0

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);
        }
    }
}
1
  • UDP traffic is sent from a UDP port to a UDP port. TCP traffic is sent from a TCP port to a TCP port. There are no exceptions to that (per protocol specifications). A device, such as a router, may be able to convert TCP traffic to and from UDP, but doing that is not common, because TCP is basically UDP + some more data. If TCP's extra overhead is used, throwing away the data provides rather little benefit. TCP port numbers range from 0 to 65535 and UDP port numbers range from 0 to 65535. UDP port 5411 and TCP port 5411 are two different ports, despite having the same number.
    – TOOGAM
    Commented Jul 17, 2016 at 12:38

1 Answer 1

5

You can't do that.

TCP ports and UDP ports can't connect to each other. The packets you send from UdpClient.Send() have the Protocol field set to 0x11, the value for UDP, but the TCP stack only recognizes packets with Protocol set to 0x06, the value for TCP. It's part of the most-basic operation of an IP module. See RFC 791 if you want the details.

5
  • That sounds right but I saw at least two post saying I CAN do that. Not connect but have the firewall let through TCP connections when I do that with UDP. Any ideas how I can poke a hole through a firewall/router?
    – user34537
    Commented Mar 13, 2014 at 22:36
  • @acidzombie24 Either the posts are wrong or you misunderstood them. Doing this for UDP connections is common. TCP is different. And any firewall will recognize and separate TCP and UDP port-spaces. Commented Mar 13, 2014 at 22:38
  • The SO answer i linked happen to have this link which says "Astonishingly, hole punching also works with TCP. After an outgoing SYN packet the firewall / NAT router will forward incoming packets with suitable IP addresses and ports to the LAN even if they fail to confirm, or confirm the wrong sequence number (ACK)." The first page talks about the steps I used.
    – user34537
    Commented Mar 13, 2014 at 23:03
  • There's a big difference between a TCP packet with a SYN and a UDP packet. No firewall is going to mistake them for each other. Commented Mar 14, 2014 at 2:38
  • So the link is just plain wrong? That kind of sucks. Heres hoping theres TCP hole punching
    – user34537
    Commented Mar 14, 2014 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.