TcpListener:连接前拒绝传入的连接请求

发布于 2024-10-25 06:11:34 字数 242 浏览 1 评论 0原文

我有一项向公共互联网上的所有人开放的服务。它运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫罗兰の梦幻 2024-11-01 06:11:34

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#.

裂开嘴轻声笑有多痛 2024-11-01 06:11:34

由于您使用的是.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.

情未る 2024-11-01 06:11:34

对于任何仍在寻找可靠答案的人:

TcpListener inListener;
bool _canClientConnect = true;
TcpClient _client;

if (_inListener != null && _inListener.Pending() && _canClientConnect) { // Accept actual client
    _canClientConnect = false;
    _cancelTokenSource = new CancellationTokenSource();
    _client = _inListener.AcceptTcpClient();
    _stream = _client.GetStream();
    if (_recieverTask != null) {
        _recieverTask.Start();
    }
}
if (_inListener.Pending() && !_canClientConnect) { // Client found connecting, but we are already connected.
    _inListener.AcceptTcpClient().Close(); // Accept the client connection and immediately close it.
}

For anyone looking for a solid answer to this still:

TcpListener inListener;
bool _canClientConnect = true;
TcpClient _client;

if (_inListener != null && _inListener.Pending() && _canClientConnect) { // Accept actual client
    _canClientConnect = false;
    _cancelTokenSource = new CancellationTokenSource();
    _client = _inListener.AcceptTcpClient();
    _stream = _client.GetStream();
    if (_recieverTask != null) {
        _recieverTask.Start();
    }
}
if (_inListener.Pending() && !_canClientConnect) { // Client found connecting, but we are already connected.
    _inListener.AcceptTcpClient().Close(); // Accept the client connection and immediately close it.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文