sending and receiving messages to/from command prompt in vb.net

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 13078 Views - Last Post: 06 August 2013 - 05:53 PM Rate Topic: -----

#1 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

sending and receiving messages to/from command prompt in vb.net

Posted 27 July 2013 - 06:42 PM

Hey, Im writing a small program that sends Messages in telnet and then reads the return from that message into vb.net but im stuck on the fundamentals.
I cant figure out how to send a message from vb.net to command prompt and then grab the returned message from the command prompt window and put it in a variable.

In an example:

I want to send the message 'get srvcount' which should return '200 0' So i need to send the message to the window then grab the '200 0' and put it in a variable. I then need to strip the '200 ' from the string so im just left with the '0' which would be the server count.

Can anyone help me with this?

Kind Regards
Jack

Is This A Good Question/Topic? 0
  • +

Replies To: sending and receiving messages to/from command prompt in vb.net

#2 ybadragon  Icon User is offline

  • Home Owner
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,647
  • Joined: 11-May 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 27 July 2013 - 08:47 PM

Is the '200 0' always a constant number, or does it possibly change to '300 0' or '300 56', etc... If it is constant of having a space before the server count, you could always assign your server count variable to everything after the space, like so

' we'll say sServReturn is the string your command returns
Dim sServReturn As String = "200 0"
' with this we'll take the substring of everthing after the space in sServReturn
' this will be the server number that you want
Dim sServNumber As String = sServReturn.Substring(sServReturn.IndexOf(" "))


Was This Post Helpful? 1
  • +
  • -

#3 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 29 July 2013 - 04:30 AM

Yeah the 200 is constant so that will work once i figure out how to send the messages thanks!

This post has been edited by andrewsw: 29 July 2013 - 08:29 AM
Reason for edit:: Removed immediate quote

Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 29 July 2013 - 07:32 AM

View PostSPACEMTNMAN, on 27 July 2013 - 07:42 PM, said:

Hey, Im writing a small program that sends Messages in telnet and then reads the return from that message into vb.net but im stuck on the fundamentals.
I cant figure out how to send a message from vb.net to command prompt and then grab the returned message from the command prompt window and put it in a variable.

I guess I'm a little confuse in trying to figure out what you want to do. If you want to Telnet using VB.Net, you could just write a Telnet Client, either as a Console of GUI application.

For an example, check This out.
Was This Post Helpful? 1
  • +
  • -

#5 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 05:39 AM

View Postlar3ry, on 29 July 2013 - 08:32 AM, said:

View PostSPACEMTNMAN, on 27 July 2013 - 07:42 PM, said:

Hey, Im writing a small program that sends Messages in telnet and then reads the return from that message into vb.net but im stuck on the fundamentals.
I cant figure out how to send a message from vb.net to command prompt and then grab the returned message from the command prompt window and put it in a variable.

I guess I'm a little confuse in trying to figure out what you want to do. If you want to Telnet using VB.Net, you could just write a Telnet Client, either as a Console of GUI application.

For an example, check This out.


Im creating a manager program that communicates with a server via telnet but the connection in telnet needs to stay constantly open and the whole thing needs to be done behind the scenes because the user of the manager wont know the commands and they only need to see the values it returns. Ill take a look at that code and see if i can adapt it thanks :D
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 05:51 AM

Good plan. Think in terms of having the telnet client you were going to communicate with, inside your program. The part that the user interacts with can be exactly how you were going to do it, but you use the internal client to do the actual telnet operations.
Was This Post Helpful? 1
  • +
  • -

#7 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 06:14 AM

View Postlar3ry, on 30 July 2013 - 06:51 AM, said:

Good plan. Think in terms of having the telnet client you were going to communicate with, inside your program. The part that the user interacts with can be exactly how you were going to do it, but you use the internal client to do the actual telnet operations.


Ive taken a look at that code and it connects to the server great but i cant figure out how to send another command once its connected? Do you have any idea? There is a Command variable but it doesnt send the command for some reason. Just the connection string.

Here is my form. Below ill detail what needs to happen. It may help people udnerstand what im trying to do :)

