Returning UDP traffic in real-time and control latency

W

Wayne M J

I am capturing syslogd traffic from a firewall/router, if I display the
results to Console it runs fine and dandy, but the minute I need it to go to
a ListBox, DataGrid or even a text box problems begin to arise.

It comes down to what looks like latency, but the control are not fast
enough to refresh in time to display all of the packet contents.

I have tried threading the function, but that only increases the latency.
At one point I seriously thought it was my code:
While True
Dim data(1024) As Byte
Dim recv As Integer
recv = s.ReceiveFrom(data, ep)
'iep = ep '*** Only needed to get the source of the traffic ***
Dim strData As String = System.Text.Encoding.ASCII.GetString(data, 0,
recv)
Console.WriteLine(strData)
End While

That is the business end of the code, and there is not much tweaking there
that can be done to speed up returns.

I have tried this code on a P4, a P3 and an old P2 with the same results,
the controls just did not refresh fast enough such that the next line of
traffic could be added.

I have compiled the code in both Debug (very bad latency about 2 messages in
3 were lost) and Release (1 in 2 to as low as 1 in 5 messages lost).
 
M

Mario

Hi Wayne,

I dont know how many stuff are you getting and if it possible to handle it
by a vb.net windows application... but we can try. You posted the console
version :-( and what we need is the windows version, where the bug or
problem is. Any way, I suggest 3 hacks:

First trick is not up update for every packet! Update only for every half second.
Cache the results until a Timer fires, and then flush the cache into a control.

Second trick is to use a StringBuilder for caching, not a string.

Third trick is to .BeginUpdate .EndUpdate your control (eg. ListBox).

Here is a sample (with a RichTextBox... add it to the form):


' Declare at module level
Private sb As New System.Text.StringBuilder

' start the timer
With Timer1
.Interval = 500
.Enabled = getData
End With
While getData = True
Dim data(1024) As Byte
Dim recv As Integer
recv = s.ReceiveFrom(data, ep)
Dim strData As String
strData = System.Text.Encoding.ASCII.GetString(data, 0, recv)
' Add data to cache
sb.Append(strData & vbCrLf)
End While
' stop the timer
Timer1.Enabled = False


Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
' flush cache
RichTextBox1.AppendText(sb.ToString)
' clear cache
sb.Remove(0, sb.Length)
End Sub


Hope this helps.
Best regards,
Mario
 
W

Wayne M J

Thanks for the reply (and sorry for the delay in response).

I ended up solving the problem, by doing nothing more than upgrading from
VS.Net2002 to VS.Net2003.

This is something that I have noticed quite a lot lately, that any code that
I wrote under 2002 suffers from latency whilst the 2003 code runs perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top