c# - NAT、wcf、双工通信……需要一些见识

标签 c# .net vb.net wcf client-server

我正在研究做一个双向通信应用程序,不一定是点对点的。看着使用 wcf,我很好奇所有这些的内部工作原理。我最关心的是 NAT/防火墙。

我需要在不对客户端进行端口转发的情况下完成这项工作。我将端口转发服务器,没关系。但是客户端需要能够连接到服务器,然后通过这个现在打开的(NAT 和防火墙)连接来回通信。

我正在研究的双工 wcf 示例似乎都使用向服务器宣布的客户端地址以用于回调。假设这是一个本地局域网地址,将无法用于公共(public)连接。如果是公共(public)地址,是否需要端口转发?

我错过了什么吗?我想建立一个连接,保持打开状态,并以双工方式使用它。因此,一旦客户端“拨出”,就可以通过 NAT 和防火墙限制。

此外,我不太确定 NAT 是如何实现的。如果我在端口 5555 上“打通”到 wcf 服务的连接,并收到回复,来自/到该 wcf 服务地址的端口 5555 是否保留在 NAT 表中?如果我要从服务器 wcf 服务发出进一步的连接,并“连接”到客户端上运行的 wcf 服务(连接到客户端公共(public) ip),NAT 表会知道它是我,并将流量转发到最初打洞的客户?

我的最终实现是:

远程客户端 <-> 端口转发后面的服务器 <-> 管理应用程序向服务器发出命令,服务器将命令中继到远程客户端。

我是不是在尝试使用 wcf 时找错了树?不久前我开始使用原始 tcp 来处理这个问题,但现在再次开始这个项目,我想走 wcf 路线来解决滚动我自己的原始 tcp 通信的所有开销。

感谢您提供的任何见解。

编辑:[PING-PONG] 你好词?

最佳答案

duplex services您可以在服务器和客户端之间进行独立通信。

关于双工服务的注意事项:

The duplex model does not automatically detect when a service or client closes its channel. So if a service unexpectedly terminates, by default the service will not be notified, or if a client unexpectedly terminates, the service will not be notified. Clients and services can implement their own protocol to notify each other if they so choose.

至于 NAT/防火墙设置阻止您的服务工作确实是一个值得关注的问题。但是,您的情况似乎符合客户端和服务器之间典型的双向通信设置。我不会怀疑您在通过一些反复试验后无法正常工作。

If I "punch" out a connection to a wcf service on port 5555, and receive a reply back, does port 5555 from / to that wcf service address stay in the NAT table? If I were to issue further connections from the server wcf service, and "connect" to a wcf service running on the client, (connect to the client public ip), will the NAT table know its me, and forward the traffic on to the client that initially punched the hole?

据我所知,这基本上就是 NAT 的工作原理。只要你的服务器的公共(public) IP 可以在端口 5555 上访问入站/出站流量,你应该没问题。您也许应该在 Server Fault 上对此进行研究或提出问题.

Am I barking up the wrong tree trying to use wcf? I started working on this using raw tcp a while ago, but picking up the project now again, I would like to go the wcf route to sort out all the overhead on rolling my own raw tcp communication.

我不认为你找错了方向。这仅取决于您要实现的目标。 WCF 将为您完成许多繁重的工作,让您专注于应用程序的核心。但是,如果您想了解有关套接字编程的更多信息,那么滚动您自己的网络 API/库将是继续做的事情。

关于c# - NAT、wcf、双工通信……需要一些见识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20164464/

相关文章:

c# - 使用 HttpClient 将字节数组发布到 Web API 服务器

vb.net - 在 VS Designer 上使用 Form(Of T)

sql - MSSQL : single quotes causes to error in queries

c# - 条件 if C# 和 VB 中的差异

mysql - "InvalidOperationException : Connection must be valid and open"在 MySqlConnection 上

c# - 错误 : await requires that the type have a suitable GetAwaiter method

c# - 在 C# .NET 应用程序的整个生命周期中使用 DecisionTree

c# - foreach 循环中的委托(delegate)和调度程序无法访问当前项目

.net - 使用 Blazor 在 Azure 上出现 POST 405(不允许的方法)错误,但在 localhost 上一切正常

c# - WPF Datagrid 对具有空元素的列进行排序