Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 />


Thanks so much for any help.

Dwayne.

Edit: I am using VS2005 win 7
Posted
Updated 16-Mar-12 9:12am
v2
Comments
TRK3 16-Mar-12 17:16pm    
It sounds like you are changing several things at once when you are going from a loopback to a multicast and any one of them might be the problem:

1) You are changing from unicast to multicast.
2) You are changing from a local loopbak to over the network.
3) Are you changing ports as well?

There may be firewall and router issues with send a UDP multi-cast over the network.

First, take the code that works (sending a unicast loopback) and change the target address to the other machine (and nothing else) and see if that gets through. If it doesn't, turn off firewalls on both machines temporarily and try again. If that still doesn't work, try another machine that is physically located next to yours connected via a single hub (not a switch or router or anything that might have some intelligence in it).

Assuming you get that working, then go from the directed unicast to the multicast and see what happens.

When you've got it narrowed down to one change that is breaking the "ping" then you can figure out what's wrong with that one thing, or let us know and ask for more help.
Faultyboy 17-Mar-12 13:02pm    
Thank you for the reply, I will do exactly as you say and I will get back to you with the results, so as to eliminate any network related issues. It shouldn't be changing ports, there should be a port for the incoming traffic and one for the outgoing. As far as i can tell this line..

Public MyIP3 As IPAddress = IPAddress.Parse("192.168.1.255")

I don't think it is getting saved properly as an IP address. It displays "Ipaddress.system" if i remember correctly where the ip variable should be.

Anyways I will get back to you in a day or 2.. :) Ta

Peace.
Faultyboy 18-Mar-12 7:55am    
I found the sample I originally used to create the app. Is this sample correct?

http://www.java2s.com/Tutorial/VB/0400__Socket-Network/UdpBroadcast.htm

How would I edit this to send and receive on the same multicast group.

P.s. There where no network issues. Problems are in the code.
Faultyboy 18-Mar-12 8:23am    
Ive gone a little bit further, could anybody please tell me if this is on the right track to sending and receiving multicast udp pings...


Public LocalHost As String = System.Net.Dns.GetHostName
Public MyIP As IPAddress = (System.Net.Dns.Resolve(LocalHost).AddressList(0))
Public MyIP2 As String = Convert.ToString(MyIP)
Public GroupIP As IPAddress = IPAddress.Parse("239.255.255.255")
Public Bytes(2000) As Byte
Public GroupEP As New IPEndPoint(GroupIP, 1001)
Public Send As New UdpClient(GroupEP)
Public Receive As New UdpClient(GroupEP)
Delegate Sub Delegate1(ByVal SntPulseVar As String)
Delegate Sub Delegate2(ByVal RePulseVar As String)
Delegate Sub Delegate3(ByVal DelNodeVar As String)
Public PulsePacket As String = (NodeNumber & " " & LocalHost & " " & MyIP2)
Public PulseFrequency As Int32
Public InPulseStr As String
Public InPulseByt As Object
Public KillThread As Boolean
Public T As Int32
Public NodeNumber As Int32

''Frm Load''

Public Sub FrmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Send.JoinMulticastGroup(GroupIP, 1001)
BtnDis.Enabled = False

End Sub

next is how i can sending and receiving..


Send.Send(Bytes, Bytes.Length, GroupEP)

InPulseByt = Receive.Receive(GroupEP)



Thanks again.
Faultyboy 18-Mar-12 8:42am    
The error is: The requested address is not valid in its context

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900