Sending Commands to Telnet - vb.net

I have a Form that I am making that lets me select a line and reset all of the port on the line. This is done through Telnet. I understand how to Socket and send the IP Address I wish to work with but what I do not understand is sending the commands to log in and reset the ports.
The set up is that when one of more check boxes are selected for the different line it calls on private sub to run a line before starting on the next one.
I have been web searching for a few days not. The last code I tried was the following:
Dim TelnetClient As TcpClient
Dim ThisStream As NetworkStream
Dim SendBuffer(128) As Byte
Dim ReadBuffer(128) As Byte
Dim ReturnVal As String
Dim ReturnLength As Integer
TelnetClient = New TcpClient("Ip Address", 23)
ThisStream = TelnetClient.GetStream
SendBuffer = System.Text.Encoding.ASCII.GetBytes("Username")
ThisStream.Write(SendBuffer, 0, SendBuffer.Length)
ReturnLength = ThisStream.Read(ReadBuffer, 0, ReadBuffer.Length)
ReturnVal = System.Text.Encoding.ASCII.GetString(ReadBuffer)
SendBuffer = System.Text.Encoding.ASCII.GetBytes("Password")
ThisStream.Write(SendBuffer, 0, SendBuffer.Length)
I have been going around in circle trying to understand this.
I have tried doing the Telnet through cmd.exe but I keep coming back with errors and abandoned that route.
I have also seen using code to find words in the Telnet.
Example:
If message.ToString.EndsWith("login:") Then
Await WriteStringAsync("username", stream
But not 100% sure on how to fully adapt it to what I can use.
Any help is appreciated.
Thank you.
Edit: Addition Info.
I have the following at the top of the code list
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
I am new to using Telnet with vb.net. However, why is it so hard to do this in vb.net and in Cmd.exe it only takes six commands?

Okey mate , here is the code you are looking for , but focus :
Dim Full_Stop As String = ""
Dim TelnetClient As New TcpClient
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
TelnetClient.Connect("IP ADDRESS", 23) 'Connecting to the IP Given
Send_Sub("Start Connection Command")
Dim thr As New Threading.Thread(AddressOf Receive_thread)
thr.Start()
End Sub
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handels StopButton.Click
Full_Stop = "Stop"
TelnetClient.Close()
End Sub
Sub Send_Sub(ByVal msg As String)
Dim byt_to_send() As Byte = System.Text.Encoding.ASCII.GetBytes(msg)
TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None)
End Sub
Sub Receive_thread()
re:
If Full_Stop = "Stop" Then Exit Sub 'If you set Full_Stop string to "Stop" the thread will end
If TelnetClient.Client.Available < 0 Then 'Check if there is any Data to receive
Dim byt_to_receive(TelnetClient.Available - 1) As Byte
TelnetClient.Client.Receive(byt_to_receive, 0, byt_to_receive.Length, SocketFlags.None)
Dim String_From_Byte As String = System.Text.Encoding.ASCII.GetString(byt_to_receive)
If String_From_Byte = "login:" Then 'If the telnet asks you to Enter the login name the Send_Sub will do the job
Send_Sub("username")
ElseIf String_From_Byte = "password:" Then 'If the telnet asks you to Enter the Password the Send_Sub will do the job
Send_Sub("password")
End If
End If
GoTo re 'this will NOT allow the thread to End by sending it back to re: statement, unless the Full_Stop is "Stop"
End Sub

Related

Why is the process id not found in GetProcesses()

I am starting a PDF, and I get a process.
The PDF is shown in a Chrome window.
Then I try to find this process among the processes, but it is not there.
I have even tried adding some time to wait before calling GetProcesses(), but that wouldn't change anything.
What am I missing?
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim sPath As String = "d:\test.pdf"
Dim nProc As Process = System.Diagnostics.Process.Start(sPath)
Dim iProcID As Integer = nProc.Id
Dim allProcesses() As Process = System.Diagnostics.Process.GetProcesses
For Each nProcess As Process In allProcesses
If nProcess.Id = iProcID Then
Stop 'found :-)
End If
Next
End Sub

