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.