0

I'm trying to make a little simple server I can use between college and home. The server works fine, clients can connect. But I want to be able to connect from outside my local network. I'm trying to find a way to display the IP address of the machine required for connecting to it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace SocketProgramming
{
    class Program
    {

    private static byte[] _buffer = new byte[1024];
    private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private static List<Socket> _clientSockets = new List<Socket>();
    private static List<string> _nicknames = new List<string>();

    static void Main(string[] args)
    {

        SetupServer();
        Console.ReadLine();

    }

    private static void SetupServer()
    {

        Console.Write("Enter a port number to establish the server on: ");
        int portNum = Int32.Parse(Console.ReadLine());
        _serverSocket.Bind(new IPEndPoint(IPAddress.Any, portNum));
        _serverSocket.Listen(5);

        _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);

    }
}
}

How do I get the IP address of the local machine? I want to be able to run this code on any machine, so using a static IP in the code won't help.

  • Possible duplicate of [Get local IP address](http://stackoverflow.com/questions/6803073/get-local-ip-address) – Thomas Weller Feb 07 '17 at 19:17
  • I'm guessing that your 'server' will be in your home network and your client will be running outside this network. In this case you want to know the IP Address of the server, which would be the public IP address of your home network. This value is not static, unless you use some service ($$) to obtain a static IP address for your home. – Callback Kid Feb 07 '17 at 19:40
  • @ThomasWeller That one fixed it. Thanks bud! – Adam Robero Feb 07 '17 at 19:52

2 Answers2

1

This post was extremely helpful for me when trying to tackle the same issue. My implementing code wound up using this answer with a slight modification to specifying exactly what Protocol type I wanted.

string localIP;

        try
        {
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP))
            {
                socket.Connect("8.8.8.8", 65530);
                IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                localIP = endPoint.Address.ToString();
            }
        }
        catch (Exception ex)
        {
            localIP = String.Empty;
        }

        return localIP;
Community
  • 1
  • 1
Kaiser12
  • 631
  • 4
  • 4
1

It is worth noting that a machine may have more than one IP address. You can get a quick list using the 'ipconfig' command from a dos or powershell cmd line. You can do something similar in c# like this:

        foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) {
            Console.WriteLine("Name: " + netInterface.Name);
            Console.WriteLine("Description: " + netInterface.Description);
            Console.WriteLine("Addresses: ");
            IPInterfaceProperties ipProps = netInterface.GetIPProperties();
            foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) {
                Console.WriteLine(" " + addr.Address.ToString());
                }
            Console.WriteLine("");
            }

see: Get All IP Addresses On Machine

Additionally, if trying to reach a remote machine you could run into firewall/router issues. If your host ip is a non-routing address (like 10.*.*.*, or 192.168.*.*, etc.) you will not be able to reach it from outside of that network.

Community
  • 1
  • 1
Dweeberly
  • 4,668
  • 2
  • 22
  • 41