• 大小: 9KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: C#
  • 标签: RosAPI  (C#)  

资源简介

C# 语言的ROS API,可以实现登录、获取winbox中的信息

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace WindowsFormsApplication
{
    /// 
    /// ROS API(C#)
    /// 

    public class MK
    {
        Stream connection;
        TcpClient con;

        public MK(string ip)
        {
            con = new TcpClient();
            con.Connect(ip 8728);
            connection = (Stream)con.GetStream();
        }
        public void Close()
        {
            connection.Close();
            con.Close();
        }
        public bool Login(string username string password)
        {
            Send(“/login“ true);
            string hash = Read()[0].Split(new string[] { “ret=“ } StringSplitOptions.None)[1];
            Send(“/login“);
            Send(“=name=“ + username);
            Send(“=response=00“ + EncodePassword(password hash) true);
            if (Read()[0] == “!done“)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Send(string co)
        {
            byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
            byte[] velikost = EncodeLength(bajty.Length);

            connection.Write(velikost 0 velikost.Length);
            connection.Write(bajty 0 bajty.Length);
        }
        public void Send(string co bool endsentence)
        {
            byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
            byte[] velikost = EncodeLength(bajty.Length);
            connection.Write(velikost 0 velikost.Length);
            connection.Write(bajty 0 bajty.Length);
            connection.WriteByte(0);
        }
        public List Read()
        {
            List output = new List();
            string o = ““;
            byte[] tmp = new byte[4];
            long count;
            while (true)
            {
                tmp[3] = (byte)connection.ReadByte();
                //if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that 
                //mikrotik send 220 as some kind of “bonus“ between words this fixed things not sure about it though
                if (tmp[3] == 0)
                {
                    output.Add(o);
                    if (o.Substring(0 5) == “!done“)
                    {
                        break;
                    }
                    else
                    {
                        o = ““;
                        continue;
                    }
                }
                else
                {
                    if (tmp[3] < 0x80)
                    {
                        count = tmp[3];
                    }
                    else
                    {
                        if (tmp[3] < 0xC0)
                        {
       

评论

共有 条评论