Questions about TcpClient And TcpServer?

kpao

Active member
Joined
Apr 3, 2006
Messages
29
Programming Experience
Beginner
I've written the telnet window application. And I've set up a tesing server so that my program can telnet to that server. My problem are that when I telnet to the server after typing username and password through my program, there is no response output anyway. Another problem is some strange characters are outputed.The figure below show:
p1.JPG

The following are some my source code:
VB.NET:
Private client As TcpClient
    Const READ_BUFFER_SIZE As Integer = 10
    Private readBuffer(READ_BUFFER_SIZE) As Byte
    Private connectionPro As ConnectionProperties
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        menDisconnect.Enabled = False
        connectionPro = New ConnectionProperties  'ConnectionProperties is a form that is used to configure the connection settings.
    End Sub
    Private Sub DoRead(ByVal ar As IAsyncResult)
        Dim bytesRead As Integer
        Dim strMessage As String
        Try
            bytesRead = client.GetStream().EndRead(ar)
            If bytesRead < 1 Then
                MessageBox.Show("Disconnected")
                client.Close()
                rtxtShow.Enabled = False
            End If
            strMessage = ASCII.GetString(readBuffer, 0, bytesRead)
            rtxtShow.Text += strMessage
            client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, New AsyncCallback(AddressOf DoRead), Nothing)
            If Regex.IsMatch(strMessage, "[Passowrd]") Then
                'MessageBox.Show(strMessage, "Alert", MessageBoxButtons.OK)
 
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub SendData(ByVal data As String)
        Try
            Dim writer As StreamWriter = New StreamWriter(client.GetStream())
            writer.Write(data + Chr(13))
            writer.Flush()
        Catch ex As Exception
            rtxtShow.Text = ex.Message
        End Try
    End Sub
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        SendData(txtCmd.Text)
    End Sub
    Private Sub menConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menConnect.Click
        rtxtShow.Text = Nothing
        If connectionPro.IP = "" Then
            MessageBox.Show("Please go to Option -> Configuration setup the connection properties.")
            Exit Sub
        End If
        client = New TcpClient(connectionPro.IP, connectionPro.PortNum)
        Dim result As String
        client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, New AsyncCallback(AddressOf DoRead), Nothing)
        rtxtShow.Enabled = True
        menConnect.Enabled = False
        menDisconnect.Enabled = True
        If connectionPro.Username = "" Then
            SendData(connectionPro.Password)
        Else
            SendData(connectionPro.Username)
            SendData(connectionPro.Password)
        End If
    End Sub
    Private Sub menDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menDisconnect.Click
        client.Close()
        rtxtShow.Enabled = False
        menConnect.Enabled = True
        menDisconnect.Enabled = False
        MessageBox.Show("Disconnected!")
    End Sub
    Private Sub menExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menExit.Click
        Me.Dispose()
    End Sub
    Private Sub menConfigure_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menConfigure.Click
        connectionPro.ShowDialog()
    End Sub
 

Attachments

  • Telnet Monitor.zip
    46.2 KB · Views: 41
Back
Top