Patrick
Patrick

Reputation: 420

Parse IPAddress from text file (loaded into string)

I'm trying to get my client app to connect to a server on a remote machine operating on my local network without knowing the server's IP Address to begin with.

Ideally I'd like the client to connect automatically, but since I've been having trouble getting that to work properly, I thought for now I'd have the user manually input their server's IP Address into the client.

I've saved the inputted IP address to a file and loaded it into a string:

string ServerIP = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"/ServerIP.cfg");

but I'm not sure how to set that string as the IPEndPoint:

private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("Insert ServerIP string here"), 8888);

I've unsuccessfully tried using ipString, IPAddress.Parse(ServerIP, 8888);, IPAddress.Parse(string ServerIP, 8888); and multiple other combinations, I still can't figure out the proper syntax for this method, and my Google-fu has failed me on this one.


EDIT: With this implementation I don't get any errors until I try to debug, and I get:

"An unhandled exception of type 'System.ArgumentNullException' occurred in System.dll

Additional information: Value cannot be null." on Client.Connect(serverEndPoint);

    private IPEndPoint serverEndPoint;
    private TcpClient Client = new TcpClient();

    public Client()
    {
        InitializeComponent();
        Client.Connect(serverEndPoint);
        string ServerIP = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"/ServerIP.cfg");
        serverEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), 8888);

    }

Upvotes: 1

Views: 843

Answers (1)

Cyral
Cyral

Reputation: 14153

IPAddress.Parse only takes one argument, I believe you meant to put the port in the IPEndPoint constructor and not the Parse method.

private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), 8888);

Upvotes: 1

Related Questions