Port in IPAddress

2019-06-13 18:40发布

I get a string ,representing ip address .It has format ip:port I need to generate IPAddress .Will use for that :

public static IPAddress Parse(
    string ipString
)

The IPAddress which I need to get should not contain the data regarding a port. Does parse support that? If not how is it possible to done?

标签: c# ip-address
2条回答
劳资没心,怎么记你
2楼-- · 2019-06-13 19:03

Assuming there's there's always a port and it's always represented by :<port number>, you can just unconditionally remove the part after the last colon:

int portStart = ipString.LastIndexOf(':');
ipString = ipString.Substring(0, portStart);

I would expect that to work with IPv6 as well, unless your IPv6 "address and port" format doesn't use a colon - as is feasible with RFC5952. Basically to do this well, you'll need to have more of an idea of the format you'll be receiving.

查看更多
来,给爷笑一个
3楼-- · 2019-06-13 19:15

Well you need to absolutely know whether you are ALWAYS getting a port along with your IP or not. If you are always getting a port then you can always use Jon's code and get rid of the part after the last colon.

The ipString then you can pass on to the IPAddress.TryParse function to get the IPAddress object.

The IPAddress.Parse function will give you an exception if the ipString for whatever reason is not in a correct format.

Now why do you have to absolutely know about the existence of the port in the string. Let me give you an example:

::213 - An absolutely valid IPV6 address. But it has two different meanings:

If you already know there must be a port then this translates to 0:0:0:0:0:0:0:0:213.

But if you do not know then this can also mean 0:0:0:0:0:0:0:213. Note there is one less segment as compared to the above one. A valid IPV6 always has 8 segments, the example i am giving is the shorthand notation for a IPV6 address.

Let's say you do not know whether you would be getting a port in your string or not. Then you MUST always assume that IPV6 addresses would be in the long notation. In which case you can check for the count of colons present like so (this is only for IPV6): (Very crude example)

 int colonCount = ipV6String.Count(c => c == ':');
 int dotCount = ipV6String.Count(c=> c== '.');
 if (((colonCount == 7) && (dotCount == 3)) ||
                 ((colonCount == 8) && (dotCount == 0)))
 {
    //Port is present in the string
    //Extract port using LastIndexOf
 }
 else
 {
    //Port NOT present
 }

To make this workable for IPV4 just check for 3 dots and 1 colon as well

Some examples of valid IPAddresses (without port information)

IPV4

x.x.x.x

IPV6

x:x:x:x:x:x:x:x
x:x:x:x:x:x:d.d.d.d
: : (short hand notation meaning all the segments are 0s)
x: x: : (means that the last six segments are 0s)
: : x : x (means that the first six segments are 0s)
x : x: : x : x (means the middle four segments are 0s)

Please check these following links as well for information about IPAddress formats:

http://publib.boulder.ibm.com/infocenter/dsichelp/ds8000ic/index.jsp?topic=%2Fcom.ibm.storage.ssic.help.doc%2Ff2c_internetprotocol_3st92x.html

http://publib.boulder.ibm.com/infocenter/ts3500tl/v1r0/index.jsp?topic=%2Fcom.ibm.storage.ts3500.doc%2Fopg_3584_IPv4_IPv6_addresses.html

查看更多
登录 后发表回答