Posted Image

Ok so when the user presses [1] The program needs to connect to telnet and send the connection string 'o localhost 3234' or something similar (could be 127.0.0.1 3234)

In the timer.tick code [2] will be updated every ten seconds by sending the command 'get srvcount' and 'get jobcount'

The button [3] needs to disconnect from telnet and send the kill command to the manager.

So i hope this helps. There are other parts to this where telnet returns xmls i need to parse but that is a way down the line at the moment.

So can you see why i need the connection constantly open? it would be unwise to have to reconnect to the server every tick of the timer.
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 06:44 AM

View PostSPACEMTNMAN, on 30 July 2013 - 07:14 AM, said:

Ive taken a look at that code and it connects to the server great but i cant figure out how to send another command once its connected? Do you have any idea? There is a Command variable but it doesnt send the command for some reason. Just the connection string.

No great problem. All you need to do is to split out the various parts. The author has been kind to us by commenting his code and using meaningful variable names.

In the SendCommands() subroutine, the first part is the "connect' code. The "sending a command" code starts at the line that says If tnSocket.Connected Then

You will have to pay attention to a few things when splitting that subroutine up, of course.

1. you might want to replace the "send command" part you move out, with code that tells the user that he is connected, or otherwise indicates that all is well.

2. you will likely have to move some variables to allow all subroutines access to them.

Give it a shot, and get back to use with your questions or a "success!" report.
Was This Post Helpful? 1
  • +
  • -

#9 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 07:19 AM

View Postlar3ry, on 30 July 2013 - 07:44 AM, said:

View PostSPACEMTNMAN, on 30 July 2013 - 07:14 AM, said:

Ive taken a look at that code and it connects to the server great but i cant figure out how to send another command once its connected? Do you have any idea? There is a Command variable but it doesnt send the command for some reason. Just the connection string.

No great problem. All you need to do is to split out the various parts. The author has been kind to us by commenting his code and using meaningful variable names.

In the SendCommands() subroutine, the first part is the "connect' code. The "sending a command" code starts at the line that says If tnSocket.Connected Then

You will have to pay attention to a few things when splitting that subroutine up, of course.

1. you might want to replace the "send command" part you move out, with code that tells the user that he is connected, or otherwise indicates that all is well.

2. you will likely have to move some variables to allow all subroutines access to them.

Give it a shot, and get back to use with your questions or a "success!" report.


' -----------------------------------------------------------------------------------
' Quick easy TELNET (synchronous) quide in 100% VB.NET (no silly Asock.dll needed!!!)
' Please use this code freely NO RESTRICTIONS, change as you like.
' -----------------------------------------------------------------------------------
' This demo is intended to help developers with no experience of telnet
' to get started. The demo is just that a DEMO, it is NOT a finished product
' and MAY contain bugs.
' Rimmelę
' Please email nemo1966:gmail.com for questions/help with this code.
' -----------------------------------------------------------------------------------
' Note: the bytes received from the telnet socket will need formatting to 
' get rid unwanted chars and/or converting to a different format. This demo
' simply converts to ASCII.
' -----------------------------------------------------------------------------------

' -----------------------------------------------------------------------------------
' Note: An error can occur if the recv byte lenth on the last call is exacly 
' 256 bytes. Will will cause the receive call in the loop to block (waiting).
' This is where an asynchronous solution would be better.
' -----------------------------------------------------------------------------------


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

Public Class Form1

    ' These vars can be formwide or local (in procedure) depending on how you want to use them.
    Dim remoteIPAddress As IPAddress
    Dim ep As IPEndPoint
    Dim tnSocket As Socket
    ' LCommand: Actual command we are going to send through telnet
    ' Quite often this is the password or login (or both)

    Dim Command As String = "get status" ' Example command only - you need to use your own

    ' LRecvString: data returned from the telnet socet
    Dim RecvString As String = String.Empty

    ' NumBytes: Number of bytes return from telnet socket (count)
    Dim NumBytes As Integer = 0
    Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command & vbCrLf)

    ' Create a byte array for recieving bytes from the telnet socket
    Dim RecvBytes(255) As [Byte]

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        SendCommands("127.0.0.01", "3234")
    End Sub

    Private Sub SendCommands(ByVal PIPAddress As String, ByVal PPort As String)


        ' Get the IP Address and the Port and create an IPEndpoint (ep)
        remoteIPAddress = IPAddress.Parse(PIPAddress.Trim)
        ep = New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))

        ' Set the socket up (type etc)
        tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)



        Try
            ' Connect
            tnSocket.Connect(ep)
        Catch oEX As SocketException
            ' error
            ' You will need to do error cleanup here e.g killing the socket
            ' and exiting the procedure.
            Exit Sub
        End Try
        MsgBox("connected!")

        ' If we get to here then all seems good (we are connected)


        ' Cleanup
        ' remoteIPAddress = Nothing
        'ep = Nothing
        'tnSocket = Nothing
        'Command = Nothing
        'RecvString = Nothing


    End Sub

    Private Sub Wait(ByVal PMillseconds As Integer)
        ' Function created to replace thread.sleep()
        ' Provides responsive main form without using threading.

        Dim TimeNow As DateTime
        Dim TimeEnd As DateTime
        Dim StopFlag As Boolean

        TimeEnd = Now()
        TimeEnd = TimeEnd.AddMilliseconds(PMillseconds)
        StopFlag = False
        While Not StopFlag
            TimeNow = Now()
            If TimeNow > TimeEnd Then
                StopFlag = True
            End If
            Application.DoEvents()
        End While

        ' Cleanup
        TimeNow = Nothing
        TimeEnd = Nothing
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            ' Wait a few seconds (3) (depending on connection) telnet can be slow.


            ' Double check we are connected
            If tnSocket.Connected Then
                ' Send the command
                tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)

                ' -------------------------------------------------------------------
                ' The below "do loop" is not actually needed. This loop is used to
                ' Provide feedback for the commands you issue. e.g. like a hyperterm 
                ' window. If you simply want to send command with no feedback rem it out
                ' -------------------------------------------------------------------

                ' loop getting 256 bytes of data from telnet socket at a time
                Do
                    ' RecvBytes with contain 256 bytes if data returned
                    ' numbytes with have the count of bytes returned
                    NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
                    RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
                Loop While NumBytes = 256 ' if less than 256 we are at the end of the recv stream

                ' Send recieved bytes to the output text box
                txtRecv.Text = RecvString
                ' -------------------------------------------------------------------
                Wait(1000)

                ' Disconnect
                tnSocket.Disconnect(False)
            End If
        Catch oEX As Exception
            ' Error cleanup etc needed
        End Try
    End Sub
End Class



Ok so ive managed to split out the code so it doesnt send the command until i click a button i added.
However it still doesnt seem to want to send the command as nothing shows up in the rich text box that is already on the form apart from the initial connection readout.
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 07:31 AM

It would probably enlighten you if you set a few breakpoints and when it stops, check the values in variables while single-stepping through the code. If you aren't sure how to do this, check out our Debugging Skills turorials here or here.

I'll hve a look at your code when I return later.
Was This Post Helpful? 1
  • +
  • -

#11 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 07:43 AM

View Postlar3ry, on 30 July 2013 - 08:31 AM, said:

It would probably enlighten you if you set a few breakpoints and when it stops, check the values in variables while single-stepping through the code. If you aren't sure how to do this, check out our Debugging Skills turorials here or here.

I'll hve a look at your code when I return later.


Ive stepped through the code with the breakpoints and when i step through it returns exactly what i want but when i just run the program it doesnt work. I think i might need to ad a sleep in somwhere as i think it may be going faster than telnet can.
Was This Post Helpful? 0
  • +
  • -

#12 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 169
  • Joined: 18-October 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 08:27 AM

I had a similar problem when I was reading data from a com port scanner; what I ended up doing was adding a loop to read the input and concatenating a string with each new message received from the buffer, with the loop terminating on the ETX character sent by the scanner. Something similar will probably work for you here if the host system sends an ETX (or CRLF or whatever) when it's done transmitting.
Was This Post Helpful? 0
  • +
  • -

#13 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 08:49 AM

View PostC.Andrews, on 30 July 2013 - 09:27 AM, said:

I had a similar problem when I was reading data from a com port scanner; what I ended up doing was adding a loop to read the input and concatenating a string with each new message received from the buffer, with the loop terminating on the ETX character sent by the scanner. Something similar will probably work for you here if the host system sends an ETX (or CRLF or whatever) when it's done transmitting.


Whoa there thats a whole lotta stuff that i have no idea about right there!
I Think i understand what you mean. are you saying i should create a loop that repeats my read function until the last character of the string is reached?
Was This Post Helpful? 0
  • +
  • -

#14 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 11:58 AM

View PostSPACEMTNMAN, on 30 July 2013 - 09:49 AM, said:

Whoa there thats a whole lotta stuff that i have no idea about right there!
I Think i understand what you mean. are you saying i should create a loop that repeats my read function until the last character of the string is reached?

Yes, there's already a Do/Loop in the SendCommand() subroutine. It gathers 256 bytes of data at a time.. What you'll need to do is to figure out how many bytes you get back, and what they are. If you know either one of these things, you can change that loop to allow you to detect the end of the transaction. You could use a timer to start a routine that gets data from the Socket, and checks for it being a complete reply, or just wait in the loop until it's complete.

As well, If you are testing the code you last posted, I hope you are aware that there are some specific addresses and strings in there that will not work on your machine. For example, IP address 127.0.0.1 is a local loopback (a connection to your own machine), and if you don't have a Telnet host running, you will not get a response. The command that is sent is just a string, "3234". You'll need to change these to fit your target IP address and commands you want to send.

Unfortunately, I don't know of any machine I can connect to via Telnet, so my troubleshooting options are limited.
Was This Post Helpful? 0
  • +
  • -

#15 SPACEMTNMAN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-June 09

Re: sending and receiving messages to/from command prompt in vb.net

Posted 30 July 2013 - 06:36 PM

View Postlar3ry, on 30 July 2013 - 12:58 PM, said:

View PostSPACEMTNMAN, on 30 July 2013 - 09:49 AM, said:

Whoa there thats a whole lotta stuff that i have no idea about right there!
I Think i understand what you mean. are you saying i should create a loop that repeats my read function until the last character of the string is reached?

Yes, there's already a Do/Loop in the SendCommand() subroutine. It gathers 256 bytes of data at a time.. What you'll need to do is to figure out how many bytes you get back, and what they are. If you know either one of these things, you can change that loop to allow you to detect the end of the transaction. You could use a timer to start a routine that gets data from the Socket, and checks for it being a complete reply, or just wait in the loop until it's complete.

As well, If you are testing the code you last posted, I hope you are aware that there are some specific addresses and strings in there that will not work on your machine. For example, IP address 127.0.0.1 is a local loopback (a connection to your own machine), and if you don't have a Telnet host running, you will not get a response. The command that is sent is just a string, "3234". You'll need to change these to fit your target IP address and commands you want to send.

Unfortunately, I don't know of any machine I can connect to via Telnet, so my troubleshooting options are limited.



The strange thing about this is im not actually connecting to a machine. The server manager im connecting to is a program on my machine that is running but it communicates information via tcp commands through telnet with the current render manager software. Im coding a stripped down version of this render manager as the current one isnt very user friendly. So the loopback is on purpose and the 3234 is the port that the manager communicates through. I know it works because like i say if i step through the code it returns the correct information.

If anyone is interested in twhat im trying to do you can take a look by downloading the render program backburner here:

http://lib.giveawayo...kburner_117388/

then if you run the manager.exe and run telnet you can type
o localhost 3234


Into telnet and it should connect to the backburner manager.
If you type any of the commands below you will get a return

help
get status
get srvcount
get jobcount
get srvlist
get joblist



If you do this it might make it clear what im trying to do.
The current monitor software is also included in that download. Its the monitor.exe program and to connect to the manager you just click file > connect

If you decide to give it a look and it may enlighten you to my problem :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2