web服務器我寫一個Web服務器在C#通用Windows平臺的應用程序。這是我到目前爲止的代碼:創建在C#UWP
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#代碼還是其他問題。
你看這個:http://loopback.codeplex.com/? WinRT和UWP應用程序具有環回保護,該工具將爲指定的應用程序刪除它。也許這是你需要的。 – Rafael
環回例外是客戶端套接字只不幸 –