Community discussions

MikroTik App
 
slimprize
Member Candidate
Member Candidate
Topic Author
Posts: 108
Joined: Thu Aug 09, 2012 2:43 am

VB .net: communicating via router's ip

Fri Aug 17, 2012 1:09 pm

Hi all,

I am trying to use the example from the wiki in a simple vb .net application. I am unable to communicate with the router. I get a host not found error.
My code is below.

Module Module1
'function from http://www.planet-source-code.com/vb/sc ... &lngWId=10
Private Function IPToInteger(ByVal Expression As String) As Integer
Try
Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(Expression)
With IPAddress
Return (System.Convert.ToInt32(.GetAddressBytes(3)) << 24) Or (System.Convert.ToInt32(.GetAddressBytes(2)) << 16) Or (System.Convert.ToInt32(.GetAddressBytes(1)) << 8) Or System.Convert.ToInt32(.GetAddressBytes(0))
End With
Catch ex As Exception
Return 0I
End Try
End Function


Sub Main()
Dim l As Long
l = IPToInteger("192.168.88.1")

Dim mk = New Mikrotik(l)
If Not mk.Login("admin", "pwd") Then
Console.WriteLine("Cant log in")
mk.Close()
Console.ReadLine()
Return
End If

mk.Send("/system/clock/getall", True)
For Each row In mk.Read()
Console.WriteLine(row)
Next
Console.ReadLine()
End Sub

End Module

I have not changed anything in the class from the wiki so am not posting that.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: VB .net: communicating via router's ip

Fri Aug 17, 2012 3:51 pm

first things first:
check if API service port is enabled, it is not blocked by firewall
check if address of router is reachable from your PC

After that you can check with python client to see if everything seems to be good. After that you can start to check with some unknown client to try to connect, authenticate and then do some queries.
 
slimprize
Member Candidate
Member Candidate
Topic Author
Posts: 108
Joined: Thu Aug 09, 2012 2:43 am

Re: VB .net: communicating via router's ip

Sat Aug 18, 2012 3:08 am

first things first:
check if API service port is enabled, it is not blocked by firewall
PL] My system firewall is not prompting me. I am on windows and the windows firewall is silant. The service port is enable on the MikroTik.
check if address of router is reachable from your PC
PL] Yes it. I can access the router without any problems.
After that you can check with python client to see if everything seems to be good.
PL] How do I use the python client? What do I specify on its commandline?
After that you can start to check with some unknown client to try to connect, authenticate and then do some queries.
PL] Winbox works.

I suspect the problem is in my .net code. I do not have a dns server on my local network. This is a home network so I have modified the code to handle a single IP address.

Any further thoughts?
 
ditonet
Forum Veteran
Forum Veteran
Posts: 835
Joined: Mon Oct 19, 2009 12:52 am
Location: Europe/Poland/Konstancin-Jeziorna
Contact:

Re: VB .net: communicating via router's ip

Sun Aug 19, 2012 1:03 am

Winbox works.
API is different service and works on port 8728 (default).
Check this in 'IP/Services'.
In your code IP address (as string) is converted to integer,
Public Sub New(ByVal ipOrDns As String, Optional ByVal port As Integer = -1)
Dim ips = Net.Dns.GetHostEntry(ipOrDns)

tcpCon.Connect(ips.AddressList(0), If(port = -1, 8728, port))
tcpStream = tcpCon.GetStream()
End Sub
Sub New in Mikrotik Class requires ipOrDns As String.
There is also bug in 'Function IPToInteger', IP address is converted in reversed order, eg. 1.88.168.192.

HTH,
 
slimprize
Member Candidate
Member Candidate
Topic Author
Posts: 108
Joined: Thu Aug 09, 2012 2:43 am

Re: VB .net: communicating via router's ip

Sun Aug 19, 2012 2:54 am

[admin@conShield] /ip service> print
Flags: X - disabled, I - invalid
# NAME PORT ADDRESS CERTIFICATE
0 telnet 23
1 ftp 21
2 www 80
3 ssh 22
4 X www-ssl 443 none
5 api 8728
6 winbox 8291

I removed the ip address conversionfunction and now, I am getting an index out of range exception.
An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in RBCmd.exe
Additional information: Index was outside the bounds of the array.

The error is at
tcpCon.Connect(ips.AddressList(0), If(port = -1, 8728, port))
my modified code is


Sub Main()
Dim mk = New Mikrotik("192.168.88.1")
If Not mk.Login("admin", "pwd") Then
Console.WriteLine("Cant log in")
mk.Close()
Console.ReadLine()
Return
End If

mk.Send("/system/clock/getall", True)
For Each row In mk.Read()
Console.WriteLine(row)
Next
Console.ReadLine()
End Sub
 
ditonet
Forum Veteran
Forum Veteran
Posts: 835
Joined: Mon Oct 19, 2009 12:52 am
Location: Europe/Poland/Konstancin-Jeziorna
Contact:

Re: VB .net: communicating via router's ip

Mon Aug 20, 2012 3:00 pm

Use Visual Studio built-in debugger to locate source of error.
Run your project using F8 key, it executes your code line by line.

HTH,
 
slimprize
Member Candidate
Member Candidate
Topic Author
Posts: 108
Joined: Thu Aug 09, 2012 2:43 am

Re: VB .net: communicating via router's ip

Mon Aug 20, 2012 3:30 pm

Hi,

I did do a trace. The problem is at


tcpCon.Connect(ips.AddressList(0), If(port = -1, 8728, port))

What I cannot fully understand is what the following line does.
Dim ips = Net.Dns.GetHostEntry(ipOrDns)

As far as I can tell, the above line is asking for a DNS server which I do not have. I just have one ip address to which I want the stream to connect to.

Pranav
 
ditonet
Forum Veteran
Forum Veteran
Posts: 835
Joined: Mon Oct 19, 2009 12:52 am
Location: Europe/Poland/Konstancin-Jeziorna
Contact:

Re: VB .net: communicating via router's ip

Mon Aug 20, 2012 4:14 pm

Below are my small code modifications:
Sub Main()
        Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse("192.168.88.1")

        Dim mk = New Mikrotik(IPAddress)

        If Not mk.Login("user", "pass") Then
            Console.WriteLine("Cant log in")
            mk.Close()
            Console.ReadLine()
            Return
        End If

        mk.Send("/system/clock/getall", True)
        For Each row In mk.Read()
            Console.WriteLine(row)
        Next
        Console.ReadLine()
    End Sub
Public Sub New(ByVal IPAddr As System.Net.IPAddress, Optional ByVal port As Integer = -1)

            tcpCon.Connect(IPAddr, If(port = -1, 8728, port))
            tcpStream = tcpCon.GetStream()
        End Sub
Now this example works without DNS.

HTH,
 
slimprize
Member Candidate
Member Candidate
Topic Author
Posts: 108
Joined: Thu Aug 09, 2012 2:43 am

Re: VB .net: communicating via router's ip

Mon Aug 20, 2012 4:41 pm

Hello Grzegorz,

Many thanks for the modification. This is exactly what I needed.

Pranav
 
willsmannar
just joined
Posts: 2
Joined: Sun Sep 09, 2012 10:12 pm

Re: VB .net: communicating via router's ip

Sun Sep 09, 2012 10:14 pm

check this tcp/ip communication http://vb.net-informations.com/communic ... amming.htm
connect and pass streams.

wills.

Who is online

Users browsing this forum: caminskibr and 25 guests