TCP/ip recive connection but locks up when trying to read data

Please can someone tell me why my server locks up when my rclient connects and try to send data?
If I comment out:
Dim bytes(rclient.ReceiveBufferSize) As Byte
rstream.Read(bytes, 0, CInt(rclient.ReceiveBufferSize))
RString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = RStringcode here
and just have
TextBox2.Text = ("Remote connected")
the server picks up on a connected client so it must be something i have done with the .read ?
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO.Ports
Dim tclient As New TcpClient
Dim tstream As NetworkStream
Dim rclient As New TcpClient
Dim rstream As NetworkStream
Dim sArray() As String
Dim Tricopter As New TcpListener(2000)
Dim Remote As New TcpListener(2001)
Dim myVal As String
Dim TricopterThread As New Thread(AddressOf TricopterThreadSub)
Dim RemoteThread As New Thread(AddressOf RemoteThreadSub)
Dim tre(tclient.ReceiveBufferSize) As Byte
Dim TState = False
Dim RState = False
Dim SerialSwitch = False
Dim Toggle = False
Dim TString As String
Dim RString As String
Dim Remo(rclient.ReceiveBufferSize) As Byte
Dim TSendText() As Byte
Private Sub RemoteThreadSub()
rclient = Remote.AcceptTcpClient
rstream = rclient.GetStream
RState = True
End Sub
Private Sub TricopterThreadSub()
tclient = Tricopter.AcceptTcpClient
tstream = tclient.GetStream
TState = True
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
Tricopter.Start()
Remote.Start()
Timer1.Start()
TricopterThread.Start()
RemoteThread.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
myVal = ">" & TrackBar1.Value & "," & TrackBar2.Value & "," & TrackBar3.Value & "," & TrackBar4.Value & "/n"
Try
If TState = False Then
TextBox1.Text = ("No tricopter connected")
Else
TextBox1.Text = myVal
If Toggle = False Then
TSendText = Encoding.ASCII.GetBytes(myVal)
End If
If Toggle = True Then
TSendText = Encoding.ASCII.GetBytes(RString)
End If
tstream.Write(TSendText, 0, TSendText.Length)
End If
If RState = False Then
TextBox2.Text = ("No Remote connected")
Else
' TextBox2.Text = ("Remote connected")
Dim bytes(rclient.ReceiveBufferSize) As Byte
rstream.Read(bytes, 0, CInt(rclient.ReceiveBufferSize))
RString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = RString
End If
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Toggle = False Then
CheckBox1.CheckState = 1
Toggle = True
Else
CheckBox1.CheckState = 0
Toggle = False
End If
End Sub
End Class
You are assuming that reads return a specific amount of data such as ReceiveBufferSize. That is not true. A read returns at least one byte, that is all.
Just to clarify, TCP does not support message based transfer.
The correct way to read depends on the protocol. If the exact number of bytes expected is known you need to read in a loop until so many bytes are received (or use BinaryReader which does that for you).
For a line based protocol you can use StreamReader.ReadLine which again automates the looping.
ReceiveBufferSize is completely unrelated to how much data is available or will come.
DataAvailable is how much data can be read right now without blocking. But more data might come. It is almost always a bug to use it. Might return 0 at any time even if data comes in 1ms later.
EDIT: For those of you who have downloaded/tested this already, I made a bugfix to the classes so you'll need to redownload the sources if you're going to use them again.
If you want to perform proper data transfer you'll need to use a more reliable method than just simply reading random data. And as usr pointed out: the TcpClient.ReceiveBufferSize property does not tell you how much data there is to receive, nor how much data there is sent to you. ReceiveBufferSize is just a variable indicating how many bytes you are expected to get each time you read incoming data. Read the MSDN page about the subject for more info.
As for the data transfer, I've created two classes which will do length-prefixed data transfer for you. Just import them to your project and you'll be able to start using them immediatelly. Link: http://www.mydoomsite.com/sourcecodes/ExtendedTcpClient.zip
Example usage
Server side
First declare a new variable for ExtendedTcpClient, and be sure to
include WithEvents in the declaration.
Dim WithEvents Client As ExtendedTcpClient
Then you just need to use a normal TcpListener to check for
incoming connections. The TcpListener.Pending() method can be
checked in for example a timer.
When you are to accept a new TcpClient, first declare a new
instance of the ExtendedTcpClient. The class requires to have a
form as it's owner, in this application Me is the current form.
Then, use the ExtendedTcpClient.SetNewClient() method with
Listener.AcceptTcpClient() as it's argument to apply the
TcpClient from the listener.
If Listener.Pending() = True Then
Client = New ExtendedTcpClient(Me)
Client.SetNewClient(Listener.AcceptTcpClient())
End If
After that you won't be needing the timer anymore, as the
ExtendedTcpClient has it's own thread to check for data.
Now you need to subscribe to the PacketReceived event of the
client. Create a sub like so:
Private Sub Client_PacketReceived(sender As Object, e As ExtendedTcpClient.PacketReceivedEventArgs) Handles Client.PacketReceived
End Sub
In there you can for example output the received packet as text into
a TextBox. Just check if the packet header is PlainText and then
you can convert the received packets contents (which is an array of
bytes, accessed via e.Packet.Contents) to a string and put it in
the TextBox.
If e.Packet.Header = TcpMessagePacket.PacketHeader.PainText Then
TextBox1.AppendText("Message recieved: " & System.Text.Encoding.Default.GetString(e.Packet.Contents) & Environment.NewLine)
End If
Lastly, when closing the form you just need to disconnect the client.
Private Sub ServerWindow_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Client IsNot Nothing Then Client.Disconnect()
End Sub
And that's it for the server side.
Client side
For the client side you will only be needing a normal TcpClient (unless you don't want to receive data there too).
Dim Client As New TcpClient
Then connect to the server via the IP and port you've given the listener.
Client.Connect("127.0.0.1", 5555) 'Connects to localhost (your computer) at port 5555.
Now if you want to send plain text to the server you'd do something like this:
Dim MessagePacket As New TcpMessagePacket(System.Text.Encoding.Default.GetBytes(TextBox2.Text), TcpMessagePacket.PacketHeader.PainText)
MessagePacket.Send(Client) 'Client is the regular TcpClient.
And now everything should be working!
Link to a complete example project: http://www.mydoomsite.com/sourcecodes/TCP%20Messaging%20System.zip
If you want to add more headers to the class, just open TcpMessagePacket.vb and add more values in the PacketHeader enum (located in the region called Constants).
Hope this helps!
Screenshot from the example project
(Click the image for larger resolution)

Serial read value does not change

I am trying to read serial data that Arduino is sending but it keeps giving me over and over the first string that it receives even if I change the Arduino output. Why does it give me only first value and not update?
Module Module1
Sub Main()
Dim com4 As IO.Ports.SerialPort = Nothing
com4 = My.Computer.Ports.OpenSerialPort("COM4")
Dim incoming As String
Do
incoming = com4.ReadLine()
Console.WriteLine(incoming)
Threading.Thread.Sleep(250)
Loop
End Sub
End Module
It could be, that depending on your arduino sketch, it sends the same data out for some time, filling up the receive buffer, which is what gets read for some time in your Do loop.
Try using events
dim withevents com4 as serialport.......
sub datareceivedhandler (sender as object, e as eventargs) handles com4.datareceived
dim incoming as string = com4.readline
console.writeline(incoming)
end sub

Writing commands to cmd with Visual Basic

I'm using VirtualBox to attach a usb device. This usb device only works under 32 bit. My host os is Windows 7 64 bit, my Guest Windows 7 32 bit.
I found code to write to the command prompt and read it back out, this has been tested and works very well. But now after I have read and want to write again the command prompt just freezes. I have no idea why it does that... I also tried the command without VB and the driver attach fine.
Any ideas how I can solve this problem?
Public Class Form1
Private Results As String
Private test As Double
Private test2 As String
'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text
update.
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Private Sub UpdateText()
resultsTextBox.Text = Results
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
'Starts the CMD Prompt
StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
'Required to redirect
StartInfo.UseShellExecute = False
'Disables the creation of a CMD Prompt outside application.
StartInfo.CreateNoWindow = False
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
'Runs the command you entered...
'SW.WriteLine(TextBox1.Text)
SW.WriteLine("cd C:\Program Files\Oracle\VirtualBox")
SW.WriteLine("vboxmanage list usbhost")
'Exits CMD Prompt
'SW.WriteLine("exit")
'Displayes the results...
Results = SR.ReadToEnd
'Im reading the string out to get the right Device id
test = InStr(Results, "0x0547", CompareMethod.Text)
test = test - 58
test2 = Mid(Results, test, 36)
'test2 gives 80be0bc1-6f69-4886-868f-c8857bff34c1
'this is the right id, if i try to input it myselves with:
'C:\Program Files\Oracle\VirtualBox>vboxmanage controlvm "test" usbattach
'80be0bc1-6f69-4886-868f-c8857bff34c1
'it works...
SW.WriteLine("vboxmanage controlvm " + Chr(34) + "test" + Chr(34) + "usbattach " + test2)
SW.WriteLine("exit")
Results = SR.ReadToEnd
SW.Close()
SR.Close()
'Invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
End Class
I think ReadToEnd doesn't finish because the Command Window is still open. I would run half of your commands and then read it in after the EXIT command is called. Then you would simply need to parse the result string and run another Command Window process.

VB.NET - Running one sub multiple times at once

I have one Private sub that runs in a loop. I want the sub to run multiple times at once. For example the program runs, you press start; you run the program again and press start, again and again... the same program doing the job at once. now i just want one program do to it alone. But i would like it to be user defined. exp. run program. type in a text box 10. press start. and it works as if 10 of them work open working on the same thing.
I have seen another program made with vb.net 2010 and its what i use and do not know how to do it. so i am just wondering.
Private Sub Flood1(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles Flood.DoWork
Dim IP As IPAddress = IPAddress.Parse(TextBox1.Text)
Dim IPPort As New IPEndPoint(IP, Convert.ToInt32(TextBox2.Text))
Dim PacketS As Byte() = New Byte(TextBox3.Text) {}
Dim SocketN As Integer = Convert.ToInt32(TextBox4.Text)
Do While Flooding = True
For i = 0 To SocketN
If Flooding = True Then
Dim _Sock(i) As Socket
_Sock(i) = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Try
_Sock(i).SendTo(PacketS, IPPort)
Threading.Thread.Sleep(500)
Catch ex As Exception
Threading.Thread.Sleep(500)
End Try
Else
Exit Do
End If
Next
Loop
End Sub
Mostly want to have this work over and over at once by the users choice... kinda hoped not to use this code else might not get helped.
You can use background worker for that.
Once you know how many workers you want to do the job
just create those many instances of background worker.
Tell me if this is the answer you are looking for or not
Sample Source Code
Imports System.ComponentModel
Module Module1
Sub Main()
Console.WriteLine("Please enter the worker count:")
Dim workerCount As Integer = Console.ReadLine()
For i As Int16 = 0 To workerCount
Dim worker As BackgroundWorker = New BackgroundWorker
worker.RunWorkerAsync(i + 1)
AddHandler worker.DoWork, AddressOf Worker_DoWork
Next
End Sub
Private Sub Worker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Console.WriteLine(e.Argument.ToString())
End Sub
End Module