【发布时间】:2016-02-07 03:32:46
【问题描述】:
我正在用 C# 编写一个 Web 服务器作为通用 Windows 平台应用程序。到目前为止,这是我的代码:
sealed partial class App : Application
{
int port = 8000;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
StartServer();
}
private void StartServer()
{
StreamSocketListener listener = new StreamSocketListener();
listener.BindServiceNameAsync(port.ToString());
Debug.WriteLine("Bound to port: " + port.ToString());
listener.ConnectionReceived += async (s, e) =>
{
Debug.WriteLine("Got connection");
using (IInputStream input = e.Socket.InputStream)
{
var buffer = new Windows.Storage.Streams.Buffer(2);
await input.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial);
}
using (IOutputStream output = e.Socket.OutputStream)
{
using (Stream response = output.AsStreamForWrite())
{
response.Write(Encoding.ASCII.GetBytes("Hello, World!"), 0, 1);
}
}
};
}
}
我尝试使用此地址连接到服务器:
http://127.0.0.1:8000/C:/pathtohtmlfile/htmlfile.html
但是,连接超时。我不确定这是 C# 代码的问题还是其他问题。
【问题讨论】:
-
你看过这个:loopback.codeplex.com 吗? WinRT 和 UWP 应用具有环回保护,该工具将为指定的应用删除它。也许这就是你所需要的。
-
不幸的是,环回异常仅适用于客户端套接字