Hello Everyone,

I was hoping for some advice on my little problem as i'm a little stumped.

I writing what is basically a telnet client using sockets in vb.net. The client works fine until I want to start parsing the received text for ANSI escape sequences(the ones that give colour). The problem seems to mainly lie with the received buffer size.

Currently I have set the buffer to 1024, the problem being that even if the data received from the server isnt 1024 the string still is that length, heres the parsing loop:

' rf is the received buffer string.
do until pos=len(rf)

If Strings.Mid(rf,pos,1) = chr(27) ' this is the start of an ansi escape
do until Strings.right(rf,1) = "m" ' m is the end of the sequence
escapecode = Strings.mid(rf,pos,i)

' here we should be changing the colour of the richtextbox
elseif Strings.mid(rf,pos,1) = chr(10)
' this is here to cut out line feeds as the richtextbox only needs carraige returns

richtextbox1.text = richtextbox1.text & strings.mid(rf,pos,1)

end if



Basically although it works it runs very bloody slowly, 1 reason I think is because even if you only receive 10 bytes from the server the length of the received buffer is still 1024 so my code has to loop round the entire thing all the time.

To clarify i'm using the getstream method on a separate thread to pass information to this sub.

In order for my project to be viable this needs to work very fast as I will also need to compare received text to an array of strings for doing auto commands(triggers).

Thanks in advance for any suggestions you chaps can offer



I don't know rtf but try using IndexOf.

Dim s As String = " This is " & Chr(27) & "test of mine"
        Dim d As Integer = s.IndexOf(Chr(27)) + 1
        Dim d1 As Integer = s.IndexOf("m", d)
        Dim s1 As String = s.Substring(d, d1 - d)
        MessageBox.Show(s & vbNewLine & s1)

Hmm that could work actually, i'll give it a go! Its odd the old winsock control worked fine sockets seem to be a real pain to me, I cant figure a way of just telling the tcpclient to just receive data and size its buffer accordingly.

Thanks for your suggestion though i'll try it now!

Ok heres the code that works BUT for some reason the text colour isnt being changed on the richtextbox, any ideas?

Private Function Settext(ByVal param As String) As String
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim pos As Integer
Dim pos2
Dim esccode As String
Dim i As Integer
Dim parse As String

If Me.RichTextBox1.InvokeRequired Then
Dim d As New Stt(AddressOf Settext)
RichTextBox1.Invoke(d, param)
pos = 1

Do Until pos = (Len(param))

parse = Strings.Mid(param, pos, 1)
If parse = Chr(10) Then

' we skip line feeds as we dont need em because
' the rtb seems to work fine with just carriage returns
pos = pos + 1

ElseIf parse = Chr(27) Then
i = 1
Do Until Strings.Right(esccode, 1) = "m" Or Strings.Right(esccode, 1) = ";"
esccode = Strings.Mid(param, pos, i)
i = i + 1


If Strings.Right(esccode, 1) = ";" Then
Do Until Strings.Right(esccode, 1) = "m"
esccode = Strings.Mid(param, pos, i)
i = i + 1
End If

Select Case esccode
Case Chr(27) & "[1m"
RichTextBox1.Font = New Font(RichTextBox1.Font.ToString, RichTextBox1.Font.Size, FontStyle.Bold)
Case Chr(27) & "[0m"
RichTextBox1.Font = New Font(RichTextBox1.Font.ToString, RichTextBox1.Font.Size, FontStyle.Regular)
vcolour = Color.Lime
Case Chr(27) & "[33m"
vcolour = Color.Yellow

End Select

pos = pos + (Len(esccode))
esccode = ""

Me.RichTextBox1.SelectionColor = vcolour
SendMessage(RichTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0)
pos = pos + 1
End If


End If
End Function

It seems I got it, on the bit when I do NEW font(blah blah) that cancels out any text colouring!?! if I dont worry about the bolding etc it all works fine. Not ideal though as I need a way to bold some stuff

You need to set the select start and the select length for the Me.RichTextBox1.SelectionColor to color the text. I can't see anywhere that you have set these.

Actually ya don't! The reason I was having problems is I was using Richtextbox.FONT rather than .SELECTIONFONT, having switched the two it works fine.

My app displays one character at a time selectioncolor changes the selection and any proceeding characters apparently.

Still looking for a faster way to do this though as the string processing is a little slow!

Thanks for your reply.