Reputation: 67
I am doing some socket testing. I am trying to connect a client to the external/public ip address of the server. Unfortunately, although I have found an open port, the socket does not respond to its external/public ip. Here is the server code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace PortScannerServer
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Server";
while (true)
{
try
{
_server.Bind(new IPEndPoint(IPAddress.Any, 0));
_server.Listen(10);
Console.WriteLine(_server.LocalEndPoint.ToString());
Console.WriteLine(GetExternalAddress().ToString());
_server.Accept();
Console.WriteLine("Connected");
break;
}
catch (Exception ex)
{
Console.WriteLine(ex.Source + ":" + ex.Message + ":" + ex.InnerException);
}
}
}
static IPAddress GetExternalAddress()
{
var html = new WebClient().DownloadString("http://checkip.dyndns.com/");
var ipStart = html.IndexOf(": ", StringComparison.OrdinalIgnoreCase) + 2;
return IPAddress.Parse(html.Substring(ipStart, html.IndexOf("</", ipStart, StringComparison.OrdinalIgnoreCase) - ipStart));
}
static Socket _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
}
Here is the client code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PortScanner
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Client";
Console.WriteLine("Enter host ip");
_ip = IPAddress.Parse(Console.ReadLine());
Console.WriteLine("Enter host port");
_port = Convert.ToInt32(Console.ReadLine());
while (true)
{
try
{
_client.Connect(new IPEndPoint(_ip, _port));
Console.WriteLine("Connected!");
Console.ReadLine();
break;
}
catch
{
Console.WriteLine("Could not connect to client");
}
}
}
static IPAddress _ip;
static int _port;
static Socket _client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
}
I can get the client to connect if I set the ip address to the local ip (127.0.0.1 or 192.168.1.1), however, it doesn't connect when I set the ip address to the external/public ip. Does anyone know how to fix this?
Upvotes: 1
Views: 1575
Reputation: 127583
From your description it appears that the server you are trying to connect to is on the same internal network as the client (the fact you said 127.0.0.1
works makes me think it might even be the same machine).
To support navigating to a public address and then being forwarded to a internal address in the originating network your NAT router needs to support a feature called "Hairpinning" or "NAT Loopback". Many consumer SOHO routers do not support this feature and the ones that do do not frequently label in their documentation if they support the feature or not.
You will need to either upgrade/replace the firmware on your router to one that supports hairpinning or replace the router entirely with one that does to be able to get your public IP to be redirected to a computer on your internal network behind the router.
There is nothing you can do in your code to fix this other than to not use the public IP and use the internal IP instead.
Upvotes: 2