Using Visual Studio NET

Open Visual Studio .NET, and click New Project. Then type in a name and location for your project (Figure 1.2). Select the Visual Basic Windows application or Visual C Windows application, depending on which language you wish to develop in. When the form appears, right-click on the toolbox and select Customize Toolbox (Visual Studio .NET 2002) or Add Remove Items (Visual Studio .NET 2003). Then select Microsoft Web Browser from the dialog box (as shown in Figure 1.3), and press OK. Visual...

Physical network tapping

Although there would be no conceivable reason for software to read data at this low level, it might be important to know whether the phone line is connected to the computer or not. A program might also want to determine the type of connection the computer has to the Internet. To cite an example, when developing a peer-to-peer network, clients that have a fast connection via a LAN should be given higher weighting in the index server(s) than 56K dial-up connections. This would ensure that new...

Serialization

Serialization is the process by which a .NET object can be converted into a stream, which can easily be transferred across a network or written to disk. This stream can be converted into a copy of the original object through a process called deserialization. The following examples are modeled on a purchase order system. A purchase order is a request to supply goods on a credit basis. The process must be highly bug resilient because an error in purchasing information could cost millions of...

System Web

One of the most common uses of HTTP within applications is the ability to download the HTML content of a page into a string. The following application demonstrates this concept. It is certainly possible to implement HTTP at the socket level, but there is a wealth of objects ready for use in HTTP client applications, and it makes little sense to reinvent the wheel. The HTTP server in the next section is implemented using HTTPWebReqest. Start a new project in Visual Studio .NET, and drag on two...

FTP command reference

Following is a comprehensive list of FTP commands as would be issued by a client. Uploads, where the server chooses the name of the remote file this name is specified in the 250 response Restarts file transfer at a specified position Renames a file (RNFR < old name> ) must be followed by RNTO Renames a file (RNTO < new name> ) must be preceded by RNFR Responds with the current working directory Responds with the contents of the current working directory in human-readable format Responds...

Writing a database to a stream

Most business applications use databases to store their data. In order to transport data from the database across a network, it must be written to a stream. The easiest way of doing this is to serialize the dataset. Note SQL Server and Oracle provide direct network access to their databases and should be used in preference to serialization. Whole books have been written on database programming, and it would be impossible to do the topic justice in this chapter however, a brief overview is...

Implementing SMTP

Before sitting down to code, you should first find out the IP address of your ISP's SMTP server. In the examples below, the SMTP server smtp.ntlworld.com is used. You should replace this with your own SMTP server, or the examples will not work. SMTP was originally designed for UNIX users and has a command-line-type feel to it, although the commands are issued over a network connection, rather than a keyboard. A good way to test the protocol is to open telnet by...

Socketlevel networking in NET

It is often necessary to understand network code written by other developers in order to debug it or adapt it to your own application. After all, no program is ever written without referring to some existing code. This book will consistently use the most concise code possible, but it is important to realize that there are many techniques to implement networked applications in .NET. It is equally important to be able to under_ Chapter 3 stand and recognize these techniques when they are used in...

Implementing a Web server

Start a new Visual Studio .NET project as usual. Draw two textboxes, tbPath and tbPort, onto the form, followed by a button, btnStart, and a list box named lbConnections, which has its view set to list. At the heart of an HTTP server is a TCP server, and you may notice an overlap of code between this example and the TCP server in the previous chapter. The server has to be multithreaded, so the first step is to declare an Array List of sockets public class Forml System.Windows.Forms.Form Public...

Icmp Dim As New Rawsocket

Programming

Ping or, as it is more correctly known, Internet control message protocol ICMP , is a protocol used to report broken network connections or other router-level problems that end hosts might need to know. When a router can't get its packet to the next hop, it discards the packet and sends an ICMP packet back to the sender. ICMP packets are not used to report lost routing problems for other ICMP packets in order to prevent network cascade effects. Many developers are familiar with the ping...

Image compression

Image compression is remarkably similar to audio compression, except that it works in two dimensions rather than one. There may not be the same obvious wave pattern in images, but in digital photographs the natural dithering in shades of color compresses very well when DCT Huffman compression is applied. During the JPEG compression process, the image is split into macrob-locks, or 8x8 blocks of pixels. Each macroblock is then compressed using a two-dimensional DCT to isolate and reduce the...

