Android/Java 对应 C# Stream 对象方法 Write() 的方法是什么?
我要翻译的代码片段如下:
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);
}
A snippet of the code that I want to translate is here:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Stream.Write 的等效项是
OutputStream.write
...但尚不清楚这是否足以帮助您。 (也不清楚为什么您当前的代码使用Encoding.ASCII.GetBytes(co.ToCharArray())
而不仅仅是Encoding.ASCII.GetBytes(co)
The equivalent of
Stream.Write
isOutputStream.write
... but it's not clear whether that will be enough to help you. (It's also not clear why your current code is usingEncoding.ASCII.GetBytes(co.ToCharArray())
instead of justEncoding.ASCII.GetBytes(co)