資源簡介
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
評論
共有 條評論