UDPClient接收问题...... [英] UDPClient Receive problem...
问题描述
您好,
我无法使用UdpClient套接字向本地PC上的端口发送数据或从本地PC上的端口接收数据。代码如下,在问题点发表评论(预先为任何信息提供thabnks):
Private Sub TestUDP()
Hello,
I'm having trouble using a UdpClient socket to send and receive data to/from a port on my local PC. The code is as follows, with comments at the problem spot (and thabnks in advance for any info):
Private Sub TestUDP()
'正确设置。
Const MY_LOCAL_IP_ADDRESS As String =" 192.168.1.203"
'Set appropriately.
Const MY_LOCAL_IP_ADDRESS As String = "192.168.1.203"
Dim udpSocket As System.Net.Sockets.UdpClient
Dim b()As Byte
Dim remoteEndPoint As System.Net.IPEndPoint = Nothing
Dim udpSocket As System.Net.Sockets.UdpClient
Dim b() As Byte
Dim remoteEndPoint As System.Net.IPEndPoint = Nothing
'创建udp客户端套接字。允许3秒读取响应。
udpSocket = New System.Net.Sockets.UdpClient
udpSocket.Client.ReceiveTimeout = 3000
udpSocket.Client.Bind(New System.Net.IPEndPoint(System.Net。 IPAddress.Parse(" 0.0.0.0"),0))
'Create udp client socket. Allow 3 second response for reads.
udpSocket = New System.Net.Sockets.UdpClient
udpSocket.Client.ReceiveTimeout = 3000
udpSocket.Client.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), 0))
'要发送的数据。
b = System.Text.Encoding.ASCII.GetBytes(" Whatever你想在这里""
'Data to send.
b = System.Text.Encoding.ASCII.GetBytes("Whatever you want here")
'发送到在这台PC上运行的另一个应用程序,在端口1757上侦听数据。它被写入以便回送数据。
udpSocket.Send( b,b.Length,New System.Net.IPEndPoint(System.Net.IPAddress.Parse(MY_LOCAL_IP_ADDRESS),1757))
'Send to another app running on this PC, listening for data on port 1757. It is written so that it echos data back.
udpSocket.Send(b, b.Length, New System.Net.IPEndPoint(System.Net.IPAddress.Parse(MY_LOCAL_IP_ADDRESS), 1757))
试试
b = udpSocket.Receive(remoteEndPoint)
Try
b = udpSocket.Receive(remoteEndPoint)
'上面的线路:
'System.Net.Sockets.SocketException:远程主机强行关闭现有连接。
''但是......如果你将'MY_LOCAL_IP_ADDRESS的最不重要的八位字节改为255,或者局域网上的某些其他非本地IP,一切都很好。它只是不喜欢localhost IP。
'THE ABOVE LINE THROWS:
'System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.
'
' But...if you change the least significant octet of
'MY_LOCAL_IP_ADDRESS to 255, or some other non-local IP on the
'LAN, all is well. It just doesn't like the localhost IP.
Console.WriteLine(System.Text.Encoding.ASCII.GetString(b,0,b.Length))
Catch ex As System.Net.Sockets.SocketException
Console.WriteLine(ex.ToString)
End Try
Console.WriteLine(System.Text.Encoding.ASCII.GetString(b, 0, b.Length))
Catch ex As System.Net.Sockets.SocketException
Console.WriteLine(ex.ToString)
End Try
End Sub
推荐答案
也许你有防火墙阻止你进行udp连接
maybe you have a firewall preventing you to make an udp connection
这篇关于UDPClient接收问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!