聊天垃圾邮件自动块用于C#聊天客户端[英] Chat spam auto block for C# chat client

本文是小编为大家收集整理的关于聊天垃圾邮件自动块用于C#聊天客户端的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我真的是C#的新手,所以 目前,我正在研究此LAN Messenger,供学校使用. Messenger本身的代码,包括我在Internet上找到的服务器和客户端,因此我自己还没有对其进行编程.非常基本的代码,因为发送和接收消息已复制.无论如何!我在垃圾邮件中遇到问题.该客户端包含连接选项,以及用于所有传入消息的多行文本框,编写消息的文本框以及发送消息本身的发送按钮.有没有办法阻止人们"垃圾邮件"聊天客户端?例如,如果用户在2秒内发送大约5条消息,则锁定"发送消息"文本框.怎么办?只要它阻止垃圾用户发送更多消息,它就不必锁定文本框. 预先感谢

推荐答案

首先要考虑的是永远不要相信客户端.这意味着任何可以访问您的源代码的人,甚至知道服务器使用的协议,可以编写自己的客户端,该客户端整天发送垃圾邮件消息.

假设垃圾邮件刚刚来自使用您提供的客户端的人,您当然可以计算给定时间表中发送的消息数,并在超过阈值时禁用发送按钮.

假设这是使用winforms,则阻止按钮的代码将是:

btnSend.Enabled = false;

要跟踪最近历史上发送的消息数,您可以创建

之类的东西
List<DateTime> messageTimestamps;

并将时间戳放在其中.

有人发送消息时,请执行此操作:

  • 从列表中删除所有条目,这些条目大于(说)2秒
  • 将新条目添加到列表
  • 如果列表计数>(Say)5,请禁用发送按钮并丢弃此消息

那时,您需要启动一个计时器才能清除阻塞状态.看看

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

看看它的工作原理.

其他推荐答案

可能每次用户发送消息时都会计数计数,如果计数器达到5,请尽一切努力阻止用户发送另一个消息.然后只需用一个计时器对象将计数器重置一次.

int spam = 0;
Timer timer = new Timer(2000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    spam = 0;
}

if (spam < 5)
{
    //send message as usual
    spam++;
}
else
    //notify user that sending messages has been disabled, please wait 'x' seconds to send another message

本文地址:https://www.itbaoku.cn/post/3337533.html

问题描述

I'm really new to C#, so Currently I'm working on this LAN messenger, for use at school. Code for the messenger itself, including server and client I have found on the internet, so I haven't programmed all of it myself. The very basic code, as sending and recieving messages has been copied. Anyways! Im having problems with people spamming the chat. The client contains connecting options, as well as a multiline textbox for all incoming messages, a textbox for writing a message and a send button to send the message itself. Is there a way to stop people from "spamming" the chat client? For example locking the "send message" textbox if a user sends like 5 messages in 2 seconds. How could this be done? It does not have to be locking the textbox, as long as it blocks the spamming user from sending more messages. Thank in advance

推荐答案

The first thing to consider is never trust a client. This means that anyone that has access to your source code, or even knows the protocols that the server uses, can write their own client that sends spam messages all day long.

Assuming the spam is just coming from people using the client you provide, you can certainly count the number of messages sent in a given timeframe and disable the Send button if a threshold is exceeded.

Assuming this is using WinForms, the code to block the button would be something like:

btnSend.Enabled = false;

To keep track of the number of messages sent in recent history, you could create something like

List<DateTime> messageTimestamps;

and put timestamps in there.

When someone sends a message, do this:

  • Remove all entries from the list that are more than (say) 2 seconds old
  • Add the new entry to the list
  • If the list count > (say) 5, disable the Send button and discard this message

At that point you would need to start a timer to clear the blocked state. Have a look at

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

to see how that works.

其他推荐答案

Maybe have a counter count up each time a user sends a message, and if the counter gets to 5, do whatever you want to do to keep the user from sending another message. Then simply reset the counter every 2 seconds with a Timer object.

int spam = 0;
Timer timer = new Timer(2000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    spam = 0;
}

if (spam < 5)
{
    //send message as usual
    spam++;
}
else
    //notify user that sending messages has been disabled, please wait 'x' seconds to send another message