发送几兆字节后异步文件IO停止

时间:2013-09-04 02:56:34

标签: c# asynchronous async-await filestream networkstream

我正在尝试使用套接字和NetworkStream创建异步TPL文件服务器。在测试时,我的浏览器小HTML文件(1.9 KB)发送得很好,有时甚至是链接发送到的Javascript或CSS文件,但它不会从HTML页面下载更多内容,包括flash,图像,我没有收到任何错误,包括没有连接错误。我可以下载一张96K的图像,但这个数字是关于限制的。我在所有响应标头中设置了Connection: Keep-Alive

有谁知道为什么我的输出流似乎停滞不前?

async Task<> WriteToStream(NetworkStream _networkStream, string filePath, int startingPoint = 0)
    {
    using (FileStream sourceStream = new FileStream(filePath,
        FileMode.Open, FileAccess.Read, FileShare.Read,
        bufferSize: 4096, useAsync: true))
        {
        byte[] buffer = new byte[4096];
        int numRead;
        while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
        {             
             _networkStream.Write(buffer, 0, numRead);
        }
    }
}

我也试过替换它:

_networkStream.Write(buffer, 0, numRead);

用这个:

await _networkStream.WriteAsync(buffer, 0, numRead);

我仍然有同样的问题。

我使用套接字因为我不能使用HttpListener或TcpListener类,因为我需要访问传入的UDP和TCP请求。

我可以使用这种简化方法调用WriteToStream()

private async void SendFileExample()
{
    //This method is only for demonstration, so parameters are hardcoded.

    // Get info and assemble header
    string file = @"C:\www\webpage.html";
    byte[] data = null;
    string responseCode = "200 OK";
    string contentType = "text/html";
    long dataLength = 1901;
    string serverName = "my Stack Overflow server is overflowing with...";

    string header = string.Format("HTTP/1.1 {0}\r\n"
        + "Server: {1}\r\n"
        + "Content-Length: {2}\r\n"
        + "Content-Type: {3}\r\n"
        + "Connection: Keep-Alive\r\n"
        + "\r\n",
        responseCode, serverName, dataLength, contentType);

    var headerBytes = Encoding.ASCII.GetBytes(header);

    //send header
    await _networkStream.WriteAsync(headerBytes, 0, headerBytes.Length);

    //send payload
    await WriteToStream(_networkStream, file, 0);

    //flush networkstream
    await _networkStream.FlushAsync();             
}

编辑:

这里调用listen循环:

_listenTask = Task.Factory.StartNew(() => ListenLoop());

这是一个假脱机请求的循环,每次都产生一个客户端:

private async void ListenLoop()
{
    for (; ; )
    {
        // Wait for connection
        var socket = await _tcpListener.AcceptSocketAsync();
        if (socket == null)
            break;

        // Got new connection, create a client handler for it
        var client = new Client(socket,dbInfo,frmClient);
        // Create a task to handle new connection
        Task.Factory.StartNew(client.Do);
    }
}

通过此方法处理连接:

public async void Do()
{
    byte[] buffer = new byte[4096];
    for (; ; )
    {
        // Read a chunk of data
        int bytesRead = await _networkStream.ReadAsync(buffer, 0, buffer.Length);

        // If Read returns with no data then the connection is closed.
        if (bytesRead == 0)
            return;

        // Write to buffer and process request
        _memoryStream.Seek(0, SeekOrigin.End);
        _memoryStream.Write(buffer, 0, bytesRead);
        bool done = ProcessHeader();
        if (done)
            break;
    }

}

ProcessHeader()主要只获取MIME类型等元数据,然后将流传递到此帖子顶部的WriteToStream()方法。

0 个答案:

没有答案