Help with Sockets and text parsing!

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Jun 2008
Posts: 7
Reputation: MarcFielding is an unknown quantity at this point 
Solved Threads: 0
MarcFielding MarcFielding is offline Offline
Newbie Poster

Help with Sockets and text parsing!

 
0
  #1
Jun 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Help with Sockets and text parsing!

 
0
  #2
Jun 23rd, 2008
I don't know rtf but try using IndexOf.
  1. Dim s As String = " This is " & Chr(27) & "test of mine"
  2. Dim d As Integer = s.IndexOf(Chr(27)) + 1
  3. Dim d1 As Integer = s.IndexOf("m", d)
  4. Dim s1 As String = s.Substring(d, d1 - d)
  5. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: MarcFielding is an unknown quantity at this point 
Solved Threads: 0
MarcFielding MarcFielding is offline Offline
Newbie Poster

Re: Help with Sockets and text parsing!

 
0
  #3
Jun 23rd, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: MarcFielding is an unknown quantity at this point 
Solved Threads: 0
MarcFielding MarcFielding is offline Offline
Newbie Poster

Re: Help with Sockets and text parsing!

 
0
  #4
Jun 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: MarcFielding is an unknown quantity at this point 
Solved Threads: 0
MarcFielding MarcFielding is offline Offline
Newbie Poster

Re: Help with Sockets and text parsing!

 
0
  #5
Jun 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Help with Sockets and text parsing!

 
0
  #6
Jun 23rd, 2008
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.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: MarcFielding is an unknown quantity at this point 
Solved Threads: 0
MarcFielding MarcFielding is offline Offline
Newbie Poster

Re: Help with Sockets and text parsing!

 
0
  #7
Jun 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC