在 IIS 中托管时 TCP 连接被拒绝
TCP connection refused when hosted in IIS
我有一个 ASP.NET 核心网站,它在 8801 端口上侦听正常的 HTTP 请求。该程序还在 8803 端口上侦听 MQTT TCP 连接。
如果我 运行 直接在服务器上使用 Kestrel 的程序如下:
dotnet MyWebsite.dll # runs the command on the server
我的客户端可以成功连接远程8803端口。所以如果我理解正确的话:
- MQTT 服务器在使用 Kestrel 时工作正常。
- 8803端口没有被防火墙禁止
- MQTT TCP 服务器可以接受来自我的客户端的连接。
但是,在 IIS 中托管时(进程内或进程外),TCP 服务器不再工作:
使用 MQTT.fx 时的日志:
2020-08-06 14:14:03,864 INFO --- BrokerConnectorController :
onConnect 2020-08-06 14:14:03,865 INFO --- ScriptsController
: Clear console. 2020-08-06 14:14:03,868 INFO --- MqttFX ClientModel
: MqttClient with ID MQTT_FX_Client assigned. 2020-08-06 14:14:08,100
ERROR --- MqttFX ClientModel : Error when connecting
org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to
server at
org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:94)
~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at
org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at
java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
~[?:1.8.0_181] at java.util.concurrent.FutureTask.run(Unknown Source)
~[?:1.8.0_181] at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(Unknown
Source) ~[?:1.8.0_181] at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown
Source) ~[?:1.8.0_181] at
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[?:1.8.0_181] at
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[?:1.8.0_181] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
Caused by: java.net.ConnectException: Connection refused: connect at
java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
~[?:1.8.0_181] at
java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.doConnect(Unknown
Source) ~[?:1.8.0_181] at
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.connect(Unknown
Source) ~[?:1.8.0_181] at java.net.PlainSocketImpl.connect(Unknown
Source) ~[?:1.8.0_181] at java.net.SocksSocketImpl.connect(Unknown
Source) ~[?:1.8.0_181] at java.net.Socket.connect(Unknown Source)
~[?:1.8.0_181] at
org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:84)
~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] ... 8 more 2020-08-06
14:14:08,102 ERROR --- MqttFX ClientModel : Please verify
your Settings (e.g. Broker Address, Broker Port & Client ID) and the
user credentials! org.eclipse.paho.client.mqttv3.MqttException: Unable
to connect to server at
org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:94)
~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at
org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at
java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
~[?:1.8.0_181] at java.util.concurrent.FutureTask.run(Unknown Source)
~[?:1.8.0_181] at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(Unknown
Source) ~[?:1.8.0_181] at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown
Source) ~[?:1.8.0_181] at
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[?:1.8.0_181] at
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[?:1.8.0_181] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
Caused by: java.net.ConnectException: Connection refused: connect at
java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
~[?:1.8.0_181] at
java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.doConnect(Unknown
Source) ~[?:1.8.0_181] at
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.connect(Unknown
Source) ~[?:1.8.0_181] at java.net.PlainSocketImpl.connect(Unknown
Source) ~[?:1.8.0_181] at java.net.SocksSocketImpl.connect(Unknown
Source) ~[?:1.8.0_181] at java.net.Socket.connect(Unknown Source)
~[?:1.8.0_181] at
org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:84)
~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] ... 8 more 2020-08-06
14:14:08,111 INFO --- ScriptsController : Clear console.
2020-08-06 14:14:08,112 ERROR --- BrokerConnectService :
实际上,我无法使用普通的 tcp 客户端连接到 server:8803。
我正在使用 MQTTnet(3.0.11) + ASP.NET Core(2.1):
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(opts => {
opts.ListenAnyIP(8803, l => l.UseMqtt());
opts.ListenAnyIP(8801);
})
.ConfigureLogging((hostingContext, logging) =>
{
// ...
})
.UseStartup<Startup>();
HTTP 网站工作正常 (8801)。
在 IIS 中托管时,我无法连接到 MQTT 服务器 (8803)。
所以我的问题是:为什么会这样?如何使用 IIS 托管程序?
如果您需要更多信息,请告诉我。
我建议您可以尝试使用其他方式创建 MSQT 服务器,并确保您已打开正确的端口以允许访问。
Startup.cs:
var mqttServerOptions = new MqttServerOptionsBuilder() .WithDefaultEndpointPort(1883) .Build(); services .AddHostedMqttServer(mqttServerOptions) .AddMqttConnectionHandler() .AddConnections() .AddMqttTcpServerAdapter();
我有一个 ASP.NET 核心网站,它在 8801 端口上侦听正常的 HTTP 请求。该程序还在 8803 端口上侦听 MQTT TCP 连接。
如果我 运行 直接在服务器上使用 Kestrel 的程序如下:
dotnet MyWebsite.dll # runs the command on the server
我的客户端可以成功连接远程8803端口。所以如果我理解正确的话:
- MQTT 服务器在使用 Kestrel 时工作正常。
- 8803端口没有被防火墙禁止
- MQTT TCP 服务器可以接受来自我的客户端的连接。
但是,在 IIS 中托管时(进程内或进程外),TCP 服务器不再工作:
使用 MQTT.fx 时的日志:
2020-08-06 14:14:03,864 INFO --- BrokerConnectorController : onConnect 2020-08-06 14:14:03,865 INFO --- ScriptsController
: Clear console. 2020-08-06 14:14:03,868 INFO --- MqttFX ClientModel : MqttClient with ID MQTT_FX_Client assigned. 2020-08-06 14:14:08,100 ERROR --- MqttFX ClientModel : Error when connecting org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:94) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_181] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181] Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_181] at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_181] at java.net.PlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_181] at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:1.8.0_181] at java.net.Socket.connect(Unknown Source) ~[?:1.8.0_181] at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:84) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] ... 8 more 2020-08-06 14:14:08,102 ERROR --- MqttFX ClientModel : Please verify your Settings (e.g. Broker Address, Broker Port & Client ID) and the user credentials! org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:94) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[?:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_181] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181] Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_181] at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?:1.8.0_181] at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_181] at java.net.PlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_181] at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:1.8.0_181] at java.net.Socket.connect(Unknown Source) ~[?:1.8.0_181] at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:84) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?] ... 8 more 2020-08-06 14:14:08,111 INFO --- ScriptsController : Clear console. 2020-08-06 14:14:08,112 ERROR --- BrokerConnectService :
实际上,我无法使用普通的 tcp 客户端连接到 server:8803。
我正在使用 MQTTnet(3.0.11) + ASP.NET Core(2.1):
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseKestrel(opts => { opts.ListenAnyIP(8803, l => l.UseMqtt()); opts.ListenAnyIP(8801); }) .ConfigureLogging((hostingContext, logging) => { // ... }) .UseStartup<Startup>();
HTTP 网站工作正常 (8801)。
在 IIS 中托管时,我无法连接到 MQTT 服务器 (8803)。
所以我的问题是:为什么会这样?如何使用 IIS 托管程序?
如果您需要更多信息,请告诉我。
我建议您可以尝试使用其他方式创建 MSQT 服务器,并确保您已打开正确的端口以允许访问。
Startup.cs:
var mqttServerOptions = new MqttServerOptionsBuilder() .WithDefaultEndpointPort(1883) .Build(); services .AddHostedMqttServer(mqttServerOptions) .AddMqttConnectionHandler() .AddConnections() .AddMqttTcpServerAdapter();