Recently I had a requirement to control a router from within a program. It was to simplify VOIP, though the reasons are irrelevent for this article. I had done similar things in the past in VB6, but thats gone the way of the Dodo now. So the first thing to do was get an up-to-date language. Luckily, Microsoft now give away Visual Basic for free! You can use the link below to download a copy from their site.
Download VB 2008 Express from the Microsoft Site.
So I downloaded and installed VB2008, and fired it up. To start with I thought I would keep it simple, and write a (very) basic telnet client, just so that I could easily test that things were working. Unfortunately, VB2008 doesn't handle sockets in the same way that VB6 used to, so I had to work out what the correct method was.
I am still looking for the correct method! I scoured countless forums, help pages and blogs. There are loads of people looking for advice on telnet via vb, but nobody providing a simple solution that doesn't involving buying a third party tool. I did however find a snippet of code which looked promising, so I threw it into VB to see what would happen. I ended up with a functioning, if bodged, telnet client. From there it was a simple matter to write a routine which controlled the VOIP on the router without the aid of a GUI.
Now, I'm not a VB expert. PHP is my game. So the code I ended up with is a bit of a bodge, and I don't know what bits of it actually do. However, taken as a whole, I know it works. On that basis, I am documenting my workings here, for two reasons:
So lets get down to business.
1. Fire up VB 2008 and create a project called MyTel, as shown below.
2. Create a form to look like the one below. We need two text boxes for the IP address and port. A third to enter commands, and a fourth to display results. Use your own img for a logo or omit it altogether. The logo is unimportant. I just like doing shiny buttons. Assign the properties shown in the table below to the assorted controls. If you keep the names identical you will be able to drop the code given later straight in, so don't skimp on the detail.
I have only listed the important parameters. The size of each individual element is entirely up to you. The layout can be different too, provided all the controls are actually present.
Form: (Name): frmMain Text: MyTel Text boxes: 1. (Name): txtIP Text: 192.168.1.1 2. (Name): txtPort Text: 23 3. (Name): txtCommand Text: Enabled: False Font: Courier New, 8pt 4 (Name): txtResponse Text: Enabled: False Font: Courier New, 8pt Scrollbars: Both Buttons: 1. (Name): cmdConnect 2. (Name): cmdSend Enable: False Timer: 1 (Name): timResponse Enable: True
3. Once you have done this, add the following code to the click event of the CONNECT button.
If you try running this, you should get a dialog box, with the bottom two text boxes disabled. If you click on CONNECT they should now be enabled. In part 2 we will add some functionality, because fun though it is, our app doesn't do much at the moment!
Writing a Visual Basic 2008 Telnet Client Part 2I have made an alteration to your program if you are interested. I have it set more for my server, but a few changes that you may be interested are .....
Checking for \n just incase they don't send data in separate lines.<br /> Some error handling so that it tells you if it can't connect.<br /> When it disconnects, it will clear out all the variables so you can re-connect or connect to something new.<br /> BUT MOST IMPORTANTLY ... it works without that timer.
Thank you for this article! it helped me A LOT!!!
Oh and most importantly, my cantact info is xxxxx@xxxxx.com (please delete this so i don't get tooo spammed)
To keep our site free from spam, all comments are moderated prior to publishing. Your comment will be read by one of our staff, and provided it is not blatant advertising, rude or against the spirit of our site, we will do our best to ensure it will appear within one business day. Should you have any specific questions about any item, you should use the Contact page to get in touch instead.