Tôi đang viết một máy chủ web dưới dạng ứng dụng Nền tảng Windows phổ biến trong C#. Đây là mã của tôi cho đến nay:Tạo một máy chủ web trong 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);
}
}
};
}
}
Tôi đã cố gắng kết nối với máy chủ sử dụng địa chỉ này:
http://127.0.0.1:8000/C:/pathtohtmlfile/htmlfile.html
Tuy nhiên, thời gian kết nối ra. Tôi không chắc chắn nếu nó là một vấn đề với mã C# hoặc với cái gì khác.
Bạn đã xem xét điều này: http://loopback.codeplex.com/? Ứng dụng WinRT và UWP có tính năng bảo vệ vòng lặp và công cụ đó sẽ xóa ứng dụng đó cho các ứng dụng được chỉ định. Có lẽ đây là những gì bạn cần. – Rafael
Trường hợp ngoại lệ vòng lặp chỉ dành cho ổ cắm máy khách –