Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Message sender via UDP

Avatar of Henry Ottiz
Henry Ottiz asked on
Visual Basic.NETChat / IMNetworking Protocols
18 Comments1 Solution149 ViewsLast Modified:
Hello
maybe i can have some help...

i have a small windows form that i want to use to do 3 things

1) send via UDP 4 lines of message form 4 textboxes (TBline1-4)  to a specific IP address specific in a textbox (TBIP).
2) "broadcast" to all connected devices the message(s) in each of the Textboxes (TBLine1-4)
3) send a PING to ALL devices connected in the same network

finally, those messages need to be changed to ASCII before sending...

here is my code, i can't make it work...
    Dim publisher As New Sockets.UdpClient(0)
    Dim listener As New Sockets.UdpClient(3100)
    Dim RemEP As IPEndPoint ' Dim RemEP As New IPEndPoint(IPAddress.Parse((TBIP.Text)), 3100) 'Dim RemEP As New IPEndPoint(IPAddress.Parse("192.168.199.253"), 3100) 
    Dim RemALL As New IPEndPoint(IPAddress.Any, 3100)

    '** ONE DISPLAY
    Private Sub BtDis_Click(sender As Object, e As EventArgs) Handles BtDis.Click
        Dim sendbytes() As Byte
        Dim TempString As String
        Dim rcvbytes() As Byte
        Dim RemEP As IPEndPoint
        Static pageNumber As Integer = 1

        RemEP = New IPEndPoint(TBIP.Text, 3100)

        Try
            TempString = ""
            TempString &= TBLine1.Text & "^40" & TBLine2.Text & "^80" & TBLine3.Text & "^120" & TBLine4.Text
            TempString &= Chr(2) & TempString & Chr(3)
            If pageNumber < 255 Then pageNumber += 1 Else pageNumber = 1
            sendbytes = ASCII.GetBytes(TempString)

            listener.Send(sendbytes, sendbytes.Length, RemEP)

            Try
                If listener.Available Then
                    rcvbytes = listener.Receive(RemEP)

                End If
            Catch ex As Exception
            End Try
        Catch ex As Exception
        End Try
    End Sub

    '** SEND TO ALL
    Private Sub BtAll_Click(sender As Object, e As EventArgs) Handles BtAll.Click
        Dim sendbytes() As Byte
        Dim TempString As String
        Static pageNumber As Integer = 1
        Dim RemALL As New IPEndPoint(IPAddress.Any, 3100)

        Try
            TempString = ""
            TempString &= TBLine1.Text & "^40" & TBLine2.Text & "^80" & TBLine3.Text & "^120" & TBLine4.Text
            TempString &= Chr(2) & TempString & Chr(3)

            'TempString &= Chr(2) & TBLine1.Text & "^40" & TBLine2.Text & "^80" & TBLine3.Text & "^120" & TBLine4.Text & Chr(3) ' STX & string & ETX //TempString = Chr(2) & TempString & Chr(3) 
            'TempString &= "$"
            'TempString &= TBLine1.Text & "^40" & TBLine2.Text & "^80" & TBLine3.Text & "^120" & TBLine4.Text
            'TempString &= Chr(2) & TempString & Chr(3)
            'TempString &= TBLine1.Text & vbCr & TBLine2.Text & vbCr & TBLine3.Text & vbCr & TBLine4.Text
            'TempString &= TBLine1.Text & "^40" & TBLine2.Text & "^80" & TBLine3.Text & "^120" & TBLine4.Text

            If pageNumber < 255 Then pageNumber += 1 Else pageNumber = 1
            sendbytes = ASCII.GetBytes(TempString)
            listener.Send(sendbytes, sendbytes.Length, RemALL)
        Catch ex As Exception
        End Try

    End Sub

    '**PING
    Private Sub BtPing_Click(sender As Object, e As EventArgs) Handles BtPing.Click
        Dim sendbytes() As Byte
        Dim TempString As String
        Static pageNumber As Integer = 1

        RemEP = New IPEndPoint((TBIP.Text), 3100)

        TBPing.Clear()
        Try
            TempString = ""
            TempString &= "PING"
            TempString &= TempString & vbCr 'TempString &= Chr(2) & TempString & Chr(3)

            If pageNumber < 255 Then pageNumber += 1 Else pageNumber = 1
            sendbytes = ASCII.GetBytes(TempString)
            listener.Send(sendbytes, sendbytes.Length, RemEP)
        Catch ex As Exception
        End Try

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Clear()
        listener.Client.ReceiveTimeout = 100
        listener.Client.Blocking = False
    End Sub

    Private Sub Form1_Close(sender As Object, e As EventArgs) Handles MyBase.Closed
        '       listener.Close()
        'listener.Client.Blocking = True
    End Sub


    'TIMER FOR RESPONSE
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 3100)
            Dim rcvbytes() As Byte
            If listener.Available Then
                rcvbytes = listener.Receive(ep)
                TextBox20.Text = ASCII.GetString(rcvbytes)
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TBIP.Clear()
        TBLine1.Clear()
        TBLine2.Clear()
        TBLine3.Clear()
        TBLine4.Clear()
        TBPing.Clear()
        ' TextBox20.Clear()
    End Sub

    Public Sub Clear()
        TBIP.Clear()
        TBLine1.Clear()
        TBLine2.Clear()
        TBLine3.Clear()
        TBLine4.Clear()
        TBPing.Clear()
        TextBox20.Clear()
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Bill Bach
Bill BachFlag of United States of America imagePresident and Btrieve Guru

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 18 Comments.
See Answers