我正在编写一个基于.NET 3.5(C#)的TCP服务器/客户端,但遇到了问题。
每次使用下面的代码传输数据时,只有5个字符传输到服务器。如何修复我的代码,以便传输超过5个字符?
TcpClient client = new TcpClient(connectto.ToString(), portto);
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
Byte[] data = new Byte[256];
data = System.Text.Encoding.ASCII.GetBytes("auth:" + adminPASS.Text);
s.Write(data, 0, data.Length);
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = s.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
服务器只获得传输内容的前5个字符,其余内容丢失。
Byte[256]
的数据,你创建了一个 256 大小的数组,但是当你执行data = System.Text.Encoding...
时,立即将其丢弃,就像你执行了Byte[] data = System.Text.Encoding...
一样。 - Scott Chamberlain