@@ -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 {
0 commit comments