我最近使用 WPF 和 C# 为 Windows 桌面开发了一个 TCP 客户端-服务器应用程序。 现在,我正在考虑将其移植到 Windows 8 Metro App。
但是,System.Net.Sockets 命名空间似乎不存在于 Metro Apps 的 SDK 中。 (所以我不能使用 TcpListener、TcpClient 等) 我也找不到工作示例。
是否可以将 TCP-IP 服务器创建为 Windows 8 Metro 应用程序(使用一些不同的方法)? 是否有任何第三方库(最好是免费和开源的)可用于完成此操作?
请帮帮我。 提前致谢。
最佳答案
推荐使用StreamSocket类。
这是一个 Example来自 MSDN。
套接字通过 TCP 提供发送和接收,StreamSocketListener 将监听传入的 TCP 连接。
这是我的想法:
首先我们需要一个 StreamSocketListener 的实例。
private StreamSocketListener _listener = new StreamSocketListener();
然后启动监听器连接接收到的事件处理程序并绑定(bind)服务名称。
_listener.ConnectionReceived += listenerConnectionReceived;
await _listener.BindServiceNameAsync("localServiceName");
If the localServiceName parameter is an empty string, then the system will select the local TCP port on which to bind. MSDN
现在我们必须接收连接:
void listenerConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
Console.WriteLine(string.Format("Recive connection from {0}", args.Socket.Information.RemoteHostName.DisplayName));
}
顺便说一句:我为此做了很多研究,没有时间(和 Windows 8 METRO 开发环境)来证明我的想法。希望我能尽快做到这一点。这真的让我很烦。 (德语/英语);)
关于c# - C# 中的 TCP 服务器 Windows 8 XAML 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17342011/