我通过
kestrel
听过两个本地端口,效果很好。
webBuilder
.UseKestrel(opt =>
{
// http request
opt.ListenLocalhost(5000);
// simple tcp communication with stm32-based device
opt.Listen(IPAddress.Parse("192.168.1.2"), 501, opt =>
{
opt.UseConnectionHandler<MyTcpHandler>();
});
})
.UseStartup<Startup>();
});
但是如何向与指定 ip(socket) 连接的设备发送消息
[HttPost("devices/{id}")]
public Task Send(int id, string msg)
{
// convert id to ipendpoint
var ipEndPoint = ipEndPoints[id];
// get socket in kestrel by ipEndPoint
???
// send message
socket.Send(Encoding.UTF8.GetString(msg));
}