IPAddress中的端口 [英] Port in IPAddress
问题描述
我得到一个字符串,代表IP地址.它的格式为ip:port 我需要生成IPAddress,将用于此:
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
)
我需要获取的IPAddress不应包含有关端口的数据.
parse
支持吗?如果没有,怎么办?
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?
推荐答案
好吧,您需要绝对知道您是否总是与您的IP
一起获得端口.如果您总是要获取端口,则可以始终使用 Jon's 代码并删除最后一个冒号之后的部分.
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.
ipString
,然后您可以传递给IPAddress.TryParse
函数以获取IPAddress对象.
The ipString
then you can pass on to the IPAddress.TryParse
function to get the IPAddress object.
如果ipString
由于某种原因格式不正确,IPAddress.Parse
函数将为您提供一个例外.
The IPAddress.Parse
function will give you an exception if the ipString
for whatever reason is not in a correct format.
现在,您为什么必须完全了解字符串中port
的存在.
让我举一个例子:
Now why do you have to absolutely know about the existence of the port
in the string.
Let me give you an example:
::213
-绝对有效的IPV6地址.但这有两种不同的含义:
::213
- An absolutely valid IPV6 address. But it has two different meanings:
如果您已经知道必须有一个端口,那么它将转换为0:0:0:0:0:0:0:0:213
.
If you already know there must be a port then this translates to 0:0:0:0:0:0:0:0:213
.
但是,如果您不知道,那也可能意味着0:0:0:0:0:0:0:213
.
请注意,与上述细分相比,细分少了一个.有效的IPV6始终有8个网段,我给出的示例是IPV6地址的简写形式.
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.
假设您不知道,您是否会在字符串中获取端口.然后,您必须始终假定IPV6地址在long notation
中.
在这种情况下,您可以像这样检查冒号的数量(这仅适用于IPV6):(非常粗略的示例)
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
}
要使其适用于IPV4
,只需检查3 dots
和1 colon
To make this workable for IPV4
just check for 3 dots
and 1 colon
as well
有效IPAddresses
的一些示例(无端口信息)
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)
请同时检查以下链接,以获取有关IPAddress格式的信息:
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
这篇关于IPAddress中的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!