Case study The Google search engine

Google.com is certainly the Internet's largest search engine. It serves 200 million requests per day and runs from more than 15,000 servers distributed worldwide. It is arguably one of the most scalable Internet services ever provided to the general public. Each server that Google uses is no more powerful than the average desktop PC. Granted, each server crashes every so often, and they are prone to hardware failure, but a complex software failover system is employed by Google to account for...

IP addresses

Csharp Intranet

Every computer that connects directly to the Internet must have a globally unique IP address. An IP address is a four-byte number, which is generally written as four decimal, period-separated numbers, such as 192.168.0.1. Computers that connect indirectly to the Internet, such as via their company network, also have IP addresses, but these do not need to be globally unique, only unique within the same network. To find out what the IP address of your computer is, open a DOS console window and...

Interpreting raw network data

Capturing and interpreting raw network data are totally separate things. Being able to recognize anomalies in the network data is the key to providing a useful tool that could be of real benefit to network managers and administrators. IP-layer packet sniffer application. 70t DQ 7 PLlyijgfFDdf pDDp Xw0 7 3 E amp T PDDi pD vn l 7 nny pnni ngfc-pnM3qi OHO S 70 IHGy PGCIi DgfUPCTSnb qei 70 any gin pcrsjs OU0 7 IDDy PGCIi ginpaSCZ list D0 7 pnyDgicansnpnivco rnra 70 any pcDiLiagucipo -is 70t DQ 7...

Asymmetric encryption

If you imagine a padlock, it consists of a bolt, a key, and a locking mechanism. Each padlock is unique. They all have different keys and different locking mechanisms. The way these padlocks are made in the factory, it is impossible to guess the shape of the key by simply looking at the locking mechanism. It is possible to close the bolt on the padlock without having a key. This makes it much more secure than the previous encryption methods described, which would be more akin to a combination...

Debugging network code

Network connections can and do break, and other applications may be already using the ports you want to use. It is therefore foolhardy to assume that a call to a Connect or Listen method will always succeed. For this reason, the try catch construct should be employed as demonstrated below serverSocket.Bind ipepServer serverSocket.Listen -1 MessageBox.Show e.Message Application.Exit serverSocket.Bind ipepServer serverSocket.Listen -1 catch e as SocketException MsgBox e.Message Catch e as...

Writing a simple TCPIP client

Create a new project as usual, and design a form as shown in Figure 3.3. Name the Send button btnSend, the Browse button btnBrowse, the File textbox tbFilename, and the Server textbox tbServer. Also add an Open File Dialog control named openFileDialog. Click on the Browse button and add the following code private void btnBrowse_Click object sender, openFileDialog.ShowDialog tbFilename.Text openFileDialog.FileName Private Sub btnBrowse_Click ByVal sender As _ System.Object, ByVal e As...

Using RSA as asymmetric encryption

RSA Rivest Shamir Adleman, named after its inventors is implemented in the RSACryptoServiceProvider class. It generates public and private keys on instantiation encryption and decryption are performed from the Encrypt and Decrypt methods. Keys are stored in XML format. Start a new project in Visual Studio .NET. Add two textboxes tbWork-ing and tbStatus. The latter should be set with MultiLine to True. Add two more buttons btnEncrypt and btnDecrypt. To further assist code development, we will...

System NetHttp WebListener

In .NET 2 Whidbey, a more elegant solution for implementing Web servers exists, namely the HttpWebListener class. This class leverages the Http.sys driver where available to deliver unprecedented performance, and integrates many features, such as SSL encryption and authentication, which would be difficult to develop from the ground up. The HttpWebListener class consists of the significant methods and properties shown in Table 4.7. Table 4.7 Significant members of the HttpWebListener class ....

Implementing FTP with the Internet Transfer Control

A full implementation of FTP is quite an undertaking. It may be worthwhile to consider the Microsoft Internet Transfer Control if you need to perform this task. It is a legacy COM control and thus carries a lot of overhead for .NET applications . Native .NET components are available commercially from Dart and IP Works. Having said that, for many applications you don't need an all-singing, all-dancing implementation of FTP to get your job done. If you are writing a feature to an application to...

Plevel network tapping

Network tapping anything that runs at the IP level includes TCP IP and UDP and everything above that, such as DNS, HTTP, FTP, and so forth. At this level, you don't need to use any special software. Everything can be done natively in .NET. To implement a layer 3 network tap in .NET, open a new project in Visual Studio .NET and add a list box named lbPackets and two buttons, btnStart and btnStop. It may be worthwhile to set the font for the list box to Courier for easier reading. After designing...

Writing a simple TCPIP server

Open a new project as before, and design a user interface as depicted in Figure 3.4. The label should be named lblStatus, and the list box, lbConnections. Like the UDP server in a preceding example, the TCP server is multithreaded. In this case, three threads are used the main thread maintains the user interface, a second thread listens for connections, and a third thread handles the connections. One socket is required for each connection and will remain loaded in memory until the connection is...

Implementing a message queue

To run this example, you will need MSMQ running on your computer. In this example, a message will be passed between two computers with an impermanent link between them. If you are on a LAN, you can simulate the dropout in connectivity by unplugging the Ethernet cable for readers with only one computer, the effect can be simulated by running the client and server one after the other not simultaneously . An application not too dissimilar from this example could be used to perform database...

Implementing ZIP compression

It is not necessary to reinvent the wheel when it comes to ZIP compression. Many third-party controls are available for download on the Internet. Some of these are under GPL and, thus, can be redistributed in binary closed-source form, once the license terms, as specified on the publisher's Web site, are adhered to. A good implementation of ZIP in .NET is the ZipLib from www.icsharpcode.net. The following example demonstrates how to compress a file using ZipLib, so it is worthwhile to download...

Writing a simple UDP server

Net Tcp Port Listener

The purpose of the UDP server is to detect incoming data sent from the UDP client. Any new data will be displayed in a list box. Table 3.2 Significant members of the UdpClient class. Initializes a new instance of the UdpClient class. For client UDP applications, this is used as new UdpClient string,int for servers use new UdpClient int . Adds a UdpClient to a multicast group. This may be invoked thus JoinMulticastGroup IPAddress . Returns a UDP datagram that was sent by a remote host. This may...

The 6to4 protocol

The 6to4 mechanism is an elegant solution to ferry IPv6 data across IPv4 networks. It is described definitively in RFC 3056. Although hosts employing 6to4 address assignment would not require any manual configuration, it is likely that 6to4-compliant routers will require some level of configuration. 6to4 generates a global IPv6 address from an IPv4 address by suffixing the IPv4 address with a global address prefix 2002 hex , appending the IPv4 address, and retaining the subnet and interface...

Implementing DNS MX

Open a new project in Visual Studio .NET and draw three textboxes named tbDNSServer, tbDomain, and tbStatus, the latter having multiline set to true. You also require a button named btnFind. Click on the Find button and enter the following code private void btnFind_Click object sender, System.EventArgs e UdpClient dnsClient new UdpClient tbDNSServer.Text , 53 DNSQuery makeQuery DateTime.Now.Millisecond DNSReply dnsClient.Receive ref endpoint this.tbStatus.Text Private Sub btnFind_Click ByVal...

Ping6

There are no prizes for guessing what ping6 does. It is simply an IPv6 implementation of the ping utility. It is a stripped-down version of ping, but it uses IPv6 addresses rather than IPv4 addresses Figure 16.3 . Parameters that are not supported by the Ping6 utility but not necessarily IPv6 C gt netsh interface ipu6 show address WARNING Could not obtain host information from machine HOP . Some commands ma y not be available. flddr Type DAD State Valid Life Pref. Life Address Link Preferred...

An overview of FTP

FTP operates on two ports 21, the control socket, and a data socket, which can exist on port 20 or some other, high port. The definitive description of the protocol is found in RFC 959 at www.ietf.org rfc rfc959-txt. Like the email protocols, the commands that flow between client and server are quite human readable and are broken up into lines, like English text however, it is not feasible to upload or download files using FTP through telnet. If, however, you require a simple batch program to...

Streams for files

Net Import Txt

Start a new Visual Studio .NET Windows application project. Drag an File Open Dialog control onto the form. Name this control openFileDialog. Then add a textbox, to be named tbResults, which should be set with multiline true. Add two buttons to the form, and name them btnReadAsync and btnReadSync. First, we shall implement asynchronous file reading. Press Read Async and enter the following code private void btnReadAsync_Click object sender, callback new AsyncCallback fs_StateChanged fs new...