TCPListener:如何拒绝连接尝试

编程入门 行业动态 更新时间:2024-10-24 20:12:07
本文介绍了TCPListener:如何拒绝连接尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我正在写一个小型服务器来检查我的程序的更新等。 这很好用,现在我正在尝试制作它更安全一点。我想要实现的是将某些IP地址列入黑名单。 目前我这样做:

Hello everyone, I'm currently writing a small server to check for updates of my programs etc. This works nicely and now I'm trying to make it a little more secure. What I want to implement is a blacklisting of certain IP adresses. Currently I'm doing it like this:

Dim client As TcpClient = listener.AcceptTcpClient blacklist = IO.File.ReadAllLines(Application.StartupPath & _ "\Blacklist.txt") If blacklist.Contains(GetIp(client).ToString) = False Then 'IP not on blacklist, proceed with processing the client Dim t As New Thread(AddressOf HandleClient) Dim argus() As Object = {client, ClientIdentifier} t.Start(argus) threads.Add(t) clients.Add(client) Else 'Client is on blacklist, close the connection tb("Blacklisted IP tries to connect. Closing connection.") client.Close() End If

现在这个工作正常,但令我困扰的是,即使客户端被列入黑名单,也必须接受TCP连接首先,在再次关闭之前建立连接。 我现在的问题是:有没有办法获得有关客户端的信息,然后才能被接受如果客户的IP被列入黑名单,我可以拒绝连接尝试吗?我似乎找不到使用手头工具的方法。 谢谢如果你对我的问题有任何疑问,请随时提出。 祝你好运 Jens

Now this works OK, but what bothers me is, that even though the client is blacklisted the TCP connection has to be accepted first and the connection is established before being closed again. My question now is: Is there any way to get information about the client that is about to be accepted before it is accepted? And can I refuse the connection attempt if the client's IP is blacklisted? I can't seem to find a way with the tools at hand. Thank you in advance for any answers and if you have further questions about my problem, please feel free to ask. Best wishes Jens

推荐答案

不幸的是,如果不首先接受套接字连接就无法做到这一点。你可以尝试获取网络信息关于哪些套接字处于SYN_SEND或SYN_RECEIVED状态,但是如果没有初始化套接字结构(构建TcpClient),则无法知道该连接是否是您正在接受的连接。 Unfortunately there is no way to do this without accepting the socket connection first. You could try getting network information about what sockets are in the SYN_SEND or SYN_RECEIVED state, but its impossible to know if that connection is the one you are in the process of accepting without initializing the socket structure (which the TcpClient is built on).

更多推荐

TCPListener:如何拒绝连接尝试

本文发布于:2023-11-12 18:52:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TCPListener

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!