Answer 1 out of 1
 
45 helpful answers
A:

 

There is no single class or API call that will just do that. You will need to write some code and it depends on what you want to accomplish. Is this UDP or TCP? It this a specific type of service you are looking for? Maybe this particular service has a way to announce itself (like via broadcast or a centric service)? Do you have a list of IP addresses or ranges or you just want to query the entire network(s)?

And do you really need it to be a custom code? There are some excellent applications that can do just that, nmap being one of the most popular. 

If you really need to write your own code to do that, start with .Net's "System.Net.Sockets" namespace, there are classes for raw sockets and TCP/UDP clients there. You will need to do roughly the following:

  • Get a list of IP addresses - either supplied by the caller or build one yourself from your network address and class.
  • For each address, try connect to the specific port (exactly how depends on whether it's TCP or UDP). You will probably want to multi thread this if performance is an issue.
    • For TCP, SYN packet
    • For UDP, just some arbitrary packet (or if you have a specific higher level protocol you are fishing for, a specific constructed packet for that protocol)
  • Wait for response or timeout.
  • Analyze the responses. This will be different for TCP and UDP but very generally:
    • For TCP, you will probably be looking for SYN-ACK response for success
    • For UDP, you will be looking for ICMP messages for failure. absence of response doesn't mean much cause the server might be just dropping the packets.
    • Timeouts mean the port is probably closed in stealth mode or host isn't responding for other reasons.
  • Remember that even if a port is open, this doesn't mean it is open to the exact connection you are expecting (e.g. port 80 is open but listening to incoming SMTP instead of HTTP as expected). You will need to do a real "handshake" with the specific protocol to determine if that is the type or service you are looking for.

Again, all that is very much possible to do on your own but it is easier to do with utilities already tailored for that. Or try searching around for some components/libraries for C# that can do port scanning/sweeping.

 

Helpful?(5)
Rated as Best Answer
 
Comment About This Answer (or add your own answer)

Feed - Subscribe to changes to this Q&A Blog
ADVERTISEMENT
ADVERTISEMENT
AOL Autos Q&A is powered by Yedda an AOL Company
Copyright © 2006-2009, Yedda Inc. and respective copyright owners