UDP Loopback adapter for testing?

TyB

Well-known member
Joined
May 14, 2009
Messages
102
Programming Experience
3-5
I have a program that I wrote to send messages to PC on the intranet. It currently uses TCP which works fine. I realized that when I want to send a message to many PC that this can take awhile so I decided to use UDP broadcast in those instances.

I'm trying to add this function but I seem not be able to test it. In the TCP side of communicating I can set the IP to 127.0.0.1 and it works fine, but nothing on the UDP side.

Are you not able to use loopback with UDP for testing?

Thanks,

Ty
 
Also

In TCP I do not have to set an IP on the receiving app just the port it is listening on, but the UDP example I used shows that you do on UDP. Is that right?

That might explain the issue because I'm trying to both listen and send on 127.0.0.1 at the same time.

Ty
 
With a UdpClient you specify IPAddress.Broadcast and the port to send to or receive from. There is no problem using two UdpClient instances in same application, where one send to a port and the other listen to same port (I posted one sample for this here), but you can do with only one also. Here is one quick sample:
VB.NET:
Public Class Broadcast2

    Private udp As Net.Sockets.UdpClient

    Private Sub Broadcast2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        udp.Close()
    End Sub

    Private Sub Broadcast2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        udp = New Net.Sockets.UdpClient(2456)
        udp.EnableBroadcast = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ep As New Net.IPEndPoint(Net.IPAddress.Broadcast, 2456)
        Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes(TextBox1.Text)
        udp.Send(b, b.Length, ep)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If udp.Available > 0 Then
            Dim ep As New Net.IPEndPoint(Net.IPAddress.Broadcast, 2456)        
            Dim b() As Byte = udp.Receive(ep)
            Me.ListBox1.Items.Add(System.Text.Encoding.UTF32.GetString(b))
        End If
    End Sub

End Class
This form has a Button, a TextBox, a ListBox and a Timer (500ms, enabled).
 
Do you have to have a gateway type app involved?

John,
I have been working with a chat type program code that uses a gateway app on a server that all the clients log into.

Is this the way it has to be? I was thinking from the reading that I could have clients listen on a port and then when I want to send a message from my server app that I just broadcast on that port?

Thanks,
Ty
 
Back
Top