저는 C#으로 간단한 포트 스캐너를 만들고 있습니다. 나는 이미 포트 스캐닝 기능을 가지고 있으며 사용자는 사용할 스레드 수와 IP 주소 범위를 전달한다. 따라서 현재 내가 범위를 파싱하고 해당 범위의 모든 IP 주소를 포함하는 ConcurrentBag
을 만들었습니다. 스레드 배열도 있습니다.C#에서 쓰레드와 ConcurrentBag 사용하기
var ipsInRange = IPAddressRange.Parse(range);
Thread[] hostThreads = new Thread[(int)threads];
ConcurrentBag<IPAddress> ips = new ConcurrentBag<IPAddress>();
foreach (var ip in ipsInRange)
{
ips.Add(ip);
}
IPAddress address;
while (!ips.IsEmpty)
{
if (ips.TryTake(out address))
{
PortScanner ps = new PortScanner(address);
ps.Scan();
}
}
// I want to assign each thread one IP address, create a new PortScanner
// instance as above, and scan the IP. I want to delete the scanned IPs
// from the bag. When the thread finishes with scanning, I want to assign
// a new one to it. I want to continue like this until the bag is empty,
// i.e. all the IPs are processed.
// So, simply I want to scan multiple IPs at the same time, using the above
// thread array and the ConcurrentBag containing list of IPs.
비록, 내가 전에 Threads
및 ConcurrentBag
을 사용하지 않은, 그리고 제가 코멘트에 위의 기록 달성하고자 : 그래서, 그것은 부분적으로 다음과 같습니다. 모든 아이디어 어떻게 응용 프로그램에 위의 스레드를 통합 할 수 있습니까?
자신을 교육하는 데 도움이되는 멀티 스레드 네트워크 프로그래밍에 대한 자료를 검색하십시오. –