Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse IPAddress on C# with Framework 4.6

Tags:

c#

How is the correct way to Parse an IPAddress from String to System.Net.IPAddress on C# with Framework 4.6.

I am trying to implement with Visual Studio 2017 NuGet TCPSimple a TCP/IP Server and Client for testing purposes, but I got a System.FormatException "Input string was not in a correct format".

I have tried different approaches, but they're not working at all!

    private void btnStart_Click(object sender, EventArgs e)
    {
        txtStatus.Text += "Server starting...";

        //var address = System.Net.IPAddress.Parse("10.0.11.174").GetAddressBytes();
        //long m_Address = ((address[3] << 24 | address[2] << 16 | address[1] << 8 | address[0]) & 0x0FFFFFFFF);
        System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(txtHost.Text));
        //System.Net.IPAddress ip = new System.Net.IPAddress.Parse("127.0.0.1");
        server.Start(ip,Convert.ToInt32(txtPort.Text));
    }

enter image description here

like image 415
JWBG Avatar asked Dec 24 '22 06:12

JWBG


1 Answers

There is an IPAddress.Parse method in the .NET framework to handle this.

IPAddress ip = IPAddress.Parse(txtHost.Text);
like image 169
Owen Pauling Avatar answered Dec 25 '22 21:12

Owen Pauling