Implementing SMTP
SMTP operates on TCP port 25. 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...
IPv6 coexistence
IPv6 will not replace IPv4 overnight. Instead, there will be a considerable period when both IPv6 and IPv4 will coexist on the Internet. In time, IPv4 will be phased out entirely. This transitional period is important for developers of mass-market software where the network infrastructure of the enduser can be one of several different hybrid systems. IPv6 deployment will take place in a much more organized way than the MBONE project did. The ad hoc deployment of MBONE created islands of...
Vbnet Wex
Private Sub btnSend_Click ByVal sender As System.Object, _ ByVal e As System.EventArgs Handles btnSend.Click Dim msg As Byte If Not Socket.SupportsIPv6 Then MessageBox.Show Cannot support IPv6 Return End If Dim ipAddrv6 As IPAddress _ IPAddress.Parse tbIPv6Addr.Text Dim ipEPv6 As IPEndPoint New IPEndPoint ipAddrv6, port Dim ipv6Socket As Socket ipv6Socket New Socket _ AddressFamily.InterNetworkV6, SocketType.Stream, _ ProtocolType.Tcp ipv6Socket.Connect ipEPv6 msg Encoding.ASCII.GetBytes...
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...
Layer 2 network tapping
When you tap into sniff network traffic at level 2, you receive not only data from other applications on your computer, but also from other applications on different computers that are on the same network. Furthermore, you get more than just IP traffic you start to see ARP requests, NETBIOS packets, and many other weird and wonderful inhabitants of the network, and they all come complete with frame headers. WinPCap is, in essence, a driver that enables you to read packets directly from a...
The FTP handshake
In the same way, FTP uses a basic authentication mechanism It accepts a username and password in plain text, which can be seen by anyone using a protocol analyzer at any point between the client and the server. FTP over SSL is recommended when a Web site carries information of substantial value. An FTP server may allow anonymous access. This is where the username is set to anonymous and the password can be anything. This is the default setup of the Microsoft FTP service. When you connect to an...
Introduction Hqn
You can't always expect your customer to have the same bandwidth as your office LAN. Huge numbers of people still use modem connections, and some use mobile GPRS devices with even lower connection speeds. These customers will only buy your software if it works at a speed that is at least usable and does not frustrate them. Online services with slow loading times will infuriate casual Web users and drive away potential customers. Conversely, people will pay more for better performance. To give...
Icmp Dim As New Rawsocket
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...
Listening for incoming phone calls
You can only do a certain number of things with a modem by sending commands back and forth via a COM port. In order to develop serious applications, you have to use the Telephony Application Programming Interface TAPI . The TAPI libraries were designed with C in mind, not .NET, so there is a steep learning curve. It is worthwhile to evaluate the various commercial components available before tinkering with low-level TAPI code. A few interesting Web sites, such as www.shrinkwrapvb.com and...
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...
IPv6 naming conventions
With Ipv4 addresses, it was practical to write each byte in decimal format, such as 195.233.254.33 however, with a 128-bit address, it becomes awkward to write 16 three-digit numbers to represent a single IP address. Therefore, a new naming convention is used to quote IPv6 addresses. IPv6 addresses are quoted in hexadecimal, not decimal. They are broken into 16bit segments, rather than 8 bits, as was the case with IPv4. Therefore, one would write FFFF FFFF rather than 255.255.255.255. To...
Acknowledgments 1
Most of the work done by MSMQ is behind the scenes and completely transparent to the application. If MSMQ fails for some reason, the application and therefore the user will not know that today's data was never transferred. Acknowledgments provide a mechanism for the sending application to verify that the receiving application has read the message and that the message queue is functioning correctly. This example builds on the code for the first example in this chapter, so open that project in...
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
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...
The 6over4 protocol
6over4 and 6to4 are similar protocols, but they are different in some significant respects. They are both used to ferry IPv6 data over IPv4 networks however, 6over4 is designed for use in multicast environments, unlike 6to4 or ISATAP. 6over4 is defined definitively in RFC 2529. 6over4 is disabled by default on Windows XP, but it can be enabled with the command line substituting the IPv4 address with that of the local computer netsh interface ipv6 add 6over4tunnel 6over4 lt ipv4 address gt From...
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...
Vbnet Vna
Imports System.Net Imports System.Net.Sockets Imports System.Text To test this example, run the server from Visual Studio .NET. Find the IPv6 address of the server computer, using the Ipv6 if command. If you are using only one computer, then this is FE80 1. Now run the client program, enter the server IPv6 address into the box provided, and type a message into the second box. Press Send, and the message should appear in the text window of the server, as depicted in Figure 16.7.
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...
DTMF tones
Dual-tone modulated frequency DTMF is a way of encoding a number into an audible sound composed of two sine waves played simultaneously. These sounds are generated when someone presses a digit on a phone's keypad. This is particularly useful for automated phone conversations, such as Press 1 if you have a billing inquiry. Press 2 if you require technical support, and so on. These sounds are decoded by the modem hardware and passed up to the TAPI callback as an event with dwMessage set to...
Audio playback
Playing audio back through a voice modem is the core feature of any CTI system. The following example demonstrates how to send a prerecorded wave file as audio to a standard telephone handset. Using prerecorded messages should be adequate in most situations, where even dynamic data such as times, dates, and prices can be composed of snippets of audio like one, two, three, four, thirteen, teen, twenty, thirty, fourty, etc. When recordings are so varied that it would be impossible to prerecord...
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...
SystemNetHttpWebListener
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 ....
Vbnet Vxr
Public Sub ListenerThread port 8889 Dim tcplistener As TcpListener New TcpListener port reportMessage Listening on port port.ToString tcplistener.Start Do Dim webproxy As WebProxy New WebProxy webproxy.UserInterface Me webproxy.clientSocket tcplistener.AcceptSocket reportMessage New client Dim thread As Thread New Thread New ThreadStart _ AddressOf webproxy.run thread.Start Loop End Sub A utility function that is used throughout the application is reportMessage. Its function is to display...
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...
Introduction Vfw
Until now, we have assumed that hackers use network-sniffing software to intercept confidential data however, there is as much danger in forged or spoofed data. Chapter 5 on SMTP POP3 demonstrates how the sender can specify the originating email address arbitrarily, making it easy to send an email that appears to have come from someone else's account. One can imagine the havoc this would cause if a student were to send an email purporting to be from a professor saying, All lectures have been...
IPlevel 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
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
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...












