0

我正在尝试使用套接字和 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();             
}

编辑:

这里什么叫做监听循环:

_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()本文顶部的方法。

4

0 回答 0