troubleshooting Question

Issue with UDP Port Program freezing. HELP!!!

Avatar of bradbritton
bradbritton asked on
Visual Basic Classic
9 Comments1 Solution267 ViewsLast Modified:
I have a program that is listening on a UDP Port for an incoming datapacket, then turns that output to hex. If there is no data comming in (streaming from field device), the program freezes. I have tried using threads and such,  but it is still freezing! Here is the body of the code.

Public Class udpReader
    Public Const udpInPort As Integer = 54321
     Public modemIP As New System.Net.IPEndPoint(System.Net.IPAddress.Any, udpInPort) 'address of sender and port used
    Public bytes As [Byte]()
    Public hexstring As String
    Public udpRecThread As Thread
    Public ba As BitArray ' Bit array to store the data
    Public dataListener As UdpClient ' the UDP Client that is used for the data to be streamed in, port client is sending from

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Sub recMessage() 'gets message from remote logger
        bytes = dataListener.Receive(modemIP) 'displays modem info
        ba = New BitArray(bytes) 'places incoming data into the bitarray
        hexstring = BytesToString(bytes, True)
        lblSizeInfo.Text = ba.Length.ToString 'gives length of the bit array

    End Sub
    Private Sub btnStart_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Try
            dataListener = New System.Net.Sockets.UdpClient(udpInPort) 'starts listening for data
            recMessage() 'runs the sub routine to get the data
            udpRecThread = New System.Threading.Thread(AddressOf recMessage) 'thread to handle connection
            udpRecThread.Start() 'starts thread when data is received

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        txtIP.Text = modemIP.ToString() ' displays the modems IP
        txtData.Text = hexstring 'displays the hex data from the modem
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        udpRecThread.Abort() ' closes the any threads
        dataListener.Close() ' closes the ports
        Me.Close() ' closes the form
    End Sub
    Function BytesToString(ByVal bytes As Byte(), Optional ByVal AsHex As Boolean = True) As String
        Dim sb As New System.Text.StringBuilder
        Dim sw As New System.IO.StringWriter(sb)
        Dim nl As String = System.Environment.NewLine
        Dim count As Integer = 0
        Dim blockSize As Integer = 16
        Dim blockFormat As String
        Dim itemFormat As String

        If AsHex Then
            blockFormat = "{0:X4}: "
            itemFormat = " {0:X2}"
        Else
            blockFormat = "{0,5}: "
            itemFormat = " {0,3}"
        End If

        For blockStart As Integer = 0 To bytes.Length Step blockSize
            sw.Write(String.Format(blockFormat, blockStart))
            For index As Integer = blockStart To blockStart + blockSize - 1
                If index < bytes.Length Then
                    sw.Write(String.Format(itemFormat, bytes(index)))
                End If
            Next
            sw.WriteLine()
        Next

        Return sw.ToString
    End Function
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros