无法获得udp组播发送 [英] Trouble getting udp multicast to send
问题描述
你好,
我想知道是否有人会介意看一下我的代码,并指出我做错了什么来帮助我.我正在尝试在同一应用程序中发送和接收多播.当我设计应用程序时,我希望它能够在环回中发送和接收良好,因此大部分程序代码都可以正常工作,就在我尝试从发送环回切换为以多播形式发送时.每当我认为我可能无法将正确的ip和端口信息发送到正确的位置时,它都会失败..由于我不是最熟练的程序员,并且我一直在努力让该应用程序查看另一个实例,因此我将不胜感激.由于缺乏更好的用语,最近两天它在网络上使用多播udp"ping"..Google对这一点有所帮助,但在此问题上没有任何成功的代码提供了很多帮助..
Hi there,
I was wondering if someone wouldn''t mind looking at a bit of my code and help me by pointing out what I''m doing wrong. I am trying to send and receive a multicast in the same application. When i was designing the app I got it to send and receive fine on the loopback so the bulk of the programs code works fine, it''s just when i tried to change over from sending a loopback to sending it as a multicast.. it fails every time I think i might not be getting the correct ip and port info to the right place.. some insight would be greatly appreciated as I am not the most proficient programmer and I have been struggling to get the app to see another instance of itself on the network with a multicast udp "ping" for lack of a better term, for the last 2 days.. Google has been helpful to a point but not much luck with any successful code on this problem..
<br />
Public LocalHost As String = System.Net.Dns.GetHostName<br />
Public MyIP As IPAddress = (System.Net.Dns.Resolve(LocalHost).AddressList(0))<br />
Public MyIP2 As String = Convert.ToString(MyIP)<br />
Public MyIP3 As IPAddress = IPAddress.Parse("192.168.1.255")<br />
Public MyIP4 As IPAddress = IPAddress.Any<br />
Public Bytes(2000) As Byte<br />
Public Port1 As Integer = (2001)<br />
Public Port2 As Integer = (2002)<br />
Public MyLocal As New IPEndPoint(MyIP3, Port1)<br />
Public MyRemote As New IPEndPoint(MyIP4, Port2)<br />
Public Send As New UdpClient(MyLocal)<br />
Public Receive As New UdpClient(MyRemote)<br />
Public Socket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)<br />
Delegate Sub Delegate1(ByVal SntPulseVar As String)<br />
Delegate Sub Delegate2(ByVal RePulseVar As String)<br />
Delegate Sub Delegate3(ByVal DelNodeVar As String)<br />
Public PulsePacket As String = (NodeNumber & " " & LocalHost & " " & MyIP2)<br />
Public PulsePacket2 As String = ("1 Shelly 192.168.0.2 Test Packet")<br />
Public PulseFrequency As Int32<br />
Public InPulseStr As String<br />
Public InPulseByt As Object<br />
Public KillThread As Boolean<br />
Public T As Int32<br />
Public NodeNumber As Int32<br />
<br />
Public Sub FrmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
<br />
Socket.Bind(MyLocal)<br />
<br />
Receive.JoinMulticastGroup(MyIP3, 12)<br />
<br />
BtnDis.Enabled = False<br />
<br />
End Sub<br />
非常感谢您的帮助.
德韦恩.
我正在使用VS2005 win 7
Thanks so much for any help.
Dwayne.
I am using VS2005 win 7
推荐答案
好吧,我明白了... :)对于任何有兴趣的人,这是发送和接收UDP广播的代码从同一应用程序..您必须指定用于发送和接收的源端口和目标端口..
Ok I got it... :) For anyone who is interested this is the code to send and receive a UDP broadcast from the same app.. you have to specify the source port and the destination port for sending and receiving..
<br />
Public EndPoint As New IPEndPoint(IPAddress.Parse("255.255.255.255"), 1001) ''''Send destination port<br />
Public EndPoint2 As New IPEndPoint(IPAddress.Any, 2001) ''''Receive destination port<br />
Public Bytes(2000) As Byte<br />
Public Send As New UdpClient(2001) ''''Send source port<br />
Public Receive As New UdpClient(1001) '''' Receive source port<br />
发送和接收我用过的...
to send and receive i used this...
<br />
Send.Send(Bytes, Bytes.Length, EndPoint) <br />
InPulseByt = Receive.Receive(EndPoint2)<br />
就像魅力一样...希望对其他人有帮助.
和平.
Works like a charm... Hope this helps anyone else.
Peace.
这篇关于无法获得udp组播发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!