Я пишу веб-сервер в качестве приложения для универсальной платформы Windows на C#. Вот мой код до сих пор:Создание веб-сервера в 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
Исключения для Loopback для клиентских сокетов, к сожалению, –