Swololol
Swololol

Reputation: 111

C# TCP Socket Client refuses connection

Here is my Server

When using multiple different port checking tools on the internet I get connections on my server, demonstrating that my ports are open

http://www.infobyip.com/tcpportchecker.php

http://www.yougetsignal.com/tools/open-ports/

http://ping.eu/port-chk/

http://ping.eu/port-chk/

Multiple successful connections

static void Main(string[] args)
{
    IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 10897);
    Console.WriteLine("Server Start");

    Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    listener.Bind(localEndPoint);
    listener.Listen(10);
    while (true)
    {
        Console.WriteLine("Waiting for a connection...");
        Socket handler = listener.Accept();
        Console.WriteLine(handler.RemoteEndPoint);
        handler.Shutdown(SocketShutdown.Both);
        handler.Close();
    }
}

Here is my client code

 static void Main(string[] args)
 {
     IPEndPoint ip = new IPEndPoint(IPAddress.Parse("MY IP HERE"), 10897);
     Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     server.Connect(ip);
 }

I get this error everytime, I have tried multiple different attempts from the client side using System.Net.Sockets.TcpClient and sockets and everytime I get connection refused so it appears to be some issue with my client since my server receives connections from port checkers Error

I have read through dozens of other socket topics but all of the issues have to do with the server and not the client, I no idea what is wrong can somebody help me?

EDIT: I can connect via localhost (127.0.0.1) but not from my public IP

Upvotes: 1

Views: 8641

Answers (5)

Gregory Gustavin
Gregory Gustavin

Reputation: 11

I decided this problem with using port forwarding on my WiFi router and add rules in Windows Firewall

For port forwarding on my WiFi I used nuget packedge - OpenNAT And code below

static async void UPnP_NAT(int port)
{
try
{
    var discoverer = new NatDiscoverer();
    var cts = new CancellationTokenSource(7000);
    // using SSDP protocol, it discovers NAT device.
    //var device1 = await discoverer.DiscoverDeviceAsync(PortMapper.Pmp, cts);

    var device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts);

    // display the NAT's IP address
    //Console.WriteLine("The external IP Address is: {0} ", await device.GetExternalIPAsync());
    IPAddress x = await device.GetExternalIPAsync();
    Console.WriteLine("Router's WAN:  " + x.ToString());
    // create a new mapping in the router [external_ip:1702 -> host_machine:1602]
    await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "[MY_Server]"));

    foreach (var mapping in await device.GetAllMappingsAsync())
    {
        if (mapping.Description.Contains("[MY_Server]"))
        {
            Console.WriteLine("Mapping on port: xxxxxx" + " - SUCCESSFUL!");
            Console.WriteLine(mapping.ToString());
        }
    }
}
catch (NatDeviceNotFoundException e)
{
    //  Open.NAT wasn't able to find an Upnp device
    Console.WriteLine(e.Message);
}
catch (MappingException me)
{
    switch (me.ErrorCode)
    {
        case 718:
            Console.WriteLine("The external port already in use.");
            break;
        case 728:
            Console.WriteLine("The router's mapping table is full.");
            break;
    }
}
}

For adding rules in Windows Firewall I used this code and added reference to Interop. NetFwTypeLib

class SetFirewall
{
private int[] portsSocket = new int[1];
private string[] portsName = new string[1];
private INetFwProfile fwProfile = null;

public SetFirewall(int portsSocket, string portsName)
{
    this.portsSocket[0] = portsSocket;
    this.portsName[0] = portsName;
}

//[PrincipalPermission(SecurityAction.Demand, Role = @"Administrators")]
protected internal void OpenFirewall()
{
    INetFwAuthorizedApplications authApps = null;
    INetFwAuthorizedApplication authApp = null;
    INetFwOpenPorts openPorts = null;
    INetFwOpenPort openPort = null;

    Assembly assembly = Assembly.GetExecutingAssembly();
    string assemblyLocation = assembly.Location.Replace(".dll", ".exe");
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assemblyLocation);
    var companyName = fvi.CompanyName;
    var productName = fvi.ProductName;
    var productVersion = fvi.ProductVersion;
    try
    {
        if (isAppFound(productName) == false)
        {
            SetProfile();
            authApps = fwProfile.AuthorizedApplications;
            authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication;
            authApp.Name = productName;
            authApp.ProcessImageFileName = assemblyLocation;
            authApps.Add(authApp);
        }

        for (int i = 0; i < portsSocket.Length; i++)
        {
            if (isPortFound(portsSocket[i]) == false)
            {
                SetProfile();
                openPorts = fwProfile.GloballyOpenPorts;
                openPort = GetInstance("INetOpenPort") as INetFwOpenPort;
                openPort.Port = portsSocket[i];
                openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                openPort.Name = portsName[i];
                openPorts.Add(openPort);
            }
        }
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
    }
    finally
    {
        if (authApps != null) authApps = null;
        if (authApp != null) authApp = null;
        if (openPorts != null) openPorts = null;
        if (openPort != null) openPort = null;
    }
}

