This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22382|Reply: 7

[WinForm] 【High Speed】C# IP address segment port multi-threaded scanner

[Copy link]
Posted on 12/11/2015 3:17:37 PM | | | |

In recent years, RFID readers, smart home gateways, temperature and humidity sensors are often used in work, generally there are serial ports and network ports, due to on-site reasons, serial ports are generally less used, most of them use network ports. The connection method is IP address and port, some devices have search software, some do not. Frequent use of the test environment and the site is inevitably confusing. A recent RFID reader uses a ZN-100 Ethernet port-to-serial port module, and the search software (ZNetCom Utility) is good and the search speed is also very fast. (As shown below)

I thought about developing one myself, first of all, the IP segment and port can be customized, which is easy to use; The search speed must be fast, and I am in a hurry to use multithreading and asynchronous. Let's take a picture first.



  1. #region 扫描方法

  2.         public void Scan(string m_host, int m_port)
  3.         {
  4.             //我们直接使用比较高级的TcpClient类
  5.             TcpClient tc = new TcpClient();
  6.             //设置超时时间
  7.             tc.SendTimeout = tc.ReceiveTimeout = 2000;

  8.             try
  9.             {
  10.                 //同步方法
  11.                   //IPAddress ip = IPAddress.Parse(host);
  12.                 //IPEndPoint IPendp = new IPEndPoint(ip, port);
  13.                 //tc.Connect(IPendp);

  14.                 //异步方法
  15.                 IAsyncResult oAsyncResult = tc.BeginConnect(m_host, m_port, null, null);
  16.                 oAsyncResult.AsyncWaitHandle.WaitOne(1000, true);//1000为超时时间

  17.                 if (tc.Connected)
  18.                 {
  19.                     //如果连接上,证明此端口为开放状态
  20.                        UpdateListBox(listBox1, m_host + ":" + m_port.ToString());
  21.                 }
  22.             }
  23.             catch (System.Net.Sockets.SocketException e)
  24.             {
  25.                 //容错处理
  26.                 //MessageBox.Show("Port {0} is closed", host.ToString());
  27.                 //Console.WriteLine(e.Message);
  28.             }
  29.             finally
  30.             {              
  31.                 tc.Close();
  32.                 tc = null;               
  33.                 scannedCount++;
  34.                 runningThreadCount--;

  35.             }
  36.         }
  37.         #endregion
  38. 如果使用同步方法会有明显的卡顿,用户体验不好;使用异步感觉很流畅,很爽,自己可以注释代码亲测。
Copy code

Source code contributes: IP端口扫描.rar (31.73 KB, Number of downloads: 12)






Previous:Which protocols are TCP-based and which are UDP-based?
Next:void pointer basic
Posted on 12/11/2015 3:51:53 PM |
Posted on 12/11/2015 6:00:48 PM |
Prompt:The post was blocked by an admin or moderator
Posted on 5/22/2016 3:13:38 PM |
It's very good
Posted on 8/20/2016 4:35:02 PM |
It turns out that the owner's scanning proxy software is also improved with reference to this source code. Thank you!
Posted on 8/21/2016 1:11:18 PM |
Why can't this work?
 Landlord| Posted on 12/3/2025 11:58:07 AM |
TCP port connectivity test



 Landlord| Posted on 12/3/2025 11:58:49 AM |
.NET/C# checks if a TCP port is available
https://www.itsvse.com/thread-10721-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com