Skip to content

Commit 9121de0

Browse files
committed
fixe tcp test func
1 parent e44c8c4 commit 9121de0

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

fast cf ip scanner/Services/IPService.cs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22

33
using fast_cf_ip_scanner.Data;
4+
using Microsoft.Maui.Animations;
5+
using System.Collections.Concurrent;
46
using System.Diagnostics;
57
using System.Net;
68
using System.Net.NetworkInformation;
@@ -134,69 +136,75 @@ async Task HttpTest(string ipAddresse)
134136

135137
public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, IpOptionModel ipOptions)
136138
{
137-
138-
var validIp = new List<IPModel>();
139+
var validIp = new ConcurrentBag<IPModel>();
139140

140141
async Task TestConnectionAsync(string ip)
141142
{
142-
143143
var ports = new List<string>();
144+
var stopwatch = new Stopwatch();
144145
var ipIsConnected = false;
146+
var ping = 0;
145147
for (int i = 0; i < ipOptions.CountOfRepeatTestForEachIp; i++)
146148
{
147-
148149
foreach (var port in ipOptions.Ports)
149150
{
150151
try
151152
{
152-
TcpClient tcpClient = new TcpClient();
153-
tcpClient.ConnectAsync(ip, int.Parse(port));
154-
await Task.Delay(ipOptions.MaxPingOfIP);
155-
if (tcpClient.Connected)
153+
using (var tcpClient = new TcpClient())
156154
{
157-
ipIsConnected = true;
158-
ports.Add(port);
155+
stopwatch.Restart();
156+
var connectTask = tcpClient.ConnectAsync(ip, int.Parse(port));
157+
158+
// Wait for either connection or timeout
159+
await Task.WhenAny(connectTask, Task.Delay(ipOptions.MaxPingOfIP));
160+
161+
stopwatch.Stop();
162+
if (tcpClient.Connected)
163+
{
164+
165+
ipIsConnected = true;
166+
ports.Add(port);
167+
168+
// Calculate ping time
169+
ping += Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
170+
// Do something with pingTime if needed
171+
}
159172
}
160173
}
161-
catch
162-
{
163-
164-
}
174+
catch { }
165175
}
166176
}
167177

168178
if (ipIsConnected)
169179
{
170-
lock (validIp)
180+
validIp.Add(new IPModel
171181
{
172-
validIp.Add(new IPModel
173-
{
174-
IP = ip,
175-
Ping = 0,
176-
Ports = string.Join(",", ports)
177-
});
178-
}
182+
IP = ip,
183+
// You can add the ping time here if needed
184+
Ping = ping / (ipOptions.CountOfRepeatTestForEachIp + ports.Count), // Placeholder for now
185+
Ports = string.Join(",", ports)
186+
});
179187
}
180188
}
181-
while (true)
189+
190+
while (validIp.Count < ipOptions.MinimumCountOfValidIp)
182191
{
183-
if (validIp.Count >= ipOptions.MinimumCountOfValidIp)
184-
{
185-
break;
186-
}
187192
var newRandomIps = GetRandomIp(ips, ipOptions.CountOfIpForTest, ipOptions.CountOfIpRanges);
188-
189-
List<Task> tasks = new List<Task>();
193+
var tasks = new List<Task>();
190194

191195
foreach (var ip in newRandomIps)
192196
{
193197
tasks.Add(TestConnectionAsync(ip));
194198
}
195-
await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count));
199+
200+
await Task.WhenAll(tasks);
201+
//await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count));
196202
}
197-
return validIp;
203+
204+
return validIp.ToList();
198205
}
199206

207+
200208
public async Task<List<IPModel>> GetValidIPWithUDPTest(string[] ips, int maxPing)
201209
{
202210

0 commit comments

Comments
 (0)