【问题标题】:UDPClient.Send to a specific IP/Port is not sending, nor is it throwing an exceptionUDPClient.Send 到特定的 IP/Port 没有发送,也没有抛出异常
【发布时间】:2021-08-12 22:34:09
【问题描述】:

我正在尝试将一个小型 python 脚本的功能提取到 VB 应用程序中。该应用程序正在测试与自定义 PCBA 的 UDP 通信。 PCBA 将向 App 发送特定消息,App 应接收该消息并将其回显,从而完成测试。我想将此功能迁移到 VB 中,这样我就没有 python 脚本的 shell,它可以工作,但不那么容易分发。

问题在于,虽然 python 代码按预期工作,但 VB 代码没有回显。它不会抛出异常,它会接收初始消息,检查消息中是否存在“Loopback”一词,并尝试通过将接收到的字节发回来做出响应。但是,没有发送字节。为什么?

有效的python脚本是:

import socket
import sys
import string

lb = "Loopback"
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('192.168.1.99', 10000)
sock.bind(server_address)
while True:
    print('\nWaiting to receive message')
    data, address = sock.recvfrom(512)
    print('>>> %s' % data)
    if lb in data.decode():
        #if lb in data:
        sock.sendto(data, address)
        print('Sent %d bytes back to %s' % (len(data), address))

尝试在 Visual Basic 中实现相同的功能(不起作用)是:

Dim udpClient As New UdpClient(10000)
        Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Parse("192.168.1.99"), 10000)
        Dim receiveBytes As Byte()
        Dim returnData As String

        While True
            receiveBytes = udpClient.Receive(RemoteIpEndPoint)
            returnData = Encoding.ASCII.GetString(receiveBytes)

            Console.WriteLine(RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString())
            Try
                If returnData.ToString().Contains("Loopback") Then
                    udpClient.Connect(RemoteIpEndPoint)
                    Dim reply As Integer = udpClient.Send(receiveBytes, receiveBytes.Length, RemoteIpEndPoint)
                    Console.WriteLine("Replied:" + reply.ToString() + " to : " + RemoteIpEndPoint.Address.ToString() + ", Port: " + RemoteIpEndPoint.Port.ToString())
                End If
            Catch ex As SocketException
                Console.WriteLine(ex.ErrorCode.ToString())
            End Try
        End While

当我在运行 VB Echo 代码时通过 WireShark 监控通信时,我可以看到接收到的数据包,但没有像 Python Echo 代码那样回显。

任何帮助将不胜感激!

【问题讨论】:

    标签: python .net vb.net udpclient


    【解决方案1】:

    我不知道发送部分,所以我不知道这是否是正确的答案。

    1. 检查你的防火墙

    2. 删除 udpClient.Connect(RemoteIpEndPoint) 源部分

    Dim udpClient As New UdpClient(10000)
    Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Parse("192.168.1.99"), 10000)
    Dim receiveBytes As Byte()
    Dim returnData As String
    
    receiveBytes = udpClient.Receive(RemoteIpEndPoint)
    returnData = Encoding.ASCII.GetString(receiveBytes)
    
          While True
                receiveBytes = udpClient.Receive(RemoteIpEndPoint)
                returnData = Encoding.ASCII.GetString(receiveBytes)
    
                Console.WriteLine(RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString())
                Try
                   If returnData.ToString().Contains("Loopback") Then
                      ' udpClient.Connect(RemoteIpEndPoint)
                      Dim reply As Integer = udpClient.Send(receiveBytes, receiveBytes.Length, RemoteIpEndPoint)
                      Console.WriteLine("Replied:" + reply.ToString() + " to : " + RemoteIpEndPoint.Address.ToString() + ", Port: " + RemoteIpEndPoint.Port.ToString())
                   End If
                Catch ex As SocketException
                   Console.WriteLine(ex.ErrorCode.ToString())
                End Try
             End While
    

    【讨论】:

    • 感谢您的建议。不幸的是,没有防火墙,没有 udpClient.Connect,它仍然不会抛出异常,但也不会发送。不过谢谢你,Think2826
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2015-07-11
    • 2018-07-20
    • 2017-02-28
    相关资源
    最近更新 更多