Setting Up .NET and Building a Port Prober - by Paul Zazzarino
For you network folks wanting try your hand at a little programming you can test drive .NET development for free by downloading:
1) .NET Framework 1.1 (similar to Sun's JRE - Java Runtime Environment)
2) .NET Framework Software Development Kit (similar to Sun's JDK Java
Go to Microsoft.com -> Downloads Once you have obtained the files then install the components. Next you then want to set up your environment path to point to the installed path.
Select "My Computer" -> "Properties" or "System Properties", then select "Advanced", then "Environment Variables". Typically all of the .NET tools install in a path named "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322". In order to access the tools from a command prompt you will want to append this path onto your system path.
The SDK comes complete with following language compilers; VB.NET, C Sharp.NET (similar to Java) and JScript.NET. Considering this is all FREE, beyond the initial pain of downloading and installing, this is a pretty good deal. If you still have difficulty. The following book goes into this in a little more detail. It also contains many illustrations of the different network capabilities of .NET. Object Orientated languages have made it easier and quicker to do "sockets" programming. In a few lines you can now accomplish quite alot of work.
C# Network Programming covers:
(Sybex, 647 pages) available from amazon.com for $40
For those of you who want to spend a little more money for roughly $79 from amazon.com or quantumbooks.com, you can purchase one of the following books. Each of these come complete with the full blown version of the .NET 2003 Standard Edtion IDE. This is a real steal, as the exact same retail version sells for $100. You will be able to edit files, build ASP/HTML pages, create and edit XML/XSD, database access, create web services with SOAP, build installation packages with MSI and CAB files etc. etc. Just make sure you get the "Deluxe Learning Edition" of either C#, Visual Basic or C++. Each book also provides 30 plus hands on lessons showing you some of the tools capabilites.
For those of you without any coding experience try VB first. If you have a couple years of experience go with C#. VB used to be the simplest one but since they reinvented the language for .NET it makes less sense with all of the add ons.
With all of that said and done here is a simple port scanner in C# which displays the active TCP servers.
You can cut and paste this with your favorite editor into a file named
class1.cs. If you are running the command line version of the SDK just
simply type in:
Your program will compile. To run it now just type in:
You should get output similar to:
TCP Server on port 1025
TCP Server on port 1026
TCP Server on port 1027
TCP Server on port 1051
TCP Server on port 2268
TCP Server on port 5000
TCP Server on port 7469
The program works by creating an IP address and port pair, what they call in .NET an endpoint. In this case the IP address is irrevelant, we use a builtin value of "Any". The program works by attempting to "bind" or "assert" the IPAddress:Port pair to a socket. If it fails we then get an error/exception. Once this happens we know there is already a server listening on that port.
Now if you wanted to have the program check the UDP ports you would
change one line from this:
Socket ns = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
to the following:
Socket ns = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Which changes the protocol from stream based (TCP) to message based datagrams (UDP). Since multiple protocol servers may be running simultaneously you would probably want to make the program more modular and test for both of them like the following:
Fairly simple, without the piles of code you would write in C or C++. Although what you gain in brevity you can lose in performance with OO languages like VB, C# and Java. The other "trade off" is that C# and VB .NET generate "managed code". (similar to Java's byte code). This means you only can run C# and VB .NET applications on machines which have the .NET Framework installed.
If you run this sample application you may notice the CPU meter or
fan rising and falling. There is a lot of work going on under the covers,
but there are ways to optimize the performance. In particular if you understand
how the native Windows OS prioritizes the network API calls, but that's
for another post.
Paul Zazzarino began programming on Altairs and other microprocessor based devices in the early 80's. This was followed by over a dozen years of tool development and mobile handheld applications in the nineties long before the term "wireless" "802.11" and "mobile computing" were part of the common lexicon. Focusing primarily on CE.NET solutions, Paul develops and evaluates mobile and wireless solutions in a variety of models. Paul extols the virtues of "Fat Clients" with the ability to operate stand-alone and independently buffering and storing data, adjusting connectivity and data delivery based on network availability and access. Paul works as an independent consultant in a B2B format and can be contacted at firstname.lastname@example.org .
Copyright 2005, This page last updated on 02/2005