Redirecting console output in vb.net

Kaos

[H]ard|Gawd
Joined
Oct 14, 2003
Messages
1,328
I have an application that I am working on to help me work a little more efficiently at work (accomplish multiple tasks within the same interface)

One of the things that I do alot is telnet to a port to see if there is something listening on it. It's easy enough to just pop open a cmd shell and do it, however Id still like to be able to do it from my interface.

The current application that I am writing simply builds jdbc connection strings, I am currently working on learning whatever would be necessary to test the string itself, but for the moment I'll settle for checking if the db port is open and listening.

I have text boxes that hold the hostname, port number and schema/db name (depending on oracle or mssql).

I then have a rich text box that when a checkbox is checked should attempt to telnet to host on the specified server and the output should be visible in the rich text box.

I found some sample code, and altered it to my needs, but I cannot seem to get it to work the way id like.

Any advice?

Code:
Private Sub telnet()
        'Should construct and send the desired console command, 
        'and then redirec the output to a rich text box

        Dim clsProcess As New System.Diagnostics.Process()
        clsProcess.StartInfo.UseShellExecute = False
        clsProcess.StartInfo.RedirectStandardOutput = True
        clsProcess.StartInfo.RedirectStandardError = True
        clsProcess.StartInfo.FileName = "cmd.exe"
        clsProcess.StartInfo.Arguments = "telnet " & txtserver.Text & " " & txtport.Text
        clsProcess.StartInfo.CreateNoWindow = True
        clsProcess.Start()


        While (clsProcess.HasExited = False)
            Dim sLine As String = clsProcess.StandardOutput.ReadLine

            If (Not String.IsNullOrEmpty(sLine)) Then
            End If

            rchPing.Text &= sLine & vbCrLf

            Application.DoEvents()
        End While

        Me.rchPing.Text += "Completed"


    End Sub

I'm completely self taught at this point, if anything makes you go "NOOB!" please point it out, I would love to learn from my mistakes.

Thanks!
 
does cmd.exe ever exit?

try using

Code:
clsProcess.CreateNoWindow = false;

to see if cmd.exe ever exits. After that, I'd try calling telnet.exe directly instead of calling cmd.exe w/ telnet as the args.
 
I tried using false, the command window comes up but I dont think it actually does anything. I tried directing it's output to a file, with no success (ie...file not written).

I also tried calling telnet itself, and could not achieve the desired result.

Im using cmd and then calling telnet because it's the exact equivalent of what I do now. Calling telnet doesnt work either (tried that before I posted)
 
Instead of passing the call to telnet as an argument, try writing it to the standard input stream
 
Learn socket programming - it'll make everything simpler.
 
try calling BeginOutputReadLine and then capturing the OutputDataReceived event
here it is in C#


p.BeginOutputReadLine();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
 
Thanks for the suggestions. Rather that ask you how to do each, I'll research on my own and post back again in a few days time.
 
lol or that, but it doesn't do what HE wants

Doesn't matter what he wants to do - there's a right way and a wrong way to solve this problem. The wrong way is just as complicated as doing it the right way. There's no reason to do it the wrong way.
 
Sometimes you need to do things the wrong way, so then the right way is more valuable to you when you come across it.

With that being said.

I tried here and there throughout the day to make it work using my original method, even refining it as I went along using "with" to define the properties rather than spell them all out.

It was going to work, you can't pass arguments to cmd.exe the way I needed to.

So taking the suggestion of amoeba, I did a little reading on using sockets and used the following code to handle my connection test

Code:
Dim tcpclient As New System.Net.Sockets.TcpClient()
        Try
            tcpclient.Connect(txtserver.Text, intport)
            Dim networkStream As System.Net.Sockets.NetworkStream = tcpclient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                MsgBox("Connection Succesful")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

At first I forgot that I would need to handle exceptions and was forcing the app to crash every time.

This works in the way I want it to. It simply checks for a listening port and connects.

Do I need to close the socket connection when Im done with it?
 
Indeed, so now onto the proper method of testing a jdbc string...maybe I can do this with one of the data controls.
 
Back
Top