C# 客户端服务器应用程序,BinaryReader 抛出异常
我有一个任务是创建一个服务器应用程序,它检查服务器上是否存在任何文件(其名称必须由客户端发送)以及是否存在以返回响应(消息 - “此文件存在。”)。我对此有问题。服务器正在做正确的事情,但是当我尝试使用 BinaryReader 读取响应时,它给了我这个异常:
Unable to read beyond the end of the stream
这是来自客户端的一些代码: 这
private void OnRequest()
{
try
{
IPEndPoint serverIp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 55555);
TcpClient client = new TcpClient();
client.Connect(serverIp);
if (client.Connected)
{
AddLog("Connected!");
writer = new BinaryWriter(client.GetStream());
writer.Write("request");
GetFileText();
writer.Write(fileText);
writer.Flush();
AddServerResponse(GetResponse(client.Client));
writer.Close();
client.Close();
AddLog("Disconnected!");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
就是 GetResponse 方法:
private string GetResponse(Socket server)
{
NetworkStream stream = new NetworkStream(server);
reader = new BinaryReader(stream);
string message;
do
{
message = reader.ReadString();
}
while (message != null);
reader.Close();
return message;
}
这里是我如何将数据发送到客户:
private void SendDataToClient(string data, Socket client)
{
if (client != null && client.Connected)
{
NetworkStream stream = new NetworkStream(client);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(data);
writer.Flush();
writer.Close();
stream.Close();
}
}
你能帮我一下吗?
I've got task to make a server application, which checks if any file on the server (which name must be sent by the client) exists and if it exists to give back a response (message - "This file exists."). I've got problem with this. The server is making the right thing but when I try to use my BinaryReader to read the response it gives me back this exception:
Unable to read beyond the end of the stream
Here is some code from the client:
private void OnRequest()
{
try
{
IPEndPoint serverIp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 55555);
TcpClient client = new TcpClient();
client.Connect(serverIp);
if (client.Connected)
{
AddLog("Connected!");
writer = new BinaryWriter(client.GetStream());
writer.Write("request");
GetFileText();
writer.Write(fileText);
writer.Flush();
AddServerResponse(GetResponse(client.Client));
writer.Close();
client.Close();
AddLog("Disconnected!");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
And that's the GetResponse method:
private string GetResponse(Socket server)
{
NetworkStream stream = new NetworkStream(server);
reader = new BinaryReader(stream);
string message;
do
{
message = reader.ReadString();
}
while (message != null);
reader.Close();
return message;
}
And here how I'm sending data to the client:
private void SendDataToClient(string data, Socket client)
{
if (client != null && client.Connected)
{
NetworkStream stream = new NetworkStream(client);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(data);
writer.Flush();
writer.Close();
stream.Close();
}
}
Could you help me, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
读取字符串后,您会检测到该消息!= null,因此循环再次运行。但服务器尚未向流写入任何其他内容,因此当您尝试读取下一个字符串时,没有任何内容可读取。每个 ReadString 首先读取字符串长度,然后读取字符串数据(请参阅 http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readstring.aspx)。服务器甚至没有提供可供读取的字符串长度,因此您会收到错误。我认为这就是正在发生的事情。您需要一些指示流的长度或何时到达末尾的指示符。
After you read the string, you detect that message != null and so the loop runs again. But the server has not written anything else to the stream, so when you try to read the next string, there's nothing to read. Every ReadString first reads a string length and then the string data (see http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readstring.aspx). The server hasn't even made a string length available to read, so you get an error. I think that's what's happening. You need some indicator of how long the stream is or when you've reached the end.
我希望流没有提供完整的二进制对象图,因此 BinaryReader 正在尝试反序列化部分对象。
I would expect that the stream hasn't provided the completely binary object graph and so the
BinaryReader
is trying to deserialize a partial object.TCP是面向流的协议,而不是面向消息的协议。这意味着所有内容都保证以正确的顺序到达,而不是全部收到或什么也没有收到。然而,这并不意味着所有内容都会以相同的方式到达。
在尝试阅读之前,您需要确保所有内容均已正确接收。
谷歌一些 NetworkStream 并看看其他人如何在服务器应用程序中使用它。您通常发送标头和数据。
TCP is a stream oriented protocol, not a message oriented protocol. This means that everything is guranteed to arrive in the correct order and than all or nothing is received. However, it do not mean that everything will arrive with the same read.
It's up to you to make sure that everything have been received properly before you attempt to read it.
Google some on NetworkStream and see how others use it in server applications. You normally send a header and than the data.