【发布时间】:2018-10-16 03:17:21
【问题描述】:
这是套接字的另一个问题。
我正在用 C# 为游戏制作机器人,到目前为止,我可以让它们进行连接以及类似的操作,但它只是在同一个套接字上同时连接多个帐户。我想我可以通过线程化包含 socket.connect() 函数的函数然后将数据包发送到服务器来做到这一点。我什至查看了过去的 python 脚本,它基本上在做同样的事情。
以下是 Python 代码示例:
if bots["loaded"] < b0ts:
bots["accounts"][acc[0]] = acc[1].replace("\n", "")
bot_thread = threading.Thread(target=self.verify_bot, args=(acc[0],))
bot_thread.start()
基本上我打算尝试将其转换为 C#,认为它会起作用。但另一方面,几乎所有的函数在几乎每种编程语言中都可能不同。
这是我在 C# 中用于线程部分的代码,出于某种原因我使用了后台工作程序,但我也不认为这是它无法正常工作的原因,所以为什么不呢。
Thread t;
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
for(int i = 0; i <= acc.Items.Count - 1; i++)
{
if(backgroundWorker2.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
t = new Thread(() => verify_bots(acc.Items[i].ToString().Split(':')[0], acc.Items[i].ToString().Split(':')[1]));
t.Start();
}
}
}
顺便说一句,拆分是为了用户名和密码,以防万一有人对此感到困惑。无论如何,只有 1 个机器人仍然连接,然后我从套接字收到错误,因为它之前已经通过一个连接或类似的东西连接,或者我认为的其他东西。
这就是我的问题所在,因为它在 python 中运行良好,而且通常我尝试转换为 C# 的东西都有效。这是 verify_bots 函数,以便人们更容易弄清楚。
private void verify_bots(string username, string password)
{
CheckForIllegalCrossThreadCalls = false;
string login_data = string.Format("userID={0}&password={1}", username, password);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(login_data);
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create("https://www.binweevils.com/login");
getTick(request); // getting cookies
request.Method = "POST";
request.KeepAlive = true;
request.UserAgent = "1337JjsHd";
request.CookieContainer = logincookie;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream stream;
stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response;
response = request.GetResponse() as HttpWebResponse;
StreamReader responsereader;
responsereader = new StreamReader(response.GetResponseStream());
string final = responsereader.ReadToEnd();
if(final.Contains("register.php"))
{
console.Text = console.Text + username+" Failed!\n";
}
else
{
string post = "";
ASCIIEncoding encoding1 = new ASCIIEncoding();
byte[] data1 = encoding1.GetBytes(post);
request = (HttpWebRequest)WebRequest.Create("https://lb.binweevils.com/php2/weevil/get-login-details.php");
request.Method = "POST";
request.KeepAlive = true;
request.UserAgent = "1337JjsHd";
request.CookieContainer = logincookie;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data1.Length;
stream = request.GetRequestStream();
stream.Write(data1, 0, data1.Length);
stream.Close();
response = request.GetResponse() as HttpWebResponse;
responsereader = new StreamReader(response.GetResponseStream());
final = responsereader.ReadToEnd();
try
{
string[] a = new string[] { "loginKey=" };
var aa = final.Split(a, StringSplitOptions.None);
logkey = aa[1].ToString();
gethash(@"C:\Python27\hash.py", logkey);
}
catch(Exception)
{
console.Text = console.Text + "Failed to get loginkey.\n";
}
sock.Connect(ip, port);
byte[] recv = new byte[2048];
sendRawPackets(sock, "<policy-file-request/>");
sock.Receive(recv);
sendRawPackets(sock, "<msg t='sys'><body action='verChk' r='0'><ver v='154' /></body></msg>");
sock.Receive(recv);
if(ip.Contains("sfs1"))
{
sendRawPackets(sock, "<msg t='sys'><body action='login' r='0'><login z='Grime'><nick><![CDATA[" + username + "]]></nick><pword><![CDATA["+logkey+"]]></pword></login></body></msg>");
}
else
{
sendRawPackets(sock, "<msg t='sys'><body action='login' r='0'><login z='Slime'><nick><![CDATA[" + username + "]]></nick><pword><![CDATA[" + logkey + "]]></pword></login></body></msg>");
}
sock.Receive(recv);
sendRawPackets(sock, "%xt%login%1#2%-2");
sock.Receive(recv);
sendRawPackets(sock, "%xt%login%2#6%-1%");
sock.Receive(recv);
sendRawPackets(sock, "%xt%login%2#7%17610%69%0,-255,190%");
sock.Receive(recv);
sendRawPackets(sock, "%xt%login%2#4%3150%FlumsFountain%52.9941101744771%0%989.1726320236921%-180%0%190%");
sock.Receive(recv);
string[] move = { "%xt%login%2#1%23%-62%892%66%", "%xt%login%2#1%23%-28%1102%146%", "%xt%login%2#1%23%133%924%-48%", "%xt%login%2#1%23%-39%882%57%", "%xt%login%2#1%23%-39%882%57%", "%xt%login%12#2%23%", "%xt%login%12#2%23%", "%xt%login%2#1%23%54%1002%137%", "%xt%login%2#1%23%-28%934%84%", "%xt%login%2#1%23%144%994%-126%", "%xt%login%2#1%23%84%1118%-177%", "%xt%login%2#1%23%-85%1032%110%", "%xt%login%2#1%23%-34%941%70%", "%xt%login%2#1%23%20%876%17%", "%xt%login%2#1%23%76%951%-79%", "%xt%login%2#1%23%92%999%-133%", "%xt%login%2#1%23%-52%1108%142%", "%xt%login%2#1%23%-92%1045%116%", "%xt%login%2#1%23%19%893%13%", "%xt%login%2#1%23%-84%914%64%", "%xt%login%2#1%23%-19%859%23%", "%xt%login%2#1%23%136%1061%-139%", "%xt%login%2#1%23%91%966%-117%", "%xt%login%2#1%23%63%1086%-156%", "%xt%login%2#1%23%120%1060%-139%", "%xt%login%2#1%23%110%938%-107%", "%xt%login%2#1%23%-84%872%69%", "%xt%login%2#1%23%-117%1045%141%", "%xt%login%2#1%23%-36%961%151%", "%xt%login%2#1%23%72%920%-97%", "%xt%login%2#1%23%117%894%-82%", "%xt%login%2#1%23%32%865%-29%", "%xt%login%2#1%23%-45%889%80%", "%xt%login%2#1%23%-122%1068%141%", "%xt%login%2#1%23%15%1093%-178%", "%xt%login%2#1%23%72%1098%-161%", "%xt%login%2#1%23%83%994%-135%" };
while(true)
{
sendRawPackets(sock, move[new Random().Next(0, move.Length)]); // making bot move around in area
sock.Receive(recv);
Thread.Sleep(5000);
}
}
}
【问题讨论】:
-
你在哪里关闭过套接字?您如何期望在您创建的所有这些线程之间共享相同的套接字。顺便说一句,请阅读
using声明、HttpClient和 StackOverflow 指南,以发布说明问题的最小代码块。 Web 请求代码似乎与您的问题无关。 -
好的,我会这样做,但也要回答你的首要问题,我说我正在查看过去的 python 脚本,基本上是在做其中的事情。 webrequest 垃圾用于验证帐户密码是否正确,然后对提供的密钥进行哈希处理,然后将数据包发送到游戏服务器以将它们实际连接到游戏。
标签: c# multithreading sockets