| | |
Help with Sockets and text parsing!
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
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:
pos=1
' 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
i=1
do until Strings.right(rf,1) = "m" ' m is the end of the sequence
escapecode = Strings.mid(rf,pos,i)
i=i+1
loop
' 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
else
richtextbox1.text = richtextbox1.text & strings.mid(rf,pos,1)
end if
pos=pos+1
loop
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
regards
Marc
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:
pos=1
' 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
i=1
do until Strings.right(rf,1) = "m" ' m is the end of the sequence
escapecode = Strings.mid(rf,pos,i)
i=i+1
loop
' 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
else
richtextbox1.text = richtextbox1.text & strings.mid(rf,pos,1)
end if
pos=pos+1
loop
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
regards
Marc
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
I don't know rtf but try using IndexOf.
VB.NET Syntax (Toggle Plain Text)
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)
Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
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)
Else
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
Loop
If Strings.Right(esccode, 1) = ";" Then
Do Until Strings.Right(esccode, 1) = "m"
esccode = Strings.Mid(param, pos, i)
i = i + 1
Loop
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 = ""
Else
Me.RichTextBox1.SelectionColor = vcolour
RichTextBox1.AppendText(parse)
SendMessage(RichTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0)
pos = pos + 1
End If
Loop
End If
End Function
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)
Else
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
Loop
If Strings.Right(esccode, 1) = ";" Then
Do Until Strings.Right(esccode, 1) = "m"
esccode = Strings.Mid(param, pos, i)
i = i + 1
Loop
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 = ""
Else
Me.RichTextBox1.SelectionColor = vcolour
RichTextBox1.AppendText(parse)
SendMessage(RichTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0)
pos = pos + 1
End If
Loop
End If
End Function
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
Other Threads in the VB.NET Forum
- Previous Thread: Insert,delete,Update codings in VB.NET?
- Next Thread: Display hours of day in 24 hour in listview
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2005 2008 access account arithmetic array assignment basic box button buttons center check code component connectionstring convert crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dissertationthesis dosconsolevb.net dropdownlist editvb.net excel fade file-dialog firewall folder ftp generatetags hardcopy image images input insert intel isnumericfuntioncall math monitor navigate net networking opacity output passingparameters peertopeervideostreaming picturebox picturebox1 port problem problemwithinstallation project record reports" savedialog searchvb.net select shutdown string survey tcp temp temperature text textbox timer toolbox trim updown user useraccounts usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf





