471,881 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,881 software developers and data experts.

Asp.net web telnet client problems

I am trying to create a telnet client that will connect to several of my network devices on campus and change settings on the devices. So far I can connect with the code below, but I can't seem to get the correct return data from the device. Sometimes I get about 100 bytes and other times I get 3000 bytes. I even put a thread sleep but that still give me different return data from different devices. I know I am going to have to use an asynccallback but I just don't know how to do that. Any help would be great. I have also tried to use networkstream.dataavailable but this still is sporatic data.

<%@ Page Language="VB" debug="true"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System" %>
<%@ Import Namespace = "System.Net.Sockets" %>
<%@ Import NameSpace="System.Threading" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Dim tcpClient As New System.Net.Sockets.TcpClient
Dim networkStream As NetworkStream
Dim numBytesRead As Integer
Dim returndata As String
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Dim sendBytes As [Byte]()

Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)

tcpClient.Connect("", 23)
networkStream = tcpClient.GetStream()
numBytesRead = networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)

Send_Receive(vbCr, "Ctrl-Y", 0)
Send_Receive(ControlKeyPressed("Y"), "Enter Password:", 0)
Send_Receive("mypasswordis123" & vbCr, "Setup", 0)
Send_Receive("c", "#", 0)

Response.Write("<BR><BR>" & returndata)
Send_Receive("show mac-address-table vid 20 address 000b.db6e.8208" & vbCr, "#", 1000)
Response.Write("<BR><BR>" & returndata)

Send_Receive("logout" & vbCr, "Setup", 0)
sendBytes = Encoding.ASCII.GetBytes("l")
networkStream.Write(sendBytes, 0, sendBytes.Length)

'If networkStream.CanRead Then
' Response.Write("loop 1")
' Dim myReadBuffer(5000) As Byte
' Dim myCompleteMessage As StringBuilder = New StringBuilder()
' Dim numberOfBytesRead As Integer = 0

' ' Incoming message may be larger than the buffer size.
' Do
' numberOfBytesRead = networkStream.Read(bytes, 0, 1)
' myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(bytes, 0, 5000))

' Loop While networkStream.DataAvailable
' Thread.Sleep(100)
' ' Print out the received message to the console.
' Response.Write(("You received the following message : " + myCompleteMessage.ToString()))
' Response.Write("Sorry. You cannot read from this NetworkStream.")
'End If


End Sub

' This function sends the Ctrl key to the telnet server
Function ControlKeyPressed(ByVal character)
ControlKeyPressed = Chr(Asc(character) - 64)
End Function

' This function sends data to the telnet server and waits for data to be returned from the telnet server.
Function Send_Receive(ByVal Sending As String, ByVal Finding As String, ByVal Pause As Integer)
sendBytes = Encoding.ASCII.GetBytes(Sending)
networkStream.Write(sendBytes, 0, sendBytes.Length)

' Pause let the returned data buffer complete.
numBytesRead = networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)
returndata = Encoding.ASCII.GetString(bytes, 0, numBytesRead)
bytes.Clear(bytes, 0, bytes.Length)
Loop While InStr(returndata, Finding) = 0
Return returndata
End Function

Aug 26 '06 #1
0 3698

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Tom Lynn | last post: by
3 posts views Thread by Yannick Turgeon | last post: by
6 posts views Thread by Donnal Walter | last post: by
2 posts views Thread by Bruce W...1 | last post: by
13 posts views Thread by Godzilla | last post: by
5 posts views Thread by pbd22 | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.