简体   繁体   English

使用相同的NetworkStream读写时出现IOException

[英]IOException when reading and writing with same NetworkStream

I get an IOExection from the following code 我从以下代码获得IOExection

public async Task Register(string handle)
{
    // send register handle
    using (NetworkStream stream = client.GetStream())
    {
        await RegisterHandle(stream);
        var line = "hello world";
        await SendMessage(stream, line);
    }
}

public async Task SendMessage(NetworkStream stream, string message)
{
    Console.WriteLine("SendMessage(" + message + ")");
    await Async.Write(stream, message);
    Console.WriteLine("End of SendMessage");
}

System.AggregateException: One or more errors occurred. (Unable to transfer    data on the transport connection: An established connection was aborted by the software in your host machine.) ---> System.IO.IOException: Unable to transfer data on the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine

What can I do to fix this? 我该怎么做才能解决此问题?

RegisterHandle just writes data then reads back; RegisterHandle只是写数据然后读回; which works fine. 效果很好。 However it fails when writing inside SendMessage. 但是,在SendMessage中写入时失败。

Its actually all explained in detail in the docuemtnation 实际上在文档中都有详细解释

You need to try and check the exception messages, and if you have control over the connecting socket work out what it is closing on you. 您需要尝试检查异常消息,并且如果您对连接套接字有控制权,请找出它正在关闭的状态。 If none of the above you may have a network problem but i would go with the Occam's razor analysis first 如果以上都不是,则可能是网络问题,但我首先要进行Occam的剃刀分析

NetworkStream.Write Method (Byte[], Int32, Int32) NetworkStream.Write方法(字节[],Int32,Int32)

IOException There was a failure while writing to the network. IOException写入网络时失败。

-or- -要么-

An error occurred when accessing the socket. 访问套接字时发生错误。 See the Remarks section for more information. 有关更多信息,请参见“备注”部分。

Remarks 备注

The Write method starts at the specified offset and sends size bytes from the contents of buffer to the network. Write方法从指定的偏移量开始,并从缓冲区的内容向网络发送大小字节。 The Write method blocks until the requested number of bytes is sent or a SocketException is thrown. Write方法将阻塞,直到发送了请求的字节数或抛出SocketException为止。 If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code, and refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error. 如果收到SocketException,请使用SocketException.ErrorCode属性获取特定的错误代码,并参阅MSDN中的Windows套接字版本2 API错误代码文档以获取有关该错误的详细说明。

Note 注意

Check to see if the NetworkStream is writable by accessing the CanWrite property. 通过访问CanWrite属性来检查NetworkStream是否可写。 If you attempt to write to a NetworkStream that is not writable, you will get an IOException. 如果您尝试写入不可写的NetworkStream,则会得到IOException。 If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. 如果收到IOException,请检查InnerException属性以确定它是否是由SocketException引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM