Visual Basic 9 / Visual Studio 2008 IPAddress.Parse() 不起作用?走动

发布于 2024-11-07 02:07:10 字数 1498 浏览 0 评论 0原文

编辑我意外地找到了解决这个问题的方法...在函数的开头添加以下行由于某种原因解决了问题...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-11-14 02:07:10

编辑:

根据编辑后的问题,我的猜测是输入不是“192.168.1.65”,但实际上包含填充。也许就像“192.168.1.65”一样简单。 仔细检查字符串。哎呀,首先检查 .Length,然后逐个字符检查。


基于原始问题的原始答案

我不相信您显示的输出是真实的,部分原因是缺少哈哈!而且,您还对其中一个数字进行了硬编码;当然应该是:

 ipEndPoint = New IPEndPoint(IPAddress.Parse(remoteIPAdress),1003 + topUniqueId)

还要记住本地 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:

 ipEndPoint = New IPEndPoint(IPAddress.Parse(remoteIPAdress),1003 + topUniqueId)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文