//[PrincipalPermission(SecurityAction.Demand, Role = @"Administrators")]
protected internal void CloseFirewall()
{
    INetFwAuthorizedApplications apps = null;
    INetFwOpenPorts ports = null;

    Assembly assembly = Assembly.GetExecutingAssembly();
    string assemblyLocation = assembly.Location;
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assemblyLocation);
    var companyName = fvi.CompanyName;
    var productName = fvi.ProductName;
    var productVersion = fvi.ProductVersion;

    try
    {
        if (isAppFound(productName) == true)
        {
            SetProfile();
            apps = fwProfile.AuthorizedApplications;
            apps.Remove(assemblyLocation);
        }

        for (int i = 0; i < portsSocket.Length; i++)
        {
            if (isPortFound(portsSocket[i]) == true)
            {
                SetProfile();
                ports = fwProfile.GloballyOpenPorts;
                ports.Remove(portsSocket[i], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
            }
        }
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
    }
    finally
    {
        if (apps != null) apps = null;
        if (ports != null) ports = null;
    }
}

protected internal bool isAppFound(string appName)
{
    bool boolResult = false;
    Type progID = null;
    INetFwMgr firewall = null;
    INetFwAuthorizedApplications apps = null;
    INetFwAuthorizedApplication app = null;
    try
    {
        progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");
        firewall = Activator.CreateInstance(progID) as INetFwMgr;
        if (firewall.LocalPolicy.CurrentProfile.FirewallEnabled)
        {
            apps = firewall.LocalPolicy.CurrentProfile.AuthorizedApplications;
            IEnumerator appEnumerate = apps.GetEnumerator();
            while ((appEnumerate.MoveNext()))
            {
                app = appEnumerate.Current as INetFwAuthorizedApplication;
                if (app.Name == appName)
                {
                    boolResult = true;
                    break;
                }
            }
        }
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
    }
    finally
    {
        if (progID != null) progID = null;
        if (firewall != null) firewall = null;
        if (apps != null) apps = null;
        if (app != null) app = null;
    }
    return boolResult;
}

protected internal bool isPortFound(int portNumber)
{
    bool boolResult = false;
    INetFwOpenPorts ports = null;
    Type progID = null;
    INetFwMgr firewall = null;
    INetFwOpenPort currentPort = null;
    try
    {
        progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");
        firewall = Activator.CreateInstance(progID) as INetFwMgr;
        ports = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts;
        IEnumerator portEnumerate = ports.GetEnumerator();
        while ((portEnumerate.MoveNext()))
        {
            currentPort = portEnumerate.Current as INetFwOpenPort;
            if (currentPort.Port == portNumber)
            {
                boolResult = true;
                break;
            }
        }
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
    }
    finally
    {
        if (ports != null) ports = null;
        if (progID != null) progID = null;
        if (firewall != null) firewall = null;
        if (currentPort != null) currentPort = null;
    }
    return boolResult;
}

protected internal void SetProfile()
{
    INetFwMgr fwMgr = null;
    INetFwPolicy fwPolicy = null;
    try
    {
        fwMgr = GetInstance("INetFwMgr") as INetFwMgr;
        fwPolicy = fwMgr.LocalPolicy;
        fwProfile = fwPolicy.CurrentProfile;
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
    }
    finally
    {
        if (fwMgr != null) fwMgr = null;
        if (fwPolicy != null) fwPolicy = null;
    }
}

protected internal object GetInstance(string typeName)
{
    Type tpResult = null;
    switch (typeName)
    {
        case "INetFwMgr":
            tpResult = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
            return Activator.CreateInstance(tpResult);
        case "INetAuthApp":
            tpResult = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));
            return Activator.CreateInstance(tpResult);
        case "INetOpenPort":
            tpResult = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}"));
            return Activator.CreateInstance(tpResult);
        default:
            return null;
    }
}
}

Upvotes: 0

geekonweb
geekonweb

Reputation: 394

It seems to be a port forwarding /routing issue. Check whether forwarded port is accessible or not with Port checker

Upvotes: 0

Peuczynski
Peuczynski

Reputation: 4733

It has something to do with Windows Firewall exception. On the first run you should get prompted to allow connection. If you allow it first time the exception is created in the firewall. Maybe you denied it. Also try elevating required priviliges for your application via app.manifest

Upvotes: 0

Swololol
Swololol

Reputation: 111

It appears the issue was my router had some setting that made it so that it wouldn't accept internal connections to the external ip, I had my friend run the client code and it connected fine

Upvotes: 2

matt-dot-net
matt-dot-net

Reputation: 4244

The code works fine (the first two lines of your server do nothing). You have a firewall or routing issue.

Upvotes: 3

Related Questions