Visual Basic 9 / Visual Studio 2008 IPAddress.Parse() 不起作用?走动
编辑我意外地找到了解决这个问题的方法...在函数的开头添加以下行由于某种原因解决了问题...
remoteIPAdress = remoteIPAdress & "END"
您好。 我有一个学校作业,要在 Visual Basic 中构建一个简单的 TCP/IP 信使... 问题是,当客户端在我构建的请求中发送他的IP(“LetMeIn\XXX.XXX.XXX.XXX”)时,即使服务器按其应有的方式接收到请求,它也会完全错误地解析它......
更准确地说,当我运行此代码片段时,我得到以下结果:
Private Function findFreeIPEndPoint(ByVal remoteIPAdress As String) As IPEndPoint
Dim ipEndPoint As IPEndPoint
System.Diagnostics.Debug.Write("LOL! The IP adress you try to parse is " & remoteIPAdress)
System.Diagnostics.Debug.WriteLine("The parsed result is " & String.Concat(IPAddress.Parse(remoteIPAdress)))
ipEndPoint = New IPEndPoint(IPAddress.Parse(remoteIPAdress), 1003 + topUniqueId)
MessageBox.Show(String.Concat(ipEndPoint.Address))
System.Diagnostics.Debug.Write("The IP adress you got is " & String.Concat(ipEndPoint.Address))
Try
listener(ipEndPoint.Port - 1003).Start()
Catch ex As Exception
End Try
topUniqueId = topUniqueId + 1
Return ipEndPoint
End Function
输出:
哈哈!您尝试解析的 IP 地址是 192.168.1.65
System.dll 中发生类型“System.FormatException”的第一次机会异常
如果我将以下行更改
ipEndPoint = New IPEndPoint(IPAddress.Parse(remoteIPAdress), 1003 + topUniqueId)
为
ipEndPoint = New IPEndPoint("192.168.1.65", 1003 + topUniqueId)
我得到:
The IP adress you got is 229.64.116.11
奇怪吧?
EDIT I accidentaly found a walk around to this problem... Adding the following line in the begining of the function solved the problem for some reason...
remoteIPAdress = remoteIPAdress & "END"
Hello.
I have a school assignment where I am supposed to build a simple TCP/IP messenger in Visual Basic...
The problem is that when the client sends his IP in a request I've built ("LetMeIn\XXX.XXX.XXX.XXX"), even if the server receives the request as it should, it parses it completely wrong...
To be more exact, when I run this snippet I get these results:
Private Function findFreeIPEndPoint(ByVal remoteIPAdress As String) As IPEndPoint
Dim ipEndPoint As IPEndPoint
System.Diagnostics.Debug.Write("LOL! The IP adress you try to parse is " & remoteIPAdress)
System.Diagnostics.Debug.WriteLine("The parsed result is " & String.Concat(IPAddress.Parse(remoteIPAdress)))
ipEndPoint = New IPEndPoint(IPAddress.Parse(remoteIPAdress), 1003 + topUniqueId)
MessageBox.Show(String.Concat(ipEndPoint.Address))
System.Diagnostics.Debug.Write("The IP adress you got is " & String.Concat(ipEndPoint.Address))
Try
listener(ipEndPoint.Port - 1003).Start()
Catch ex As Exception
End Try
topUniqueId = topUniqueId + 1
Return ipEndPoint
End Function
Output:
LOL! The IP adress you try to parse is 192.168.1.65
A first chance exception of type 'System.FormatException' occurred in System.dll
If i change the following line
ipEndPoint = New IPEndPoint(IPAddress.Parse(remoteIPAdress), 1003 + topUniqueId)
to
ipEndPoint = New IPEndPoint("192.168.1.65", 1003 + topUniqueId)
I get this:
The IP adress you got is 229.64.116.11
Weird right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
根据编辑后的问题,我的猜测是输入不是“192.168.1.65”,但实际上包含填充。也许就像
“192.168.1.65”
一样简单。 仔细检查字符串。哎呀,首先检查.Length
,然后逐个字符检查。基于原始问题的原始答案
我不相信您显示的输出是真实的,部分原因是缺少哈哈!而且,您还对其中一个数字进行了硬编码;当然应该是:
还要记住本地 IP 和公共 IP 之间的区别。 192.168 IP 是您的 LAN。远程客户端不会有这个,但将使用通过其路由器/代理/其他方式转换的公共IP地址。
Edit:
With the edited question, my guess is that the input is not
"192.168.1.65
", but actually contains padding. Perhaps as simple as"192.168.1.65 "
. Check the string carefully. Heck, check the.Length
first, then character by character.Original answer based on the original question
I don't believe the output you are displaying is true, in part because of the missing LOL! But also, you have one of the numbers hard-coded; surely it should be:
Also keep in mind the differnce between a local IP and a public IP. A 192.168 IP is your LAN. Remote clients won't have that, but will be using a public IP address translated via their router/proxy/whatever.