Category Archives: Programming

Reading Broadcasted UDP

This is the code used to listen to broadcasted data on the local network.


Private Sub Listen()
Try
DimSTATUS as string = string.empty
Dim port As Integer = 8050
Dim done As Boolean = False
Dim listener As New UdpClient(port)
Dim groupEP As New IPEndPoint(IPAddress.Any, port)
Try
While Not done
STATUS = "Waiting for broadcast"
Dim bytes As Byte() = listener.Receive(groupEP)
STATUS = "Received broadcast from " & groupEP.ToString()
debug.print(Encoding.ASCII.GetString(bytes, 0, bytes.Length))

End While
Catch e As Exception
STATUS = "ERR: 4" & e.Message.ToString
Finally
listener.Close()
End Try
Catch ex As Exception
STATUS = "ERR: " & ex.Message.ToString
End Try

End Sub

Broadcasting UDP

This is useful for broadcasting messages over the local network or to broadcast data coming from hardware over the network where it can be received from another computer and processed.


Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Function GetLocalBroadCastIP() As System.Net.IPAddress
Dim localIP() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName)
For Each current As System.Net.IPAddress In localIP
If current.ToString Like "*[.]*[.]*[.]*" Then
Try
Dim SplitVar() As String = current.ToString.Split(".")
If Len(SplitVar(0)) <= 3 And Len(SplitVar(1)) <= 3 And Len(SplitVar(2)) <= 3 And Len(SplitVar(3)) <= 3 Then current = IPAddress.Parse(SplitVar(0).ToString & "." & SplitVar(1).ToString & "." & SplitVar(2).ToString & ".255") Return current End If Catch ex As Exception End Try End If Next End Function Private Sub SendUdpData(str As String) Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) Dim broadcast As IPAddress = GetLocalBroadCastIP Dim sendbuf As Byte() = Encoding.ASCII.GetBytes(str) Dim ep As New IPEndPoint(broadcast, 8050) s.SendTo(sendbuf, ep) If txtSent.TextLength > MAXTEXT Then
txtSent.Clear()
End If
txtSent.AppendText(str)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendUDPData("hello World")
end sub

Binary Formatting as Dictionary(of

The following Subroutines will encode as dictionary(of String, String) as a binary element to save to a hard drive. I use this to save Settings that I do not put into a registry.

#Region "Binary Saving / Loading"
Dim SettingsFile as string = My.Application.Info.DirectoryPath & "\Settings.bin"
Private Sub SaveBinary(ByVal d As Dictionary(Of String, String))
Dim MyFormatter As New BinaryFormatter()
Dim MyFile As New FileStream(SettingsFile, FileMode.Create, FileAccess.Write, FileShare.None)
MyFormatter.Serialize(MyFile, d)
MyFile.Close()
End Sub
Private Function LoadBinary() As Dictionary(Of String, String)
Dim d As New Dictionary(Of String, String)
Dim MyFormatter As New BinaryFormatter()
Dim MyFile As New FileStream(SettingsFile, FileMode.Open, FileAccess.Read, FileShare.None)
d = MyFormatter.Deserialize(MyFile)
MyFile.Close()
Return d
End Function
#End Region

Custom Sorting Numbers in a Non-Linear Orders

I had a problem where a company wanted an array of pictures from a database custom sorted. The pictures already had ID’s assigned to them from a database index.

The items selected would be of varying lengths each time, and not all images would be selected or used.

Computers are great at sorting numbers ascending and descending, but there is really no simple way to order numbers in a nonlinear order.

So taking those indices and the order they requested, I finally came up with a fairly simple way to reorder the requested items without having to hardcode the silly order in.
Dim order() As Integer = {20, 18, 15, 4, 6, 13, 3, 19, 2, 14, 0, 5, 16, 1, 10, 7, 11, 8, 12, 9, 17}
Dim items() As Integer = {9, 12, 14, 14, 5, 8, 3, 10, 18, 20}
Dim dtsort As New DataTable
TextBox1.Clear()
dtsort.Columns.Add("val", GetType(Integer))
dtsort.Columns.Add("sort", GetType(Integer))

For Each i As Integer In items
dtsort.Rows.Add(i, Array.IndexOf(order, i))
Next

Dim dv As New DataView(dtsort)
dv.Sort = "sort ASC"

For Each row As DataRowView In dv
TextBox1.AppendText(row.Item("val") & vbCrLf)
Next