问题描述
我第一次用C#编写一些代码,这需要使用HTTP与网络连接的设备进行对话.不幸的是,设备损坏了,所以我不能使用HttpWebRequest
/HttpWebResponse
类,因此我必须自己使用TcpClient
和NetworkStream
来进行操作.
我有以下代码:
I''m writing some code in C# for the first time which requires talking to a network attached device using HTTP. Unfortunately the device is broken and so I cannot use the HttpWebRequest
/HttpWebResponse
classes, so I''m having to do it myself with TcpClient
and NetworkStream
.
I''ve got the following code:
// make and send the request
byte[] sendBytes = Encoding.ASCII.GetBytes("GET / HTTP/1.1" + Environment.NewLine + Environment.NewLine);
netStream.Write(sendBytes, 0, sendBytes.Length);
// read and display the response
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
int bytesRead = netStream.Read(bytes, 0, tcpClient.ReceiveBufferSize);
Console.Writeline(bytesRead + " bytes read");
string returnData = Encoding.ASCII.GetString(bytes);
Console.Writeline(returnData);
现在我可以肯定地知道,我的回复应该是191个字节长.但是,我在bytesRead
缓冲区中只能得到40个字节.
使它起作用的唯一方法是执行以下操作:
Now I know for certain that my response should be 191 bytes long. However I only get 40 bytes in my bytesRead
buffer.
The only way I can make it work is by doing this:
// make and send the request
byte[] sendBytes = Encoding.ASCII.GetBytes("GET / HTTP/1.1" + Environment.NewLine + Environment.NewLine);
netStream.Write(sendBytes, 0, sendBytes.Length);
// sleep while waiting for a complete response
Thread.Sleep(50)
// read and display the response
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
int bytesRead = netStream.Read(bytes, 0, tcpClient.ReceiveBufferSize);
Console.Writeline(bytesRead + " bytes read");
string returnData = Encoding.ASCII.GetString(bytes);
Console.Writeline(returnData)
显然,这很丑陋,并且对于不同的网络条件并不稳健,因此我想一种避免这种情况的方法.
我已经尝试了诸如在netStream.DataAvailable
上循环并附加任何其他数据之类的各种操作,但这并没有让我更进一步.
除非我有误解,否则NetworkStream
文档中的以下句子暗示这种混乱不是必需的:
可以在NetworkStream
类的实例上同时执行读写操作,而无需同步.只要有一个用于写操作的唯一线程和一个用于读操作的唯一线程,读和写线程之间就不会出现交叉干扰,并且不需要同步.
有人建议替代方案吗?
Clearly this is ugly and is not robust to different network conditions, so I would like a way to avoid it.
I''ve tried various things like looping on netStream.DataAvailable
and appending any further data but this doesn''t get me any further.
Unless I have misinterpreted it, the following sentence from the NetworkStream
documentation implies that this kind of messing around isn''t necessary:
Read and write operations can be performed simultaneously on an instance of the NetworkStream
class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.
Will anybody suggest an alternative?
推荐答案
StreamReader reader = new StreamReader(netStream);
StreamWriter writer = new StreamWriter(netStream);
// make and send the request
writer.Write("GET / HTTP/1.1" + Environment.NewLine + Environment.NewLine);
writer.Flush();
// read and display the response
string returnData = reader.ReadToEnd();
Console.Writeline(returnData)
这篇关于NetworkStream.Write/Read in C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!