Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27569

VS 2010 telnet to several servers one at a time using a loop but only get the first one.

$
0
0
Hello group. I need to connect to several unix boxes one at a time using a loop but once I connect to the first one then disconnect from it I cannot connect to any of the others. I have to close my program then restart it to make the next connection.

Here is a cut down version of what I am doing. NOTE: I have left out all the "Stuff" I have to do to read the data coming from the server to determine if it is asking for the login and password etc.

Imports System
Imports System.IO
Imports System.Net.Sockets
Imports System.Text


Public Class Reboot_the_systems
Dim clientSocket As New TcpClient()
Dim serverStream As NetworkStream
Dim readData As String = ""
Dim EchoText As String = ""


Private DispBuf() As Byte
Private DispData As String

Private Sub Reboot_All_Units()
Dim i as integer = 0
For i = 0 to 7
openConnection(IP(i))
next
End Sub


Public Sub OpenConnection(ByVal IP As String)


Try
clientSocket.Connect(IP, 23)
serverStream = clientSocket.GetStream()
RunClient()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


End Sub


Private Sub RunClient()
Do While (clientSocket.Connected)
serverStream = clientSocket.GetStream()
Dim inStream(10240) As Byte
Try
serverStream.Read(inStream, 0, clientSocket.ReceiveBufferSize)
Catch ex As Exception
End Try
GetDispData(inStream)
readData = DispData
DispData = ""
ShowText()
Loop
End Sub


Private Sub ShowText()
EchoText = String.Concat(EchoText, readData) ' txtDisplay.Text + Environment.NewLine + readData


If InStr(EchoText, "login:") > 0 Then
Send_Text_To_Remote("root")
End If


If InStr(EchoText, "Password:") > 0 Then
Send_Text_To_Remote("pwd")
End If


If InStr(EchoText, "#") > 0 Then
Send_Text_To_Remote("Reboot")
CloseConnection()
End If


End Sub


Private Sub Send_Text_To_Remote(ByVal txt As String)
If clientSocket.Connected Then
Dim outStream As Byte()
outStream = Encoding.ASCII.GetBytes(txt & vbCr)
serverStream.Write(outStream, 0, outStream.Length)
End If
End Sub


Public Sub CloseConnection()
clientSocket.Close()
End Sub

The error message I get when the loop tries to open the next unit is as follwows;

Cannot access a disposed object.
Object name: 'System.Net.Sockets.TcpClient'.

I would appreciate it if anyone can guide me in the right direction.

Thanks

Richard

Viewing all articles
Browse latest Browse all 27569

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>