Skip to content

Commit e725530

Browse files
committed
* Added timeout setting
1 parent c4ff821 commit e725530

7 files changed

Lines changed: 117 additions & 57 deletions

File tree

Advanced PortChecker/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<setting name="AutoUpdate" serializeAs="String">
2323
<value>True</value>
2424
</setting>
25+
<setting name="TimeOut" serializeAs="String">
26+
<value>5000</value>
27+
</setting>
2528
</Advanced_PortChecker.Properties.Settings>
2629
</userSettings>
2730
</configuration>

Advanced PortChecker/Classes/PortChecker.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ internal static class PortChecker
1717
/// <param name="address">The IP address that needs to be scanned.</param>
1818
/// <param name="startPort">The starting point of ports that needs to be scanned.</param>
1919
/// <param name="stopPort">The final port in a range of ports that needs to be scanned.</param>
20+
/// <param name="timeout">The amount of time before the operation cancels.</param>
2021
/// <param name="oi">The operation information regarding this scan.</param>
2122
/// <returns>A list of information regarding the ports and address that was scanned.</returns>
22-
internal static async Task<List<LvCheck>> CheckTCPUDP(string address, int startPort, int stopPort, OperationInformation oi)
23+
internal static async Task<List<LvCheck>> CheckTCPUDP(string address, int startPort, int stopPort, int timeout, OperationInformation oi)
2324
{
2425
List<LvCheck> lv = new List<LvCheck>();
2526
await Task.Run(() =>
@@ -28,8 +29,8 @@ await Task.Run(() =>
2829
{
2930
if (oi.IsCancelled) return;
3031

31-
lv.AddRange(CheckTCP(address, i, i, oi, false).Result);
32-
lv.AddRange(CheckUDP(address, i, i, oi, false).Result);
32+
lv.AddRange(CheckTCP(address, i, i, timeout, oi, false).Result);
33+
lv.AddRange(CheckUDP(address, i, i, timeout, oi, false).Result);
3334

3435
oi.Progress.Report(i);
3536
}
@@ -45,10 +46,11 @@ await Task.Run(() =>
4546
/// <param name="address">The IP address that needs to be scanned.</param>
4647
/// <param name="startPort">The starting point of ports that needs to be scanned.</param>
4748
/// <param name="stopPort">The final port in a range of ports that needs to be scanned.</param>
49+
/// <param name="timeout">The amount of time before the operation cancels.</param>
4850
/// <param name="oi">The operation information regarding this scan.</param>
4951
/// <param name="reportProgress">A boolean to represent whether this method should report the current progress or not.</param>
5052
/// <returns>A list of information regarding the ports and address that was scanned.</returns>
51-
internal static async Task<List<LvCheck>> CheckTCP(string address, int startPort, int stopPort, OperationInformation oi, bool reportProgress)
53+
internal static async Task<List<LvCheck>> CheckTCP(string address, int startPort, int stopPort, int timeout, OperationInformation oi, bool reportProgress)
5254
{
5355
List<LvCheck> lv = new List<LvCheck>();
5456
await Task.Run(() =>
@@ -58,12 +60,12 @@ await Task.Run(() =>
5860
if (oi.IsCancelled) return;
5961

6062
// ReSharper disable once UseObjectOrCollectionInitializer
61-
LvCheck check = new LvCheck()
63+
LvCheck check = new LvCheck
6264
{
6365
Address = address,
6466
Port = i,
6567
Type = "TCP",
66-
Description = IsTcpOpen(address, i) ? "Open" : "Closed"
68+
Description = IsTcpOpen(address, i, timeout) ? "Open" : "Closed"
6769
};
6870
lv.Add(check);
6971

@@ -84,10 +86,11 @@ await Task.Run(() =>
8486
/// <param name="address">The IP address that needs to be scanned.</param>
8587
/// <param name="startPort">The starting point of ports that needs to be scanned.</param>
8688
/// <param name="stopPort">The final port in a range of ports that needs to be scanned.</param>
89+
/// <param name="timeout">The amount of time before the operation cancels.</param>
8790
/// <param name="oi">The operation information regarding this scan.</param>
8891
/// <param name="reportProgress">A boolean to represent whether this method should report the current progress or not.</param>
8992
/// <returns>A list of information regarding the ports and address that was scanned.</returns>
90-
internal static async Task<List<LvCheck>> CheckUDP(string address, int startPort, int stopPort, OperationInformation oi, bool reportProgress)
93+
internal static async Task<List<LvCheck>> CheckUDP(string address, int startPort, int stopPort, int timeout, OperationInformation oi, bool reportProgress)
9194
{
9295
List<LvCheck> lv = new List<LvCheck>();
9396
await Task.Run(() =>
@@ -97,12 +100,12 @@ await Task.Run(() =>
97100
if (oi.IsCancelled) return;
98101

99102
// ReSharper disable once UseObjectOrCollectionInitializer
100-
LvCheck check = new LvCheck()
103+
LvCheck check = new LvCheck
101104
{
102105
Address = address,
103106
Port = i,
104107
Type = "UDP",
105-
Description = IsUdpOpen(address, i) ? "Open" : "Closed"
108+
Description = IsUdpOpen(address, i, timeout) ? "Open" : "Closed"
106109
};
107110
lv.Add(check);
108111

@@ -122,16 +125,19 @@ await Task.Run(() =>
122125
/// </summary>
123126
/// <param name="address">The IP address that needs to be scanned.</param>
124127
/// <param name="port">The port that needs to be scanned.</param>
128+
/// <param name="timeout">The amount of time before the operation cancels.</param>
125129
/// <returns>Returns true if the port is open for connections.</returns>
126-
private static bool IsTcpOpen(string address, int port)
130+
private static bool IsTcpOpen(string address, int port, int timeout)
127131
{
128132
try
129133
{
130134
using (TcpClient tcpClient = new TcpClient())
131135
{
132-
tcpClient.Connect(address, port);
136+
tcpClient.SendTimeout = timeout;
137+
tcpClient.ReceiveTimeout = timeout;
138+
139+
return tcpClient.ConnectAsync(address, port).Wait(timeout);
133140
}
134-
return true;
135141
}
136142
catch (Exception)
137143
{
@@ -145,16 +151,22 @@ private static bool IsTcpOpen(string address, int port)
145151
/// </summary>
146152
/// <param name="address">The IP address that needs to be scanned.</param>
147153
/// <param name="port">The port that needs to be scanned.</param>
154+
/// <param name="timeout">The amount of time before the operation cancels.</param>
148155
/// <returns>Returns true if the port is open for connections.</returns>
149-
private static bool IsUdpOpen(string address, int port)
156+
private static bool IsUdpOpen(string address, int port, int timeout)
150157
{
151158
try
152159
{
153160
using (UdpClient udpClient = new UdpClient())
154161
{
162+
udpClient.Client.ReceiveTimeout = timeout;
163+
udpClient.Client.SendTimeout = timeout;
164+
165+
166+
155167
udpClient.Connect(address, port);
168+
return true;
156169
}
157-
return true;
158170
}
159171
catch (Exception)
160172
{

Advanced PortChecker/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Advanced PortChecker/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
<Setting Name="AutoUpdate" Type="System.Boolean" Scope="User">
1515
<Value Profile="(Default)">True</Value>
1616
</Setting>
17+
<Setting Name="TimeOut" Type="System.Int32" Scope="User">
18+
<Value Profile="(Default)">5000</Value>
19+
</Setting>
1720
</Settings>
1821
</SettingsFile>

Advanced PortChecker/Windows/MainWindow.xaml.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,21 @@ private async void BtnScan_Click(object sender, RoutedEventArgs e)
110110
if (IntStop.Value != null)
111111
{
112112
PgbStatus.Maximum = (double) IntStop.Value;
113+
int timeout = Properties.Settings.Default.TimeOut;
114+
115+
TxtAddress.Text = TxtAddress.Text.Replace("https://", "");
116+
TxtAddress.Text = TxtAddress.Text.Replace("http://", "");
117+
113118
switch (CbaMethod.Text)
114119
{
115120
case "TCP":
116-
await PortChecker.CheckTCP(TxtAddress.Text, (int) IntStart.Value, (int) IntStop.Value, _oI, true);
121+
await PortChecker.CheckTCP(TxtAddress.Text, (int) IntStart.Value, (int) IntStop.Value, timeout, _oI, true);
117122
break;
118123
case "UDP":
119-
await PortChecker.CheckUDP(TxtAddress.Text, (int) IntStart.Value, (int) IntStop.Value, _oI, true);
124+
await PortChecker.CheckUDP(TxtAddress.Text, (int) IntStart.Value, (int) IntStop.Value, timeout, _oI, true);
120125
break;
121126
case "Both":
122-
await PortChecker.CheckTCPUDP(TxtAddress.Text, (int) IntStart.Value, (int) IntStop.Value, _oI);
127+
await PortChecker.CheckTCPUDP(TxtAddress.Text, (int) IntStart.Value, (int) IntStop.Value, timeout, _oI);
123128
break;
124129
}
125130
}

Advanced PortChecker/Windows/SettingsWindow.xaml

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,75 @@
99
AllowsTransparency="True"
1010
UseLayoutRounding="True"
1111
Title="Advanced PortChecker - Settings"
12-
Height="200" Width="300"
12+
Height="300" Width="300"
1313
WindowStartupLocation="CenterScreen" Icon="/Advanced PortChecker;component/globe.ico"
1414
TitleTextAlignment="Center">
15-
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
15+
<Grid>
1616
<Grid.RowDefinitions>
17-
<RowDefinition Height="Auto"></RowDefinition>
18-
<RowDefinition Height="Auto"></RowDefinition>
17+
<RowDefinition></RowDefinition>
1918
<RowDefinition Height="Auto"></RowDefinition>
2019
</Grid.RowDefinitions>
21-
<Grid HorizontalAlignment="Center">
22-
<CheckBox x:Name="ChbAutoUpdate">Automatically check for updates</CheckBox>
23-
</Grid>
20+
<TabControl>
21+
<TabItem Header="General">
22+
<Grid >
23+
<Grid.RowDefinitions>
24+
<RowDefinition Height="Auto"></RowDefinition>
25+
</Grid.RowDefinitions>
26+
<Grid.ColumnDefinitions>
27+
<ColumnDefinition Width="Auto"></ColumnDefinition>
28+
<ColumnDefinition Width="Auto"></ColumnDefinition>
29+
<ColumnDefinition Width="Auto"></ColumnDefinition>
30+
</Grid.ColumnDefinitions>
31+
<Label Content="Connection time-out:"></Label>
32+
<syncfusion:IntegerTextBox x:Name="IntTimeOut" Grid.Column="1" MinValue="1"></syncfusion:IntegerTextBox>
33+
<Label Grid.Column="2" Content="milliseconds"></Label>
34+
</Grid>
35+
</TabItem>
36+
<TabItem Header="Theme">
37+
<Grid>
38+
<Grid.RowDefinitions>
39+
<RowDefinition Height="Auto"></RowDefinition>
40+
<RowDefinition Height="Auto"></RowDefinition>
41+
</Grid.RowDefinitions>
42+
<Grid HorizontalAlignment="Center">
43+
<CheckBox x:Name="ChbAutoUpdate">Automatically check for updates</CheckBox>
44+
</Grid>
45+
<Grid Grid.Row="1">
46+
<Grid.RowDefinitions>
47+
<RowDefinition Height="Auto"></RowDefinition>
48+
<RowDefinition Height="Auto"></RowDefinition>
49+
<RowDefinition Height="Auto"></RowDefinition>
50+
</Grid.RowDefinitions>
51+
<Grid.ColumnDefinitions>
52+
<ColumnDefinition Width="Auto"></ColumnDefinition>
53+
<ColumnDefinition></ColumnDefinition>
54+
</Grid.ColumnDefinitions>
55+
<Label Margin="2" Content="Style:"></Label>
56+
<ComboBox Margin="2" x:Name="ChbStyle" SelectedValuePath="Content" Grid.Row="0" Grid.Column="1" IsReadOnly="True">
57+
<ComboBoxItem>Metro</ComboBoxItem>
58+
<ComboBoxItem>Blend</ComboBoxItem>
59+
<ComboBoxItem>VS2010</ComboBoxItem>
60+
<ComboBoxItem>Office2003</ComboBoxItem>
61+
<ComboBoxItem>Office2007Blue</ComboBoxItem>
62+
<ComboBoxItem>Office2007Black</ComboBoxItem>
63+
<ComboBoxItem>Office2007Silver</ComboBoxItem>
64+
<ComboBoxItem>Office2010Blue</ComboBoxItem>
65+
<ComboBoxItem>Office2010Black</ComboBoxItem>
66+
<ComboBoxItem>Office2010Silver</ComboBoxItem>
67+
<ComboBoxItem>ShinyRed</ComboBoxItem>
68+
<ComboBoxItem>ShinyBlue</ComboBoxItem>
69+
<ComboBoxItem>SyncOrange</ComboBoxItem>
70+
<ComboBoxItem>Transparent</ComboBoxItem>
71+
</ComboBox>
72+
<Label Margin="2" Grid.Row="1" Grid.Column="0" Content="Metro brush:"></Label>
73+
<syncfusion:ColorPicker Margin="2" x:Name="CpMetroBrush" Grid.Row="1" Grid.Column="1" />
74+
<Label Margin="2" Grid.Row="2" Grid.Column="0">Border thickness:</Label>
75+
<syncfusion:IntegerTextBox Margin="2" x:Name="IntBorderThickness" Grid.Row="2" Grid.Column="1" MinValue="0"></syncfusion:IntegerTextBox>
76+
</Grid>
77+
</Grid>
78+
</TabItem>
79+
</TabControl>
2480
<Grid Grid.Row="1">
25-
<Grid.RowDefinitions>
26-
<RowDefinition Height="Auto"></RowDefinition>
27-
<RowDefinition Height="Auto"></RowDefinition>
28-
<RowDefinition Height="Auto"></RowDefinition>
29-
<RowDefinition Height="Auto"></RowDefinition>
30-
</Grid.RowDefinitions>
31-
<Grid.ColumnDefinitions>
32-
<ColumnDefinition Width="Auto"></ColumnDefinition>
33-
<ColumnDefinition></ColumnDefinition>
34-
</Grid.ColumnDefinitions>
35-
<Label Margin="2" Content="Style:"></Label>
36-
<ComboBox Margin="2" x:Name="ChbStyle" SelectedValuePath="Content" Grid.Row="0" Grid.Column="1" IsReadOnly="True">
37-
<ComboBoxItem>Metro</ComboBoxItem>
38-
<ComboBoxItem>Blend</ComboBoxItem>
39-
<ComboBoxItem>VS2010</ComboBoxItem>
40-
<ComboBoxItem>Office2003</ComboBoxItem>
41-
<ComboBoxItem>Office2007Blue</ComboBoxItem>
42-
<ComboBoxItem>Office2007Black</ComboBoxItem>
43-
<ComboBoxItem>Office2007Silver</ComboBoxItem>
44-
<ComboBoxItem>Office2010Blue</ComboBoxItem>
45-
<ComboBoxItem>Office2010Black</ComboBoxItem>
46-
<ComboBoxItem>Office2010Silver</ComboBoxItem>
47-
<ComboBoxItem>ShinyRed</ComboBoxItem>
48-
<ComboBoxItem>ShinyBlue</ComboBoxItem>
49-
<ComboBoxItem>SyncOrange</ComboBoxItem>
50-
<ComboBoxItem>Transparent</ComboBoxItem>
51-
</ComboBox>
52-
<Label Margin="2" Grid.Row="1" Grid.Column="0" Content="Metro brush:"></Label>
53-
<syncfusion:ColorPicker Margin="2" x:Name="CpMetroBrush" Grid.Row="1" Grid.Column="1" />
54-
<Label Margin="2" Grid.Row="2" Grid.Column="0">Border thickness:</Label>
55-
<syncfusion:IntegerTextBox Margin="2" x:Name="IntBorderThickness" Grid.Row="2" Grid.Column="1" MinValue="0"></syncfusion:IntegerTextBox>
56-
</Grid>
57-
<Grid Grid.Row="2">
5881
<Grid.ColumnDefinitions>
5982
<ColumnDefinition></ColumnDefinition>
6083
<ColumnDefinition></ColumnDefinition>

Advanced PortChecker/Windows/SettingsWindow.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private void LoadSettings()
3737
{
3838
try
3939
{
40+
IntTimeOut.Value = Properties.Settings.Default.TimeOut;
4041
ChbAutoUpdate.IsChecked = Properties.Settings.Default.AutoUpdate;
4142

4243
ChbStyle.SelectedValue = Properties.Settings.Default.VisualStyle;
@@ -53,6 +54,7 @@ private void BtnSave_Click(object sender, RoutedEventArgs e)
5354
{
5455
try
5556
{
57+
if (IntTimeOut.Value != null) Properties.Settings.Default.TimeOut = (int) IntTimeOut.Value;
5658
if (ChbAutoUpdate.IsChecked != null) Properties.Settings.Default.AutoUpdate = ChbAutoUpdate.IsChecked.Value;
5759
Properties.Settings.Default.VisualStyle = ChbStyle.Text;
5860

0 commit comments

Comments
 (0)