@@ -38,7 +38,7 @@ public async Task<List<IPModel>> GetIpValid(string[] ips, IpOptionModel ipOption
3838 //case "UDP test":
3939 // validIps = await GetValidIPWithUDPTest(ips, ipOptions);
4040 // break;
41- case "Terminal Ping" :
41+ case "Terminal Ping test " :
4242 validIps = await GetValidIpWithPingTest ( ips , ipOptions ) ;
4343 break ;
4444
@@ -64,9 +64,9 @@ async Task HttpTest(string ipAddresse)
6464 Timeout = TimeSpan . FromSeconds ( ipOptions . MaxPingOfIP ) ,
6565 } ;
6666
67- int totalPing = 0 ;
6867 var ports = new List < string > ( ) ;
69- var ipIsConnected = false ;
68+
69+ var timeoutCount = 0 ;
7070 for ( int i = 0 ; i < ipOptions . CountOfRepeatTestForEachIp ; i ++ )
7171 {
7272 foreach ( var port in ipOptions . Ports )
@@ -80,36 +80,36 @@ async Task HttpTest(string ipAddresse)
8080
8181 if ( result != null )
8282 {
83- ipIsConnected = true ;
84- ports . Add ( port ) ;
83+ if ( ! ports . Any ( p => p == port ) )
84+ {
85+ ports . Add ( port ) ;
86+ }
8587 }
8688 }
8789 catch
8890 {
89- ipIsConnected = false ;
90- stopwatch . Stop ( ) ;
91+ timeoutCount ++ ;
9192 }
92-
93-
94- var currentPing = Convert . ToInt32 ( stopwatch . Elapsed . TotalMilliseconds ) ;
95- totalPing += currentPing ;
9693 }
9794 }
98- int ping = totalPing / ( ipOptions . CountOfRepeatTestForEachIp + ipOptions . Ports . Count ) ;
99- if ( ipIsConnected )
95+ if ( ports . Any ( ) )
10096 {
97+ var totalPing = Convert . ToInt32 ( stopwatch . Elapsed . TotalMilliseconds ) ;
98+ int ping = totalPing / ( ipOptions . CountOfRepeatTestForEachIp * ipOptions . Ports . Count ) ;
10199 lock ( validIp )
102100 {
103101 validIp . Add ( new IPModel
104102 {
105103 IP = ipAddresse . ToString ( ) ,
106104 Ping = ping ,
107- Ports = string . Join ( "," , ports )
105+ Ports = string . Join ( "," , ports ) ,
106+ CountOfTimeout = timeoutCount
108107 } ) ;
109108 }
110109 }
111-
112110 }
111+
112+
113113 #region repeat test
114114 while ( true )
115115 {
@@ -145,8 +145,8 @@ async Task TestConnectionAsync(string ip)
145145 {
146146 var ports = new List < string > ( ) ;
147147 var stopwatch = new Stopwatch ( ) ;
148- var ipIsConnected = false ;
149- var ping = 0 ;
148+ var totolTimeOut = 0 ;
149+ var totalPing = 0 ;
150150 for ( int i = 0 ; i < ipOptions . CountOfRepeatTestForEachIp ; i ++ )
151151 {
152152 foreach ( var port in ipOptions . Ports )
@@ -161,32 +161,40 @@ async Task TestConnectionAsync(string ip)
161161 // Wait for either connection or timeout
162162 await Task . WhenAny ( connectTask , Task . Delay ( ipOptions . MaxPingOfIP ) ) ;
163163
164- stopwatch . Stop ( ) ;
165164 if ( tcpClient . Connected )
166165 {
167-
168- ipIsConnected = true ;
169- ports . Add ( port ) ;
170-
171- // Calculate ping time
172- ping += Convert . ToInt32 ( stopwatch . Elapsed . TotalMilliseconds ) ;
173- // Do something with pingTime if needed
166+ stopwatch . Stop ( ) ;
167+ totalPing += Convert . ToInt32 ( stopwatch . Elapsed . TotalMilliseconds ) ;
168+ if ( ! ports . Any ( p => p == port ) )
169+ {
170+ ports . Add ( port ) ;
171+ }
172+ }
173+ else
174+ {
175+ totolTimeOut ++ ;
174176 }
175177 }
176178 }
177- catch { }
179+ catch {
180+ totolTimeOut ++ ;
181+ }
178182 }
179183 }
180184
181- if ( ipIsConnected )
185+ if ( ports . Any ( ) )
182186 {
183- validIp . Add ( new IPModel
187+ int ping = totalPing / ( ipOptions . CountOfRepeatTestForEachIp * ipOptions . Ports . Count ) ;
188+ lock ( validIp )
184189 {
185- IP = ip ,
186- // You can add the ping time here if needed
187- Ping = ping / ( ipOptions . CountOfRepeatTestForEachIp + ports . Count ) , // Placeholder for now
188- Ports = string . Join ( "," , ports )
189- } ) ;
190+ validIp . Add ( new IPModel
191+ {
192+ IP = ip ,
193+ Ping = ping ,
194+ Ports = string . Join ( "," , ports ) ,
195+ CountOfTimeout = totolTimeOut
196+ } ) ;
197+ }
190198 }
191199 }
192200
@@ -200,8 +208,14 @@ async Task TestConnectionAsync(string ip)
200208 tasks . Add ( TestConnectionAsync ( ip ) ) ;
201209 }
202210
203- await Task . WhenAll ( tasks ) ;
204- //await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count));
211+ for ( int i = 0 ; i < ipOptions . MaxPingOfIP / 100 ; i ++ )
212+ {
213+ await Task . Delay ( 100 ) ;
214+ if ( validIp . Count >= ipOptions . MinimumCountOfValidIp )
215+ {
216+ return validIp . ToList ( ) ;
217+ }
218+ }
205219 }
206220
207221 return validIp . ToList ( ) ;
@@ -280,7 +294,8 @@ async Task TestConnectionAsync(string ipAddress)
280294 {
281295 try
282296 {
283- PingReply reply = pingSender . Send ( ipAddress , ipOptions . MaxPingOfIP ) ;
297+ var reply = await pingSender . SendPingAsync ( ipAddress , ipOptions . MaxPingOfIP ) ;
298+
284299
285300 if ( reply . Status == IPStatus . Success )
286301 {
@@ -307,7 +322,7 @@ async Task TestConnectionAsync(string ipAddress)
307322 {
308323 IP = ipAddress ,
309324 Ping = avgPing ,
310-
325+ CountOfTimeout = totalTimeOut
311326 } ) ;
312327 }
313328 }
@@ -322,10 +337,15 @@ async Task TestConnectionAsync(string ipAddress)
322337 {
323338 tasks . Add ( TestConnectionAsync ( ip ) ) ;
324339 }
325-
326- await Task . WhenAll ( tasks ) ;
340+ for ( int i = 0 ; i < ipOptions . MaxPingOfIP / 100 ; i ++ )
341+ {
342+ await Task . Delay ( 100 ) ;
343+ if ( validIp . Count >= ipOptions . MinimumCountOfValidIp )
344+ {
345+ return validIp . ToList ( ) ;
346+ }
347+ }
327348 }
328-
329349 return validIp . ToList ( ) ;
330350 }
331351
0 commit comments