Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ConcurrentBag c获取项目#_C# - Fatal编程技术网

C# 从ConcurrentBag c获取项目#

C# 从ConcurrentBag c获取项目#,c#,C#,我有一个函数如何为我返回ConcurrentBag这个ConcurrentBag包含ip和他的端口。 第一步,他从txt文件中获取ip和端口的值。 他将此值添加到ConcurrentBag。 树他返回一个包含ip和他的端口的ConcurrentBag 这是我的功能: public static ConcurrentBag<ServerSocks> loadSocks() { var result = new ConcurrentBag<S

我有一个函数如何为我返回ConcurrentBag这个ConcurrentBag包含ip和他的端口。 第一步,他从txt文件中获取ip和端口的值。 他将此值添加到ConcurrentBag。 树他返回一个包含ip和他的端口的ConcurrentBag

这是我的功能:

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;
        }
public static ConcurrentBag loadSocks()
{
var结果=新的ConcurrentBag();
字符串fileSocks=Path.GetFullPath(Path.Combine(Application.StartupPath,“socks list.txt”);
var input=File.ReadAllText(fileSocks);
var r=newregex(@“(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})”;
foreach(r.Matches中的匹配(输入))
{
字符串ip=match.Groups[1]。值;
字符串端口=匹配。组[2]。值;
if(result.Any(x=>x.IpAddress.Trim()==ip.Trim())
继续;
Add(新的ServerSocks{IpAddress=ip,Port=Convert.ToInt32(Port)});
}
返回结果;
}
我的问题是main方法,我希望每个循环给我一个新的ip和his端口,我希望将它们影响到属性oServer.socksproxy服务器和oServer.socksproxy端口

这是主方法中的代码:

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]));



            }
ConcurrentBag list=loadSocks();
var oServer=新的SmtpServer(“”);
对于(int i=0;i
在我的txt文件中,我有2个ip 134.34.54.154:17815 173.33.54.157:17815

他总是使用我134.34.54.154,我不知道我的代码中的问题是什么, 如果第一封邮件发送第二封邮件必须使用第二个ip,我应该怎么做


感谢当前的编码方式,您的
oServer.SocksProxyServer=ipAndPort.IpAddress;oServer.SocksProxyPort=ipAndPort.Port始终将被分配socks-list.txt文件中的第一个值。您正确地装满了包,然后从包中一个接一个地取出ip,并将其分配给SocksProxy服务器属性,但在您获得发送邮件代码之前,它会一直覆盖

我想你应该做一些类似的事情:

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);
            }
        } 
        }        
    }
ConcurrentBag list=loadSocks();
var oServer=新的SmtpServer(“”);
对于(int i=0;i

编辑:我想我理解你在这里想要实现的目标。第二次通过For循环时,列表将为空,因为它将从包中删除ip。更改为foreach将允许这些IP下次通过for循环停留在包中。在这种情况下,实际上不需要ConcurrentBag。

请提供一个。这将帮助你自己解决问题。如果您这样做了,但仍然卡住了,您可以回来发布您的MCVE,您尝试了什么,以及结果如何,以便我们能够更好地帮助您。@JeffC我已经编辑了我的代码,您能告诉我是好是坏吗