Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a Telnet Client in VB.NET

Status
Not open for further replies.

2xhelix

IS-IT--Management
Jul 15, 2004
9
CA
So far, I have been able to connect to the telnet server, however, the data the comes back looks like

RichTextBox1Attempting connection to host...
Connected to server.
}'}[?3l[?7h[?3l[?7h   Sign On   System . . . . . : TAITOR  Subsystem . . . . : QINTER  Display . . . . . :

meaning, i am missing some serious formatting issues. I've placed it inside a RichTextBox already ... does anyone have any suggestions, or even sample source on how to build a telnet CLIENT in vb.net
 
Find out what the Ascii code is for the characters being returned (using someting like substring) then replace them out of the string or with somehing your textbox can accept..

one idea..


 
The sections that look like this :



Are most likely ANSI color codes. That block is an eascape character, CHR(27), the bracket signifies the color code sequence, the number determines which color (or status), and the m signifies the end of the sequence.

The  in that case means to make the current color bold.  is setting the text to its default coloration.

The parts that end with H are probably codes designed for a specific client. i.e.- They are probably extra information that a specific client understands and uses.

I'm working on a MUD client myself. It took me some work but this works well for the color codes.

===================================================
Private Sub Socket1_ReadEvent(ByVal sender As Object, ByVal e As _
AxSocketWrenchCtrl._DSocketWrenchEvents_ReadEvent) Handles Socket1.ReadEvent
Socket1.RecvLen = e.dataLength

Dim newdata As String
Socket1.RecvLen = e.dataLength
newdata = Socket1.RecvData

ColorText(newdata)
UpdateConsole()

rtbGameWindow.SelectionStart = rtbGameWindow.TextLength
rtbGameWindow.Focus()
rtbGameWindow.ScrollToCaret()
txtCmdLine.Focus()
End Sub
===================================================
Sub ColorText(ByVal ParseANSICode As String)

If ParseANSICode = "" Then Exit Sub

On Error Resume Next

Dim EscSequence As String
Dim CodeString As String
Dim ColorCode As String
Dim EscStart As Integer, EscEnd As Integer, EscLength As Integer

' Set the default font to Courier New, color to silver.
rtbTemp.SelectionFont = New System.Drawing.Font("Courier New", 10, _
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
rtbTemp.SelectionColor = System.Drawing.Color.Silver

' Apply escape characters to variable for easy use.
EscSequence = Microsoft.VisualBasic.ChrW(27) & "["

While InStr(ParseANSICode, EscSequence)
' Initialize the temp textbox.
rtbTemp.SelectionStart = rtbTemp.TextLength
If LastColor.IsEmpty Then LastColor = System.Drawing.Color.Silver
rtbTemp.SelectionLength = 0

' Set the rich text box color to the last color.
rtbTemp.SelectionColor = LastColor

' Determine the first occurrence of an ANSI color code, then find its end position.
EscStart = InStr(ParseANSICode, EscSequence)
EscEnd = InStr(EscStart, ParseANSICode, "m")
EscLength = (EscEnd - EscStart) + 1


' First extract the entire ANSI escape code from the incoming data, then extract the ColorCode numbers fromt he CodeString.
CodeString = Mid(ParseANSICode, EscStart, EscLength)
ColorCode = CodeString.Replace(EscSequence, "")
ColorCode = ColorCode.Replace("0;", "")
ColorCode = ColorCode.Replace("m", "")


' Set the selection color.
rtbTemp.SelectedText = Microsoft.VisualBasic.Left(ParseANSICode, EscStart - 1)
LastColor = SetColor(ColorCode)

' Remove the current ANSI code from the text.
ParseANSICode = Mid(ParseANSICode, EscEnd + 1)
End While

rtbTemp.SelectedText = ParseANSICode
rtbTemp.SelectionStart = rtbTemp.TextLength

Exit Sub

End Sub
====================================================
Public Function SetColor(ByVal ANSICode As Integer)

On Error GoTo ErrHandle

Select Case ANSICode
Case 0 'for normal display
SetColor = System.Drawing.Color.Silver
Case 1 'for bold on
SetColor = System.Drawing.Color.Transparent
Case 5 ' blink on
SetColor = System.Drawing.Color.Transparent
Case 7 'reverse video on
SetColor = System.Drawing.Color.Transparent
Case 8 'nondisplayed (invisible)
SetColor = System.Drawing.Color.Silver
Case 30 'black foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.White Else SetColor = System.Drawing.Color.Silver
Case 31 'red foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.Red Else SetColor = System.Drawing.Color.DarkRed
Case 32 'green foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.Green Else SetColor = System.Drawing.Color.DarkGreen
Case 33 'yellow foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.Yellow Else SetColor = System.Drawing.Color.Olive
Case 34 'blue foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.Blue Else SetColor = System.Drawing.Color.DarkBlue
Case 35 'magenta foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.Magenta Else SetColor = System.Drawing.Color.DarkMagenta
Case 36 'cyan foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.Cyan Else SetColor = System.Drawing.Color.DarkCyan
Case 37 'white foreground
If LastColor.Equals(System.Drawing.Color.Transparent) Then SetColor = System.Drawing.Color.White Else SetColor = System.Drawing.Color.WhiteSmoke
End Select

Exit Function
ErrHandle:
MsgBox(Err.Number & ", " & Err.Description & ", " & "SetColor")
Resume Next
End Function
=================================================
Public Sub UpdateConsole()
On Error GoTo ErrHandle

rtbGameWindow.SelectionStart = rtbGameWindow.TextLength
rtbGameWindow.SelectionLength = 0
rtbGameWindow.SelectedRtf = rtbTemp.Rtf

rtbTemp.Rtf = rtbTemp.Rtf.Empty
rtbGameWindow.SelectionStart = rtbGameWindow.TextLength

Exit Sub

ErrHandle:
MsgBox(Err.Number & Err.Description & "UpdateConsole")
Resume Next
End Sub
==========================================


In the SetColor function, there are several codes unaccounted for because they are not normally used in MUD games.
 
Also, one problem I've not discovered a way around yet with that code, it appears to double space the text when it puts it on the main rich text box. I've tried removing it, but the only way it works is for me to replace all the cr's, lf's and \par's with empty strings, which causes all the lines to run together.

In the code above you may have noticed I used to rich text boxes. rtbGameWindow is the rtb that the user sees, rtbTemp is hidden beneath it and is where all the incoming data is stored before it gets parsed and colored and finally projected to the rtbGameWindow.
 
Thanks, I will try to use some of this code today. What about the cursor location from the system? Even if i am able to properly display the telnet session, do you have any clue on how I would manipulate the cursor?
 
The cursor from the text box?

I think you should be able to set its position using RTB1.SelectionStart

don't forget to set RTB1.SelectionLength to 0 though or you might start overwriting what is in the text box.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top