从ConcurrentBag c#中获取项目
本文关键字:获取 项目 ConcurrentBag | 更新日期: 2023-09-27 18:10:50
我有一个函数如何为我返回ConcurrentBag这个ConcurrentBag包含ip和他的端口。第一步,他从文件txt中获得IP和端口的值。然后将这个值添加到ConcurrentBag中。返回一个包含ip和port的ConcurrentBag,
这是我的函数loadsocks:
public static ConcurrentBag<ServerSocks> loadSocks()
{
var result = new ConcurrentBag<ServerSocks>();
string fileSocks = Path.GetFullPath(Path.Combine(Application.StartupPath, "socks-list.txt"));
var input = File.ReadAllText(fileSocks);
var r = new Regex(@"('d{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3}):('d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
string port = match.Groups[2].Value;
if (result.Any(x => x.IpAddress.Trim() == ip.Trim()))
continue;
result.Add(new ServerSocks { IpAddress = ip, Port = Convert.ToInt32(port) });
}
return result;
}
对我来说,问题是进入方法main,我想为每个循环给我一个新的ip和他的端口,我想影响他们的属性oServer。SocksProxyServer和oServer.SocksProxyPort.
这是main方法中的代码:
ConcurrentBag<ServerSocks> list = loadSocks();
var oServer = new SmtpServer("");
for (int i = 0; i < nRcpt; i++)
{
while (!list.IsEmpty)
{
ServerSocks ipAndPort;
if (!list.TryTake(out ipAndPort)) continue;
try
{
//code here to send message using below IP and Port
oServer.SocksProxyServer = ipAndPort.IpAddress;
oServer.SocksProxyPort = ipAndPort.Port;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
arMail[i] = new SmtpMail("TryIt");
arSmtp[i] = new SmtpClient();
SmtpMail oMail = arMail[i];
oMail.From = "";
//oMail.DKCertificate
oMail.Sender = "";
oMail.Subject = "";
oMail.TextBody = "";
oMail.AutoTextBody = false;
try
{
string fileHtml = Path.GetFullPath(Path.Combine(Application.StartupPath, "lettercmb.html"));
oMail.ImportHtmlBody(fileHtml, ImportHtmlBodyOptions.NoOptions);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
SmtpClient oSmtp = arSmtp[i];
Console.WriteLine(oServer.SocksProxyServer);
Console.ReadLine();
arResult[i] = oSmtp.BeginSendMail(oServer, oMail, null, null);
Console.WriteLine(String.Format("Start to send email to {0} ...",
arRcpt[i]));
}
在我的文件TXT我有2 IP 134.34.54.154:17815173.33.54.157:17815
USE me always 134.34.54.154,我不知道我的代码是怎么出问题的,我想要什么我想如果第一个消息发送第二个消息必须使用第二个IP我该怎么办?
谢谢
当前编码的方式,您的oServer.SocksProxyServer = ipAndPort.IpAddress; oServer.SocksProxyPort = ipAndPort.Port;
总是将被分配到socks-list.txt文件中的第一个值。你把包正确地填满,你把ip一个接一个地从包里拿出来,把它们分配给SocksProxyServer属性,但在你到达你的发送邮件代码之前,它会一直覆盖。
我认为你应该这样做:
ConcurrentBag<ServerSocks> list = loadSocks();
var oServer = new SmtpServer("");
for (int i = 0; i < nRcpt; i++)
{
while (!list.IsEmpty)
{
try
{
//code here to send message using below IP and Port
oServer.SocksProxyServer = list[i%list.count].IpAddress;
oServer.SocksProxyPort = list[i%list.count].Port;
arMail[i] = new SmtpMail("TryIt");
arSmtp[i] = new SmtpClient();
SmtpMail oMail = arMail[i];
oMail.From = "";
//oMail.DKCertificate
oMail.Sender = "";
oMail.Subject = "";
oMail.TextBody = "";
oMail.AutoTextBody = false;
try
{
string fileHtml = Path.GetFullPath(Path.Combine(Application.StartupPath, "lettercmb.html"));
oMail.ImportHtmlBody(fileHtml, ImportHtmlBodyOptions.NoOptions);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
SmtpClient oSmtp = arSmtp[i];
Console.WriteLine(oServer.SocksProxyServer);
Console.ReadLine();
arResult[i] = oSmtp.BeginSendMail(oServer, oMail, null, null);
Console.WriteLine(String.Format("Start to send email to {0} ...",
arRcpt[i]));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
编辑:我想我明白你想要达到的目的。第二次通过For循环时,列表将为空,因为它从包中删除了ip。更改为foreach将允许这些ip在下一次通过for循环时保留在包中。在这种情况下,ConcurrentBag并不需要。