• 大小: 8KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: C#
  • 标签: ROS  API  中文  

资源简介

MIKROTIK ROS API C#版,解决中文乱码,支持用户名,中文备注

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Windows.Forms;


namespace ROSAPI
{
    class MK  //ROS API类
    {
        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)//登陆ROS
        {
            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)//发送参数,发送修改命令,操作ROS
        {
            //byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
            byte[] bajty = Encoding.Default.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.Default.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();//读取Stream返回的数据,就是ROS返回的数据
                //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);//返回空,T类output则为加字符串o的值
                    
                    if (o.Substring(0 5) == “!done“)
                    {
                        break;
                    }
                    else
                    {
                        o = ““;
                        continue;
                    }
                    
                }
                else
                {
                    if (tmp[3] < 0x80

评论

共有 条评论