latika singh

latika singh

  • NA
  • 31
  • 32.8k

Win Forms UDP application

Feb 7 2012 10:10 AM
Hi All,

I am trying to establish a connection through UDP to a hardware device I want to talk to. Following is my implementation on the win forms:
I catches exception and gives the error message when I try to send the data. It says: "Cannot send packets to an arbitrary host while connected."

Pl advise.

Try
            Dim dataSend() As Byte = New Byte() {}

            myEP = New IPEndPoint(IPAddress.Parse("192.168.2.50"), 20000)
            myUDP.Connect(myEP)

            If myUDP.Client.Connected Then
                lblStatus.Text = "Connection successful"
            End If

            lblStatus.Text = "Converting Data"
            dataSend = Encoding.ASCII.GetBytes(txtDatatoSend.Text)
            If dataSend.Length = 0 Then
                lblStatus.Text = "Send textbox cannot be left blank"
            Else
                lblStatus.Text = ""
            End If
            If myUDP.Client.Connected Then
                myUDP.Send(dataSend, dataSend.Length, myEP)
            End If
        Catch ex As System.Exception
            MsgBox(ex.ToString)
        End Try



Answers (3)