TcpListener:连接前拒绝传入的连接请求
我有一项向公共互联网上的所有人开放的服务。它运行 TcpListener 来管理传入连接。
该服务通过传入的 IP 地址维护持续行为不当的客户端列表。当连接关闭时,来自列出的 IP 的任何连接都会发送一条“离开”消息。
如果首先没有打开套接字,但当 AcceptTcpClient 返回时,连接已经打开,我更喜欢它。
有没有办法让我的代码在打开传入连接请求之前介入并检查它们(也许会拒绝它们)?
非常感谢。
I have a service that's open to everyone on the public internet. It runs TcpListener to manage incoming connections.
The service maintains a list of persistently misbehaving clients by their incoming IP address. Any connections coming from a listed IP are sent a "go away" message as the connection is closed down.
I'd prefer it if the socket isn't opened in the first place, but by the time AcceptTcpClient has returned, the connection is already opened.
Is there a way for my code to step in and examine incoming connection requests (and perhaps reject them) before they are opened?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WSAAccept
允许指定一个回调来决定是否接受或拒绝连接。请参阅此问题了解如何从 C# 调用它。WSAAccept
allows specifying a callback which can decide whether to accept or reject a connection. See this question for how to call it from C#.由于您使用的是.Net,我假设您必须在Windows 下运行。
套接字 API 无法实现您想要的功能。但是,您可以让您的程序添加 Windows 防火墙规则来完成相同的操作 - 假设您已打开 Windows 防火墙。
Since you're using .Net I assume you must be running under Windows.
There is no way from the sockets API to do what you seek. However, you could have your program add Windows Firewall rules to accomplish the same thing -- assuming you have Windows Firewall turned on.
对于任何仍在寻找可靠答案的人:
For anyone looking for a solid answer to this still: