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