Fix bug: Encoding.UTF8 caused Unhandled exception

This commit is contained in:
e.knis 2017-10-09 15:33:58 +07:00
parent db13bfa39c
commit e0866921c3

View file

@ -11,7 +11,7 @@ namespace AsterNET.IO
private TcpClient tcpClient; private TcpClient tcpClient;
private NetworkStream networkStream; private NetworkStream networkStream;
private StreamReader reader; private StreamReader reader;
private StreamWriter writer; private BinaryWriter writer;
private Encoding encoding; private Encoding encoding;
private bool initial; private bool initial;
@ -29,8 +29,7 @@ namespace AsterNET.IO
this.tcpClient = new TcpClient(host, port); this.tcpClient = new TcpClient(host, port);
this.networkStream = this.tcpClient.GetStream(); this.networkStream = this.tcpClient.GetStream();
this.reader = new StreamReader(this.networkStream, encoding); this.reader = new StreamReader(this.networkStream, encoding);
this.writer = new StreamWriter(this.networkStream, encoding); this.writer = new BinaryWriter(this.networkStream, encoding);
this.writer.AutoFlush = true;
} }
#endregion #endregion
@ -47,8 +46,7 @@ namespace AsterNET.IO
this.tcpClient = tcpClient; this.tcpClient = tcpClient;
this.networkStream = this.tcpClient.GetStream(); this.networkStream = this.tcpClient.GetStream();
this.reader = new StreamReader(this.networkStream, encoding); this.reader = new StreamReader(this.networkStream, encoding);
this.writer = new StreamWriter(this.networkStream, encoding); this.writer = new BinaryWriter(this.networkStream, encoding);
this.writer.AutoFlush = true;
} }
#endregion #endregion
@ -156,7 +154,8 @@ namespace AsterNET.IO
/// <summary>connection has already been closed.</summary> /// <summary>connection has already been closed.</summary>
public void Write(string s) public void Write(string s)
{ {
writer.Write(s); writer.Write(encoding.GetBytes(s));
writer.Flush();
} }
#endregion #endregion