如何使用owin httplistener将文件发送给客户端?

时间:2015-08-10 17:14:28

标签: c# owin httplistener

我已经搜索了很多关于从控制台应用程序发送文件的问题,该文件使用自己的HTTP监听器自托管到ASP.NET MVC5上托管的Web应用程序我发现了一个名为{的响应方法{1}},但我不知道如何使用它。这是我的代码:

res.SendFileAsync

这是一个处理HTTP请求的自己的启动类。

1 个答案:

答案 0 :(得分:0)

如果您想向所有客户发送byte[],则有完整的教程here可以满足您的所有需求,基本上可以节省您的时间(这是您应如何发送文件),它应如下所示:

static void Main(string[] args) {
    //---listen at the specified IP and port no.---
    Console.WriteLine("Listening...");
    serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT_NO));
    serverSocket.Listen(4); //the maximum pending client, define as you wish
    serverSocket.BeginAccept(new AsyncCallback(acceptCallback), null);      

    //normally, there isn't anything else needed here
    string result = "";
    do {
        result = Console.ReadLine();
        if (result.ToLower().Trim() != "exit") {
            byte[] bytes = null;
            //you can use `result` and change it to `bytes` by any mechanism which you want
            //the mechanism which suits you is probably the hex string to byte[]
            //this is the reason why you may want to list the client sockets
            foreach(Socket socket in clientSockets)
                socket.Send(bytes); //send everything to all clients as bytes
        }
    } while (result.ToLower().Trim() != "exit");
}

我建议您深入研究该帖子并了解整个过程,看看它是否适合您的解决方案。