 Network Programming in .NET Metzger,  Debugging by Thinking , ISBN 1-55558-307-5, 600pp, 2003Mosher,  Microsoft Outlook Programming: Jump Start for Administrators, Developers ,  and Power Users , ISBN 1-55558-286-9, 624pp, 2002Lawrence,  Compaq Visual Fortran: A Guide to Creating Windows Applications , ISBN 1-55558-249-4, 468pp, 2002Breakﬁeld & Burkey,  Managing Systems Migrations and Upgrades: Demystifying the Technology Puzzle , 320pp, ISBN 1-55558-256-7, 2002For more information or to order these and other Digital Presstitles, please visit our website at www.bh.com/digitalpress!At www.bh.com/digitalpress you can:•Join the Digital Press Email Service and have news aboutour books delivered right to your desktop•Read the latest news on titles•Sample chapters on featured titles for free•Question our expert authors and editors•Download free software to accompany select texts Network Programming in .NET With C# and Visual Basic .NET Fiach Reid AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO• SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Elsevier Digital Press200 Wheeler Road, Burlington, MA 01803, USALinacre House, Jordan Hill, Oxford OX2 8DP, UKCopyright © 2004, Elsevier Inc.  All rights reserved.No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail: permissions@elsevier.com.uk.  You may also complete your request on-line via the Elsevier homepage (http://elsevier.com), by selecting  “Customer Support” and then “Obtaining Permissions.”Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data Application submitted.ISBN: 1-55558-315-6 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library.For information on all Digital Press publications visit our Web site at www.digitalpress.com and www.bh.com/digitalpress04  05  06  07  08  09  10  9  8  7  6  5  4  3  2  1Printed in the United States of America To my parents, thank you for everything.This page intentionally left blank  vii Contents Prefacexv Who should read this book?xvWhat hardware and software do you need?xviHow this book is organizedxviPart I: Basic network applications xviPart II: Network application design xviPart III: Specialized networking topics xviiConventions used in this bookxviiFurther informationxviii Acknowledgmentsxix1Understanding the Internet and Network Programming 1 1.1Introduction11.2Why network programming in .NET?21.3What can a network program do?21.4IP addresses31.5The network stack61.6Ports71.7Internet standards71.8What is .NET?91.9Getting started111.10Using Visual Studio .NET121.11Using the .NET SDK161.11.1Compiling with Visual Basic.NET 191.11.2Compiling with C#  201.11.3Testing the application 201.12Conclusion20 viiiContents 2I/O in the .NET Framework21 2.1Introduction212.2Streams212.2.1Streams for ﬁles 222.2.2Encoding data 282.2.3Binary and text streams 292.2.4Serialization 332.2.5Writing a database to a stream 442.3Conclusion54 3Working with Sockets 55 3.1Introduction553.2What is a socket?553.3Creating a simple “hello world” application563.3.1Writing a simple UDP client 573.3.2Writing a simple UDP server 583.4Using TCP/IP to transfer ﬁles623.4.1Writing a simple TCP/IP client 623.4.2Writing a simple TCP/IP server 653.5Debugging network code733.6Socket-level networking in .NET753.7Conclusion86 4HTTP: Communicating with Web Servers87 4.1Introduction874.1.1Data mining 884.2HTTP884.2.1The HTTP request 884.2.2The HTTP response 914.2.3MIME types 934.2.4System.Web  934.2.5Posting data 974.2.6A note on cookies 1044.2.7A WYSIWYG editor 1054.3Web servers1134.3.1Implementing a Web server 1144.4System.Net.HttpWebListener1244.5Mobile Web browsers1284.5.1Mobile Web SDK 1304.6Conclusion130 ContentsixContents 5SMTP and POP3: Communicating with email Servers131 5.1Introduction1315.2Sending an email1315.3SMTP1325.3.1Implementing SMTP  1335.4Post ofﬁce protocol 31405.4.1Implementing POP3 1415.5System.Web.Mail1485.5.1Attachments 1515.5.2Images 1535.6Mail application programming interface1535.6.1Accessing the address book 1565.6.2IMAP 1585.6.3Network news transfer protocol 1595.7Conclusion161 6FTP: Communicating with File Servers163 6.1Background1636.2Microsoft ﬁle sharing1636.3Netware ﬁle sharing1646.4An overview of FTP1656.4.1How FTP uses ports 1676.4.2The FTP handshake 1686.4.3Navigating folders 1706.4.4FTP command reference 1716.4.5Implementing FTP 1726.4.6Implementing FTP with the Internet Transfer Control 1746.4.7A more substantial implementation of FTP 1786.4.8FTP support in .NET 2.0 1936.5Conclusion194 7Securing a Network: Firewalls, Proxy Servers, and Routers   195 7.1Introduction1957.1.1Building a network from scratch 1957.2Building an enterprise network1997.2.1Routers 1997.2.2Firewalls 2007.3Tunneling out of an enterprise network203 xContents 7.4Avoiding the networking pitfalls2057.4.1Firewall tunneling 2067.5Conclusion207 8Protecting Data: Encryption209 8.1Introduction2098.2Cryptanalysis2098.3Terminology2128.4Asymmetric encryption2128.5Using RSA as asymmetric encryption2138.6Symmetric encryption2188.6.1Using 3DES as symmetric encryption 2188.7Piracy protection2248.8Conclusion225 9Controlling User Access: Authentication and Authorization227 9.1Introduction2279.2Authentication techniques2279.2.1IIS authentication 2289.3Microsoft .NET Passport authentication2309.4Hashing information2329.4.1Hashing algorithms 2349.4.2Using SHA  2349.5SSL2369.6Certiﬁcates2369.7Server certiﬁcates 2389.8Client certiﬁcates2399.8.1Microsoft Certiﬁcate Services 2409.8.2Reading certiﬁcates 2419.9Permissions in .NET2449.10Financial network security2469.10.1X.25 2479.10.2ISO 8730  2479.10.3SWIFT 2489.10.4Corporate transactions  2489.11Conclusion249 ContentsxiContents 10Programming for Scalability251 10.1Introduction25110.2Case study: The Google search engine25110.3Replication and redundancy25310.4Scalable network applications25410.5Future prooﬁng25510.6Thread pooling25610.6.1Implementing a thread pool 25810.7Avoiding deadlocks26110.8Load balancing26210.9Conclusion272 11Optimizing Bandwidth Utilization275 11.1Introduction27511.2Tricks and tips to increase performance27511.2.1Caching 27611.2.2Keep-alive connections 27711.2.3Progressive downloads 27811.2.4Tweaking settings 27811.3Multicast UDP28211.3.1Multicast basics 28211.3.2Multicast routing 28311.3.3Implementing multicast 28411.4Data compression28911.5Lossless compression29011.5.1Implementing ZIP compression 29111.6Lossy compression29611.6.1Audio compression 29611.6.2Image compression 29811.6.3Video compression 30211.7Conclusion303 12Ping, DNS, and WHOIS: Monitoring your Network305 12.1Introduction30512.2DNS30512.2.1Implementing DNS MX 30612.3Ping31412.4WHOIS32112.4.1Telnet 326 xiiContents 12.5Other members of the TCP/IP suite32712.5.1ARP 32712.5.2RIP 32712.5.3OSPF 32812.5.4BGP/EGP 32812.5.5SNMP 32812.5.6PPP 32812.6WMI32912.6.1Reading WMI data 33012.6.2Leveraging WMI 33312.7Conclusion336 13Analyzing Network Packets337 13.1Introduction33713.2IP-level network tapping33913.2.1Interpreting raw network data 34413.2.2IP packets in detail 34613.2.3ICMP packets in detail 34813.2.4TCP/IP packets in detail 34913.2.5UDP packets in detail 35113.2.6DNS packets in detail 35213.3Layer 2 network tapping35413.3.1Using rvPacket and WinPCap 35413.3.2Using PacketX and WinPCap 36013.4Physical network tapping36613.5Conclusion376 14Adding Digital Telephony379 14.1Introduction37914.2Basic telephony38014.3Listening for incoming phone calls38214.4DTMF tones39914.5       Audio playback40114.5.1Audio playback over TAPI 41314.6Conclusion417 15Message Queues419 15.1Introduction41915.2MSMQ420 ContentsxiiiContents 15.3Implementing a message queue42015.3.1Queuing complex objects 42715.3.2Transactions 43515.3.3Acknowledgments 43715.4Timeouts43915.5Journal 44115.6Queued Components44315.7Security44715.8Scalability44915.9Performance issues45115.10Conclusion452 16IPv6: Programming for the Next-generation Internet 453 16.1Introduction45316.2What is IPv6?45316.3The history of IPv645416.4So what changes?45516.5IPv6 naming conventions45616.6Installing IPv645716.6.1Auto conﬁguration 45716.7Using IPv6 utilities45816.7.1IPv6 45816.7.2NETSH 45916.7.3Ping6 45916.7.4Tracert6 46016.7.5IPSec6  46116.7.6Windows 2000 speciﬁc 46316.8IPv6 routing46416.8.1Route determination process 46516.8.2Administering the IPv6 routing table 46616.8.3IPv6 routing advertisements 46816.9IPv6 coexistence46916.9.1The 6to4 protocol 46916.9.2The ISATAP protocol 47116.9.3The 6over4 protocol 47316.10IPv6 in .NET47316.11Conclusion479 17Web Services and Remoting         481 17.1Introduction48117.2Creating a Web service481 xivContents 17.2.1Deploying a Web service 48517.3Using a Web service48617.4Asynchronous calls to Web services48917.4.1Wait handles 49017.4.2Callbacks 49117.5Interoperability49317.6Performance49417.7Security49517.8Web services enhancements49717.8.1Web service extensions: Attachments 49817.8.2Web service extensions: Routing  50017.8.3A word on Project Hailstorm (MyServices) 50017.9.NET remoting50017.9.1How remoting works 50117.9.2Implementing remoting 50217.9.3Asynchronous use of remote objects 50617.9.4Deployment of a remoting service 50817.9.5Conﬁguration 50917.9.6Hosting remote objects within IIS 51017.9.7Hosting remote objects within a Windows service 51117.9.8Distributed garbage collection 51517.10Conclusion518 Index519 xv Preface This book will help you develop network applications with .NET, usingeither the C# or VB.NET programming language. It covers everything you need to know about network programming in.NET, from basic get-started information, to a huge selection of advancednetworking technologies that may have seemed like science ﬁction—untilnow. Whether you’re looking for a solution to a speciﬁc networking issue orfor a general all-round knowledge of network application development,you’ll ﬁnd it in this book! Who should read this book? This book is aimed at professional developers with some previous program-ming experience. Basic knowledge of either C# or VB.NET is an advantage,but not essential. This is not a beginners guide to .NET, and as such it isassumed that you already know basic programming constructs such as  if statements and loops.No previous experience with network programming is assumed, so evencomplete newcomers will ﬁnd this book comprehensive enough cover allthe basics. Seasoned programmers may skip the ﬁrst chapter, and readerswill quickly ﬁnd the pace fast enough to keep even the most expert develop-ers glued to the pages.Although the book is geared for developers, as a solution architect, ITmanager, or even computer science undergraduate, you will also ﬁnd thisbook of enormous beneﬁt. Every new concept is introduced with its associ-ated technology theory and commercial implications for IT businesses. Thisbook keeps a keen eye on best practice techniques, as well as providesground-up implementations. Using this approach, project managers can xviPreface help guide developers towards an implementation that could provide futureﬂexibility or lead to faster end-product deployment.  What hardware and software do you need? In order to use the code examples provided in this book, you should installthe latest version of the .NET framework from Microsoft’s Web site. It isalso highly recommended that you install Visual Studio .NET, rather thanuse the command-line based compilers supplied with the .NET SDK. The minimum hardware requirements for Visual Studio .NET are (cid:1) Intel Pentium processor; 450 MHz or equivalent (cid:1) Microsoft Windows 2000, NT 4.0, or XP (cid:1) 128 Mb RAM (cid:1) 3 Gb of available disk spaceThe telephony examples in chapter 14 require the use of a voice modemand access to a live analog phone line.  How this book is organized The book is divided into three main parts. The following sections willdescribe what is covered in each part of the book. Part I: Basic network applications Chapters 1 to 6 cover the established Internet technologies. These includethe main activities that we all carry out in our daily lives, everything frombrowsing the Web, sending e-mail, and maybe uploading ﬁles with FTP.Knowing how to implement these basic networking operations from .NETis a must for any serious developer. Ever wanted to link to your companyWeb site from your application or to send an e-mail whenever the programcrashes? These chapters show you how.  Part II: Network application design Chapters 7 to 11 discuss network application design. These chapters areaimed at enterprise-scale development of heavy-duty distributed applica- PrefacexviiChapter tions. Provided are ﬁve chapters on hardware, encryption, authentication,scalability, and performance. Encryption and authentication provide youwith the conﬁdence to know that nobody can defraud your system or com-promise the conﬁdentiality of the information held within it. Scalabilityensures that you can keep your service working at full tilt even underextreme loads. With an excellent chapter on performance enhancing tech-niques, after reading this section you can be sure that no customer turnsaway because they were ”bored waiting.” All together this handful of pagesequates to a huge step forward in application quality.  Part III: Specialized networking topics Chapters 12 to 17 are geared toward the more specialized networking topicsand the more advanced developer with a keen interest in niche or cutting-edge technologies. Each chapter in this section is the result of months ofresearch, brought to you in simple step-by-step examples. This sectionincludes possibly the ﬁrst published implementation of frame-level packetcapture in .NET, as well as a cool telephony application built from scratchin .NET. These chapters also cover MSMQ, IPv6, WMI, DNS, Ping, WHOIS,Telnet, ARP, RIP, OSPF, BGP/EGP, SNMP, PPP, Web services, remoting,and more! Conventions used in this book Typographical conventions This book uses  fixed-spaced  font to differentiate between Englishtext and keywords that are used verbatim in computer code. Words high-lighted in  italic  are used to emphasize a new programming term. Note:  A note such as this is used to emphasize an important point or a worthwhile observation. Code Code examples in this book are labeled as either C# or VB.NET and areprinted with ﬁxed-spaced fonts, such as the following example: C#public int addition(int a, int b){  return a+b;} xviiiPreface In some cases, other scripts, such as SQL, ASP.NET, or MS-DOS areused and labeled accordingly.  Further information You can ﬁnd help for speciﬁc problems and questions by investigating sev-eral Web sites. A good place to start for issues relating to .NET is alwaysMicrosoft’s ofﬁcial Web site at  msdn.Microsoft.com/net .For deﬁnitive information on speciﬁc network protocols, you shouldconsult the IETF (Internet Engineering Task Force) Web site at  http://www.ietf.org/rfc.html .You may also contact the author with any questions or comments regard-ing this book. While every care has been taken to ensure that all the informa-tion within is correct and accurate, you are free to report anything you feel ismissing or erroneous, so that these can be corrected in future revisions. Fiach Reid fiach@eircom.net Co. Donegal, IrelandFebruary 2004 xix Acknowledgments This book was made possible by a wonderful network of people at DigitalPress. Of these people I would like to personally thank Pam Chester andTheron Shreve, without whom this book would have never been published.I would also like to thank Alan Rose and all at Multiscience Press for theirefforts in getting this book into print.I am extremely grateful to the assistance of my technical reviewer, DavidStephenson at HP. His technical expertise improved the code examples inthis book one hundred fold. A big thank you goes out to all those atMicrosoft who offered their assistance in the writing of this book, especiallyChristopher Brown and Lance Olson. I would like to also like to say thanks to everybody at eyespyfx.com fortheir help and support and also to the guys at cheapﬂights.ie for their exper-tise and sense of humor. Above all else, I would like to thank my parents forbeing so supportive of me for the past twenty-three years.This page intentionally left blank  1 1 Understanding the Internet and Network Programming  1.1Introduction This book will help you develop network applications with .NET, usingeither the C# (pronounced C-sharp) or VB.NET programming language. Itis broken up into three distinct sections: networking basics, distributedapplication design, and specialized networking topics.The ﬁrst six chapters of the book cover the established Internet technol-ogies, such as email and the World Wide Web. Leveraging established tech-nologies such as these gives the general public greater access to yoursoftware service because most users will already have a Web browser oremail client on their computers. The next ﬁve chapters discuss network application design. Thisincludes application security, performance, and scalability. Containedwithin these chapters is practical, hands-on advice to help improve theoverall quality of your software. With tougher security, your applicationswill be less susceptible to theft of intellectual property and privileged infor-mation. The performance and scalability improvements described in thissection will ensure that your application remains responsive even under themost extreme loads.The specialized networking topics section provides a wealth of informa-tion about both niche and cutting-edge Internet technologies. Theseinclude chapters on telephony, packet capture, message queues, IPv6, andMicrosoft’s latest offerings in the ﬁeld of distributed application develop-ment: Web services and remoting.   2 1.3 What can a network program do? 1.2Why network programming in .NET? One of the ﬁrst technical decisions to be made whenever a new project isundertaken is what language to use. .NET is a capable platform on whichto develop almost any solution, and it offers substantial support for net-work programming. In fact, .NET has more intrinsic support for network-ing than any other platform developed by Microsoft.This book assumes that you have already decided to develop with .NET,and languages outside the .NET platform will not be discussed in any greatdetail, except for comparative purposes. This is not to say that .NET is thebe-all and end-all of network-programming applications. If your applica-tion runs over a UNIX-only infrastructure communicating via Java remotemethod invocation (RMI), then .NET is not the way to go. In most cir-cumstances, however, you will ﬁnd that .NET is more than capable of han-dling whatever you throw at it.  1.3What can a network program do? A network program is any application that uses a computer network totransfer information to and from other applications. Examples range fromthe ubiquitous Web browser such as Internet Explorer, or the program youuse to receive your email, to the software that controls spacecraft at NASA.All of these pieces of software share the ability to communicate withother computers, and in so doing, become more useful to the end-user. Inthe case of a browser, every Web site you visit is actually ﬁles stored on acomputer somewhere else on the Internet. With your email program, youare communicating with a computer at your Internet service provider (ISP)or company email exchange, which is holding your email for you. This book is largely concerned with creating network programs, notWeb sites. Although the capabilities of Web sites and network programs arequickly converging, it is important to understand the arguments for andagainst each system. A service accessed via a Web site is instantly accessibleto users across many different platforms, and the whole networking archi-tecture is ready-built for you; however, there is a point at which features aresimply unfeasible to implement using Web sites and at which you have toturn to network applications.Users generally trust network applications; therefore, these programshave much greater control over the computers on which they are runningthan a Web site has over the computers viewing it. This makes it possible 1.4 IP addresses3Chapter 1 for a network application to manage ﬁles on the local computer, whereas aWeb site, for all practical purposes, cannot do this. More importantly, froma networking perspective, an application has much greater control over howit can communicate with other computers on the Internet.   To give a simple example, a Web site cannot make the computer that isviewing it open a persistent network connection to another computer(except the computer from which the Web site was served). This applieseven when the Web site contains embedded content such as a Java applet orFlash movie. There is one exception to this rule, when executable content(such as an ActiveX control) is included in a page. In this case, the page iscapable of everything a network program could do, but most browsers andantivirus software will warn against or deny such executable content.Therefore, this scenario is commonly accepted as being unfeasible becauseof public distrust. 1.4IP addresses Every computer that connects directly to the Internet must have a globallyunique IP address. An IP address is a four-byte number, which is generallywritten as four decimal, period-separated numbers, such as 192.168.0.1.Computers that connect indirectly to the Internet, such as via their com-pany network, also have IP addresses, but these do not need to be globallyunique, only unique within the same network. To ﬁnd out what the IP address of your computer is, open a DOS con-sole window and type  IpConfig  (Windows NT, 2000, and XP) or  winIpcfg (Windows 95, 98, and ME).In Figure 1.1, the PC has two IP addresses: 192.618.0.1 and81.98.59.133. This is unusual because this particular PC contains two net-work cards and is connected to two different networks. Only one of thoseIP addresses is publicly accessible.If you receive the IP address 127.0.0.1, your computer is not connectedto any network. This IP address always refers to the local machine and isused in later examples.In the same way that you can tell whether a phone number is local orinternational by looking at the preﬁx, you can tell whether the computerwith that IP address is on the same local area network or somewhere else onthe Internet by looking closely at an IP address. In the case of IP addresses,they are always the same length, but certain preﬁxes (192.168 being the 4 1.4 IP addresses most common) indicate that the computer is in a local area network, orintranet, and not accessible to the outside world.If you share your Internet connection with other computers on yournetwork, you may have a private IP address. These can be recognized asbeing within the IP address ranges listed in Table 1.1.The same private IP address may exist on two computers in differentlocal area networks (LANs). This does not cause a problem because neithercomputer can directly contact the other. Whereas a privately addressedcomputer can initiate a request for information from a foreign computer,no foreign computer can initiate a request for information from a privatelyaddressed computer. The exception to this rule would be where network address translation(NAT) or port forwarding is set up on the router that lies upstream of theprivately addressed computer. This is where requests from foreign machinesdestined for the IP address of the router are forwarded to a designated com- Figure 1.1 IPConﬁg. Table 1.1 Private IP families. IP Address RangeNumber of Distinct Addresses 10.0.0.0 to 10.255.255.255 Up to 16 million computers (Class A)172.16.0.0 to 172.31.255.255 900,000 computers (Class B)192.168.0.0 to 192.168.255.255 65,000 computers (Class C) 1.4 IP addresses5Chapter 1 puter behind the router. Responses from this computer are forwarded fromthe router back to the foreign machine that initiated the request. The bene-ﬁts of such an architecture are security and the possibility for load balanc-ing, which is described in more detail in later chapters.All computers with private IP addresses must be connected to at leastone computer or network router with a public IP address to access theInternet.  In order to ensure that no two computers on the Internet have the sameIP address, there is a central regulatory body known as the InternetAssigned Numbers Authority (IANA), and more recently the Internet Cor-poration for Assigned Names and Numbers (ICANN). This body actsthrough ISPs to assign public IP addresses to organizations and individuals.Although it is possible to be allocated one IP address at a time, it is morecommon to be allocated IP addresses in contiguous blocks.Contiguous blocks come in three classes: A, B, and C. Class A addressesare blocks of IP addresses with the same ﬁrst byte only. Class A is more than16 million IP addresses in size. Class B addresses are blocks of IP addresseswith the same ﬁrst and second byte. Class B holds 65,024 public IPaddresses. The full 2 16  byte range is not available because the last byte of anIP address cannot be 0 or 255 because these are reserved for future use.Class C addresses are blocks of IP addresses with the same ﬁrst, second, andthird byte. Class C holds 254 public addresses, and class C addresses areroutinely allocated to companies. A computer may not always have the same IP address. It may obtain itsIP address from your ISP’s dynamic host control protocol (DHCP) server.This means that your IP address may change every time you go online.Such an IP address is called a dynamic IP address. If you are on an intranet,you can check to see if your IP address is liable to change by checking the“obtain IP address automatically” radio button in TCP/IP properties, underNetwork in the control panel.The purpose of DHCP is that if there is a limited number of IPaddresses available to the ISP, it will allocate its subscribers with IPaddresses from a pool on a ﬁrst-come, ﬁrst-served basis. IP addresses are 32-bit numbers, with a maximum value of about 4 billion, and the number ofcomputers in the world is fast approaching that ﬁgure. IPv6 is a solution tothat problem and is discussed in later chapters.There is one identiﬁer built into every network card that is genuinelyunique and cannot be changed. This is called the hardware, or media accesscontrol (MAC) address. A sample MAC address is 00-02-E3-15-59-6C. 6 1.5 The network stack This is used on intranets to identify computers when they log on to the net-work. A system called address resolution protocol (ARP) is used to associateMAC addresses with IP addresses. 1.5The network stack The digital signals that travel between computers on the Internet areextremely complex. Without the concept of encapsulation, programmerswould quickly become bogged down with insigniﬁcant details.This technique is used in everyday life, where you may ask a taxi driverto take you to the city center. It is the taxi driver’s responsibility to ﬁnd thequickest route and to operate the car. At a lower level again, it is the carmanufacturer’s responsibility to ensure that gasoline will be present in theengine pistons while the accelerator is depressed.Encapsulation is where the more complex details of a task are hidden,and the programmer only needs to concentrate on what is happening at ahigher level. The open systems interconnection (OSI) network stack modelhas seven layers of encapsulation, as shown in Table 1.2.In modern programming, however, the network stack looks more likeTable 1.3.The most important layer for any programmer is the uppermost layerbecause this will afford the greatest ease of use and will suit most applica-tions. When you head down the stack, implementation becomes more difﬁ-cult, albeit more ﬂexible.   Table 1.2 The traditional network stack. Level NameLayer NameExample Protocol Level 7Application layerFTPLevel 6Presentation layerXNSLevel 5Session layerRPCLevel 4Transport layerTCPLevel 3 Network layerIPLevel 2Data-Link layerEthernet FramesLevel 1Physical  layerVoltages 1.7 Internet standards7Chapter 1 This book covers the application layer primarily, but coverage is given toall of the various layers, excluding the physical layer, which would applyonly to electronics engineers.In network programming, you generally do not need to concern yourselfwith how information travels between two computers, just with what youwant to send. The ﬁner details are handled at lower levels and are controlledby the computer’s operating system.  1.6Ports If you want to browse the Web and receive emails at the same time, yourcomputer needs to decide which bits of network trafﬁc are emails andwhich are Web pages. To tell the difference, every piece of data on the net-work is tagged with a port number: 80 for Web pages, 110 for incomingemail. This information is contained within either the transmission controlprotocol (TCP) or user datagram protocol (UDP) header that immediatelyfollows the IP header. Table 1.4 lists common protocols and their associatedport numbers. 1.7Internet standards When developing a networked application, it is important not to reinventthe wheel or otherwise create an application that is unnecessarily incompat-ible with other applications of the same genre. This book often refers tostandards documents, so it is worthwhile knowing where to ﬁnd them. A shining example is dynamic HTML, which was implemented differ-ently on Internet Explorer and Netscape Navigator. This meant that mostWeb sites that used dynamic HTML would fail to work properly on allbrowsers. Thus, Web developers avoided it and moved toward cross- Table 1.3 The modern network stack. Level NameLayer NameExample Protocol Level 4Structured Information layerSOAPLevel 3Messaging layerHTTPLevel 2Stream layerTCPLevel 1 Packet layerIP 8 1.7 Internet standards browser technologies, such as Macromedia Flash and Java Applets. The rea-son for this downfall is lack of standardization.Two organizations are largely responsible for regulating Internet stan-dards: the Internet Engineering Task Force (IETF) and the World WideWeb Consortium (W3C). The IETF is a not-for-proﬁt organization, whichregulates the most fundamental protocols on the Internet. Anyone can sub-mit a protocol to them, and it will be publicly available as a request for Table 1.4 Well-known port numbers. PortProtocol 20FTP (data)21FTP (control)25SMTP (email, outgoing)53DNS  (domain names)80HTTP (Web)110POP3 (email, incoming)119NNTP (news)143IMAP (email, incoming)Source:  www.iana.org/assignments/port-numbers.txt. Table 1.5 Important RFCs. RFC DocumentProtocol Described RFC 821SMTP (email, outgoing)RFC 954WHOISRFC 959FTP (uploading and downloading)RFC 1939POP3 (email, incoming)RFC 2616HTTP (Web browsing)RFC 793TCP (runs under all above protocols)RFC 792ICMP (ping)RFC 791IP (runs under TCP and ICMP) 1.8 What is .NET?9Chapter 1 comments (RFC) on their Web site at  www.ietf.org/rfc.html.  Table 1.5 listssome important RFC documents.The W3C ( www.w3c.org )   is designed to facilitate standard interopera-bility among vendors. Only large corporations can become members of theW3C. The W3C is responsible for hypertext markup language (HTML),cascading style sheets (CSS), and extensible markup language (XML). 1.8What is .NET? .NET is not a programming language. It is a development framework thatincorporates four ofﬁcial programming languages: C#, VB.NET, ManagedC++, and J# .NET. Where there are overlaps in object types in the four lan-guages, the framework deﬁnes the framework class library (FCL).All four languages in the framework share the FCL and the commonlanguage runtime (CLR), which is an object-oriented platform that pro-vides a runtime environment for .NET applications. The CLR is analogousto the virtual machine (VM) in Java, except it is designed for Windows, notcross-platform, use; however, a stripped-down version of the .NET frame-work, known as the .NET compact framework, is capable of running onWindows CE devices, such as palmtops and certain cell phones. Further-more, there are initiatives to port the CLR to Linux, such as the MONOproject ( www.go-mono.com ).In this book, the two most popular .NET programming languages, C#and VB.NET, are used. Both languages differ syntactically, but are equallycapable and offer identical performance characteristics. Languages in the.NET framework are highly interoperable, so there is no need to be con-ﬁned to a single language. A class compiled from VB.NET can be calledfrom a C# application and vice versa. Similarly, a class written in VB.NETcan derive from a compiled class written in C#. Exceptions and polymor-phism are also supported across languages. This is made possible by a speci-ﬁcation called the Common Type System (CTS).When an application written in a .NET language is compiled, itbecomes the Microsoft intermediate language (MSIL) byte code, which isthen executed by the CLR. MSIL code generated from compiling C# isgenerally identical to MSIL code generated from compiling VB.NET code.Exceptions to this lie with a few language-speciﬁc features, such as how C#can use classic C-style pointers within unsafe code and how VB.NET canuse VB6-style Windows API deﬁnitions. 10 1.8 What is .NET? One of the failings of interpreted, or semicompiled, languages is a per-formance loss. .NET avoids this problem by using a just-in-time (JIT)compiler, which is generally transparent to the user. JIT acts ondemand,whenever MSIL code is ﬁrst executed. JIT compiles MSIL code to machinecode, which is optimized for the processor of the computer that is executingthe code. In this way, JIT can leverage new features as they become availablein new Intel processors without rendering older computers obsolete.   .NET languages are object-oriented rather than procedurally based. Thisprovides a natural mechanism to encapsulate interrelated data and methodsto modify this data within the same logical construct. An object is a pro-grammatic construct that has properties or can perform actions. A coreconcept of object orientation is the ability of one class to inherit the proper-ties and methods of another. The most common example used in this bookis inheritance from  System.Windows.Forms.Form . This provides the stan-dard Windows user interface (i.e., a grey window with a title bar and theMinimize/Restore/Close button set at the top right). You can make your own classes, which could form a base class fromwhich other classes inherit. A typical example would be a class representinga car that could inherit from the vehicle class. .NET does not support mul-tiple inheritance, so the car class cannot inherit from a vehicle class and aWindows form. Interestingly, every class within .NET derives from a rootcalled  System.Object .An interface is a contract that stipulates what methods and properties aclass must expose. To return to the previous example, the vehicle interfacecould be that it must be able to move, hold people, and be bought and sold.The beneﬁt of interfaces is that software designed to auction vehicle objectswould work with cars, motorcycles, and boats. An object can inherit frommultiple interfaces. Thus, a boat could inherit from the vehicle interfaceand expose extra methods that satisfy with the marine interface (e.g., buoy-ancy ratings, nationality). The code examples in this book are designed to be stand-alone Win-dows applications, rather than portable, self-contained classes. Thisapproach is used to ensure that examples are kept as concise as possible. Inreal-world applications, networking code is generally kept separate fromother facets of the application (e.g., user interface (UI), database access).Therefore, it is commonplace to keep classes associated with networking ina separate assembly.An assembly is generally a .DLL ﬁle that contains precompiled (MSIL)code for a collection of .NET classes. Unlike standard Win32 DLLs in 1.9 Getting started11Chapter 1 which developers had to rely on documentation, such as header ﬁles, to useany given DLL, .NET assemblies contain metadata, which provides enoughinformation for any .NET application to use the methods contained withinthe assembly correctly. Metadata is also used to describe other features ofthe assembly, such as its version number, culture, the originator of the code,and any custom attributes that were added to the classes..NET provides a unique solution to the issue of sharing assembliesbetween multiple applications (aptly named DLL Hell). Generally, wherean assembly is designed for use with only one application, it is containedwithin the same folder (or  bin  subfolder) as the application. This is knownas a  private assembly . A  public assembly  is copied into a location where all.NET applications on the local system have access too. Furthermore, thispublic assembly is designed to be versioned, unique, and tamperproof,thanks to a clever security model. This location into which public assem-blies are copied is called the global assembly cache (GAC).If you are developing a component that will be shared among many appli-cations, you can transfer it to the GAC with these simple steps. First, create akey-pair ﬁle by typing  sn –k c:\keys.snk  at the command prompt. Youthen associate the key ﬁle with your assembly by adding the code  [assem-bly:AssemblyKeyFile(“c:\keys.snk“)]  to the head of your class. Finally, itcan be copied into the GAC, either by copying and pasting into windows\assembly with Windows Explorer or by typing  gacutil /I:MyAssembly.dll . 1.9Getting started The examples in this book require you to have access to Microsoft VisualStudio .NET. To program in Microsoft .NET, you need to have theMicrosoft .NET SDK or Microsoft Visual Studio .NET. The former is freelyavailable at the Microsoft Web site ( http://msdn.microsoft.com/netframework/technologyinfo/howtoget/ ). The SDK can be used to create .NET applications,but it is awkward to create graphical user interfaces (GUIs) and use com-mand-line-based compilers.Visual Studio .NET is not free, but no serious .NET developer shouldattempt to write .NET applications without it. A free alternative to VisualStudio .NET is SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/Default.aspx). This ﬁrst example will include instructions for develop-ers opting to use the .NET SDK, as well as Visual Studio .NET users, butno further examples will use the .NET SDK. 12 1.10 Using Visual Studio .NET All examples are given in the two most popular .NET languages: C# andVisual Basic .NET. Both languages have exactly the same capabilities, andthere is absolutely no difference in performance between the two languages.If you are familiar with C or C++, you should choose to develop in C#. Ifyou are familiar with Visual Basic, you should choose to develop in VisualBasic .NET. When developing an application, you should not swapbetween languages. The ﬁrst example demonstrates how to display a Web page within a.NET application. 1.10Using Visual Studio .NET Open Visual Studio .NET, and click New Project. Then type in a name andlocation for your project (Figure 1.2).Select the Visual Basic Windows application or Visual C# Windowsapplication, depending on which language you wish to develop in.When the form appears, right-click on the toolbox and select CustomizeToolbox (Visual Studio .NET 2002) or Add/Remove Items (Visual Studio.NET 2003). Then select Microsoft Web Browser from the dialog box (asshown in Figure 1.3), and press OK. Figure 1.2 Visual Studio.NET, New Projectdialog. 1.10 Using Visual Studio .NET13Chapter 1 Drag the Explorer icon onto the form, and then drag a button and text-box onto the form. The ﬁnished form should look like Figure 1.4.  The next step is to set the properties of all the user interface elements.Right-click on the button and select the Properties option. You will see theProperties snap-in window appearing.  Scroll up to the top of this window,and click on the property labeled  (Name) . Enter in the new name,  btn-Browse , as shown in Figure 1.5. Similarly, name the textbox  tbURL  and the Microsoft Web Browser con-trol  webBrowser . If you double-click on the button, you will see a page of code alreadywritten for you. Find the reference to  btnBrowse_Click  and insert the fol-lowing code: VB.NET Private Sub btnBrowse_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) Handles _ btnBrowse.Click   webBrowser.Navigate(tbURL.Text)End Sub Figure 1.3 Visual Studio.NET, CustomizeToolbox dialog. 14 1.10 Using Visual Studio .NET C# private void btnBrowse_Click(object sender, System.EventArgs e){  object notUsed = null;  webBrowser.Navigate(tbURL.Text,ref notUsed,ref notUsed, ref notUsed, ref notUsed);} The code consists simply of a single method call,  navigate . Thisinvokes the standard process that Internet Explorer goes through as it navi-gates the Web. The reason for the extra parameters to the method in the C#version is that C# does not support optional parameters. The  navigate method has four optional parameters:  Flags ,  targetFrameName ,  postData ,and  Headers . None of these is needed for this simple example.In the application, click Debug  →→→→ Start, type in the name of a Web pagein the space provided, and press the Browse button. You will see that Webpage appearing in the Web Browser control on the page, such as that shownin Figure 1.6.You will quickly notice that the Web browser window behaves identi-cally to Internet Explorer. This is because the component that was added tothe toolbox is the main processing engine behind Internet Explorer. This Figure 1.4 Visual Studio.NET, form designview. 1.10 Using Visual Studio .NET15Chapter 1 component was developed before .NET arrived on the scene, so it uses anolder component model than the native .NET-managed controls.Applications written in .NET are referred to as  managed , or  type-safe , code . This means that the code is compiled to an intermediate language(IL) that is strictly controlled, such that it cannot contain any code thatcould potentially cause a computer to crash. Applications written in nativecode have the ability to modify arbitrary addresses of computer memory,some of which could cause crashes, or general protection faults.Components designed before the advent of .NET are written in nativecode and are therefore unmanaged and deemed unsafe. There is no techni-cal difﬁculty in combining unsafe code with a .NET application, as shownpreviously; however, if an underlying component has the potential to bringdown a computer, the whole application is also deemed unsafe. Unsafe Figure 1.5 Visual Studio.NET, Propertiestool window. 16 1.11 Using the .NET SDK applications may be subject to restrictions; for instance, when they are exe-cuted from a network share, they could be prevented from operating. Onthe whole, though, if a component can do the job, use it. The Internet Explorer component is a Common Object Model (COM)control. This type of model was used most extensively in Visual Studio 6.0.When a COM object is imported into a .NET application, a Runtime call-able wrapper (RCW) class is created. This class then exposes all the proper-ties and methods of the COM object to .NET code. In some cases, thisimporting process produces an interface that is virtually identical to theoriginal COM object; however, as aptly demonstrated in the previousexample, there may be some differences in the syntax of function calls.In the original COM object, the  Navigate  method’s last four parameterswere optional, but in the case of C#, the optional parameters had to bepassed  ref notUsed . 1.11Using the .NET SDK Using the .NET SDK to develop .NET applications makes a lot more workfor a developer. This section shows you how to write and compile a .NETapplication from the command line. The command line may be adequate for development of console appli-cations,  ASP.NET, and components, but it is not feasible to develop large Figure 1.6 Visual Studio.NET, form atruntime. 1.11 Using the .NET SDK17Chapter 1 Windows forms applications from the command line. The previous exam-ple, although easy to implement in Visual Studio .NET, would require alarge and complex program. Nevertheless, it should be informative toVisual Studio .NET developers to be aware of the code that is autogener-ated by Visual Studio .NET.In the true programming tradition, we shall start with a program thatsimply displays “Hello World.” To make this different, the program will bewritten as a Windows form. After all, DOS console applications are verymuch past their sell-by date, and there seems little point in using them at all.The code for this application may seem daunting at ﬁrst, but this shouldillustrate how much extra work is required to implement applications with-out Visual Studio .NET.First, decide which language you want to develop in, either C# or VisualBasic .NET. Open a text editor, such as Notepad, and type in the followingcode: C# using System;using System.Windows.Forms;namespace helloWorld{  public class Form1 : System.Windows.Forms.Form  {      public Form1()    {      this.Text = "Hello World";    }    [STAThread]    static void Main()     {      Application.Run(new Form1());    }  }}VB.NETImports SystemImports System.Windows.FormsPublic Class Form1 Inherits System.Windows.Forms.Form181.11Using the .NET SDK Public Sub New ( )  InitializeComponent( ) End Sub Private Sub InitializeComponent( )  Me.Text = "Hello World" End subEnd ClassModule Module1 Sub Main ( )  Application.Run ( new Form1 ( ) ) End subEnd ModuleAll this code does is open a window with the caption “Hello World,”which is somewhat underwhelming for the amount of code entered. Look-ing closely at the code, you can see the process of events that make up aWindows application in .NET.An application in .NET is made up of namespaces, some of which aresystem deﬁned and others are coded in. This application contains threenamespaces: System, System.Windows.Forms, and helloWorld. The latter isthe only namespace of the three that is actually supplied by the program-mer. The helloWorld namespace contains a class, named Form1. This classinherits from System.Windows.Forms.Form. This means that the class willhave a visible presence on screen. Whenever a class is created, a function known as the constructor is called.This function can be recognized in C# when the name of the function isthe same as that of the class. In VB.NET, the constructor is a subroutinenamed New. In the case of the previous example and in most Windowsapplications, this constructor is used to place user interface elements (some-times referred to as widgets) on the form. In the previous example, the con-structor calls InitializeComponent, which then sets the window name ofthe current form (this) to “Hello World.”Every application must have a starting point. It is tradition in virtuallyevery programming language that the stating point should be called Main.In C#, the [STAThread] attribute indicates the function which acts as theentry point for this single threaded apartment (STA) application. Everyapplication must have one, and only one, entry point.1.11Using the .NET SDK19Chapter 1[STAThread] static void Main() In VB.NET, the main function is coded in a different way but operatesidentically. The main function must appear in a separate module and becoded as follows. A module is a programmatic element that contains codethat is global to the entire application.Module Module1: Sub Main ( )Once a Windows application starts, at least one form (a class inheritingfrom System.Windows.Forms.Form) must be created in order for there to bea visual interface. To create a new form, we call Application.Run, passingan instance of the form. 1.11.1Compiling with Visual Basic.NETSave the ﬁle to d:\temp\helloworld.vb. Open the command prompt bypressing Start→→→→Run and then typing cmd for Windows NT, 2000, or XP orcommand for Windows 95, 98, or ME.Note: Path names mentioned differ among computers, depending oninstallation options.Type the following:DOSD:\temp> path %path%;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705D:\temp> Vbc /t:winexe /r:system.dll /r:system.windows.forms.dll helloworld.vbD:\temp> helloworldFigure 1.7“Hello World”application.201.12Conclusion1.11.2Compiling with C# Save the ﬁle to d:\temp\helloworld.cs. Open the command prompt bypressing Start > Run and then typing cmd for Windows NT, 2000, or XP orcommand for Windows 95, 98, or ME.Note: Path names mentioned differ among computers, depending oninstallation options.DOSD:\temp> path %path%;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705D:\temp> csc /t:exe helloworld.cs D:\temp> helloworld1.11.3Testing the applicationTo run the application, you need to compile it ﬁrst. Depending on whatlanguage you used to program the application, skip to the relevant section.Once it has compiled, you can run the application by clicking on the exe-cutable (.exe) ﬁle generated from the compilation. You should see a formresembling Figure 1.7.1.12ConclusionThis chapter should whet your appetite for .NET network programmingand give you a better understanding of what you have to bear in mind whenworking with networks.The following chapter deals with input and output (I/O) in .NET,which forms the foundation for all native .NET networking functions. 21 2 I/O in the .NET Framework 2.1Introduction This chapter lays the foundation for virtually every networking examplecontained in this book. Without a working knowledge of how .NET han-dles I/O, it may prove difﬁcult to adapt the code examples in this book toyour own needs.I/O applies to network data transfers, as well as saving and loading toyour computer’s hard disk Later chapters will describe how to perform net-work transfers; however, this chapter will be concerned with the underly-ing I/O operations that are common to both types of transfers. The ﬁrsthalf of this chapter will demonstrate how to read and write to disk, using.NET streams.The second half of this chapter develops the stream concept by demon-strating how to convert complex objects, such as database queries, into aformat that can be written to a .NET stream.  2.2Streams In order to provide similar programmatic interfaces to the broad range of I/Odevices with which a programmer has to contend, a stream-based architec-ture was developed in .NET. I/O devices can be anything from printers tohard disks to network interfaces. Not all devices support the same functions. For example, it is possibleto read only the second half of a 1-Mb ﬁle, but not possible to downloadonly the second half of a Web page. Therefore, not all streams support thesame methods.Properties such as  canRead() ,  canSeek() , and  canWrite()  indicate thecapabilities of the stream when applied to a particular device. 22 2.2 Streams The most important stream in the context of this book is the  network-Stream , but another important stream is  fileStream , which is used exten-sively throughout this book to demonstrate ﬁle transfers over networks.Streams can be used in two ways: asynchronously or synchronously.When using a stream synchronously, upon calling a method, the thread willhalt until the operation is complete or fails. When using a stream asynchro-nously, the thread will return from the method call immediately, and when-ever the operation is complete, a method will be called to signify thecompletion of the operation, or some other event, such as I/O failure.It is not user friendly to have a program “hang” when it is waiting for anoperation to complete. Therefore, synchronous method calls must be usedin a separate thread.Through the use of threads and synchronous method calls, computersachieve the illusion of being able to do several things at once. In reality,most computers have only one central processing unit (CPU), and the illu-sion is achieved by quickly switching between tasks every few milliseconds.The following application illustrates the two techniques. The code inthis book will tend toward using synchronous streams, but it is importantto be able to recognize and understand asynchronous streams. 2.2.1Streams for ﬁles 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 , whichshould be set with  multiline=true . Add two buttons to the form, andname them  btnReadAsync  and  btnReadSync . First, we shall implement asynchronous ﬁle reading. Press Read Asyncand enter the following code: C# FileStream fs;byte[] fileContents;AsyncCallback callback;private void btnReadAsync_Click(object sender, System.EventArgs e){  openFileDialog.ShowDialog(); 2.2 Streams23Chapter 2   callback = new AsyncCallback(fs_StateChanged);  fs = new FileStream(openFileDialog.FileName, FileMode.Open,   FileAccess.Read, FileShare.Read, 4096, true);  fileContents = new Byte[fs.Length];  fs.BeginRead(fileContents, 0, (int)fs.Length, callback, null);} VB.NET Dim fs As FileStreamDim fileContents As Byte()Dim callback As AsyncCallbackPrivate Sub btnReadAsync_Click(ByVal sender As _    System.Object, ByVal e As System.EventArgs) _    Handles btnReadAsync.Click OpenFileDialog.ShowDialog() callback = New AsyncCallback(AddressOf fs_StateChanged) fs = New FileStream(OpenFileDialog.FileName,     FileMode.Open, FileAccess.Read, FileShare.Read, _     4096, True) ReDim fileContents(fs.Length) fs.BeginRead(fileContents, 0, fs.Length, callback, Nothing)End Sub This code requires a little explanation. First, the magic number, 4096, issimply a performance characteristic because it is quicker to transfer datafrom disks in 4-Kb chunks than 1 byte at a time.The ﬁnal parameter in the  FileStream  constructor indicates whetherthe operation is to be completed asynchronously or synchronously.The most important thing to note is that there is no reference to tbResults;  this implies that some other function must handle the data oncethe read is complete. The  AsyncCallback  constructor refers to another func-tion, which is also referenced in the  BeginRead  method, so this must be it.As you can see from the code, the  fs_StateChanged  function has not yetbeen implemented. This function is called whenever the ﬁle is ﬁnishedreading. 24 2.2 Streams Note:  Synchronous use of  FileStream  is more efﬁcient when the ﬁle size is less than 64 Kb and the ﬁle is located on the local machine. C# private void fs_StateChanged(IAsyncResult asyncResult){  if (asyncResult.IsCompleted)  {    tbResults.Text = Encoding.UTF8.GetString(fileContents);    fs.Close();  }} VB.NET Private Sub fs_StateChanged(ByVal asyncResult As _       IAsyncResult)   If asyncResult.IsCompleted Then        tbResults.Text = Encoding.UTF8.GetString(fileContents)        fs.Close()   End IfEnd Sub Now, let’s look at how the same operation is carried out using synchro-nous streams and threading.Click on the Read Sync button, and enter the following code: C# private void btnReadSync_Click(object sender, System.EventArgs e){    Thread thdSyncRead = new Thread(new ThreadStart(syncRead));  thdSyncRead.Start();} VB.NET Private Sub btnReadSync_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) Handles _     btnReadSync.Click 2.2 Streams25Chapter 2         Dim thdSyncRead = New Thread(New ThreadStart _        (AddressOf syncRead)) thdSyncRead.Start();End Sub This code doesn’t perform any ﬁle handling; instead, it creates a newthread, whose entry point is the  syncRead  function. When this thread runs,it does so in parallel with any other code that is running at the same time,which includes the background operating system (OS) “housekeeping”(Windows message handling) functions. If the code above were replaced by a simple call to  syncRead() , the pro-gram would still operate; however, if the ﬁle happened to be severalgigabytes in size, the user would quickly perceive the application to be“hung.” A hung application is notably nonresponsive and may turn whitewhen dragged behind another application. What is actually happening isthat the main thread of application is taking 100% processor time anddoes not give the OS time to handle simple tasks such as redrawing theuser interface.In certain time-critical applications, it may be necessary to take 100%processor time, but any application with a user interface should remainresponsive at all times.The next task is to implement the  syncRead  function: C# public void syncRead(){  openFileDialog.ShowDialog();  FileStream fs;  try  {    fs = new FileStream(ofd.FileName, FileMode.OpenOrCreate);  }  catch(Exception ex)  {    MessageBox.Show(ex.Message);    return;  }    fs.Seek(0, SeekOrigin.Begin);  byte[] fileContents = new byte[fs.Length];  fs.Read(fileContents, 0, (int)fs.Length);  tbResults.Text = Encoding.UTF8.GetString(fileContents); 26 2.2 Streams   fs.Close();} VB.NET     Public Sub syncRead()        OpenFileDialog.ShowDialog()        Dim fs As FileStream        Try            fs = New FileStream(ofd.FileName, _            FileMode.OpenOrCreate)        Catch ex As Exception            MessageBox.Show(ex.Message)            Return        End Try            fs.Seek(0, SeekOrigin.Begin)        ReDim fileContents(fs.Length)        fs.Read(fileContents, 0, fs.Length)        tbResults.Text = Encoding.UTF8.GetString(fileContents)        fs.Close()    End Sub In the above code, you will notice that the  FileStream  constructor isenclosed in a  try/catch  block. This allows the program to recover grace-fully from problems such as a missing ﬁle or an unreadable disk. In real-world applications, any operation that relies on the existence of ﬁles or net-work resources should be contained within a  try/catch  block. This allowsprograms to continue execution, even if something unexpected happens. Inmost examples throughout this book,  try/catch  blocks are not used inorder to keep the examples concise and readable.Three namespaces must be included in the code as follows: C# using System.IO;using System.Text;using System.Threading; VB.NET Imports System.IOImports System.ThreadingImports System.Text 2.2 Streams27Chapter 2 Note:  The most concise way to read text ﬁles (under 1 Gb) is:    (new StreamReader(filename)).ReadToEnd();  To test the application, press Debug  →→→→ Start. Press either button, andthen open a ﬁle, and you will see its contents in the textbox opposite, asshown in Figure 2.1. Many ﬁles, such as those designed to hold audio orvideo data, will display as pages of seemingly random characters because thedata is not designed to be displayed as text and requires another program tointerpret into something we can see or hear. An interesting observation you can make with this application is that ifyou compare the textual representation of a database ﬁle ( .mdb ) with anMp3  ( .mp3 ), you will notice that the database ﬁle contains many identicalpages of text, whereas the Mp3 ﬁle contains a real mixture of characters.The similarity of data within a ﬁle is known as its  entropy . By reducing theentropy of data in a ﬁle, the ﬁle size can be reduced. This is why a databaseshrinks in size when compressed, but an Mp3 doesn’t. Chapter 11 dealswith this topic in more detail.The signiﬁcant methods and properties for  FileStream  are shown inTable 2.1. Figure 2.1 Reading ﬁles usingsynchronous andasynchronousmethods. 28 2.2 Streams 2.2.2Encoding data In the previous example, in both synchronous and asynchronous modes, acall was made to  Encoding.UTF8.GetString()  in order to convert the bytearray to a string. The reason for such a verbose statement is the variety ofways in which a byte array can represent a string. Other valid formats areUnicode ( Encoding.Unicode ), ASCII, and UTF7.Unicode Transformation Format 8 (UTF8)   represents each byte as a dif-ferent character; Unicode represents every two bytes as a character. This sys-tem is used for Eastern languages such as Japanese, but also covers English.Applications designed for worldwide markets should have all human-read-able strings encoded in Unicode to facilitate localization at a later date. Table 2.1 Signiﬁcant members of  FileStream .  Method or PropertyPurpose Constructor Initializes a new instance of the  FileStream . It may be invoked thus:  FileStream(string, FileMode) . Length Gets the length of the ﬁle. Returns  long . Position Gets or sets the current position of the ﬁle pointer. Returns  long . BeginRead() Begins an asynchronous read. It may be invoked thus:  BeginRead(byte[] array,int offset,int numBytes, AsyncCallback userCallback, object stateObject) . BeginWrite() Begins an asynchronous write. It may be invoked thus:  BeginWrite(byte[] array,int offset,int numBytes, AsyncCallback userCallback, object stateObject) . Write Writes a block of bytes to this stream using data from a buffer. It may be invoked thus:  Write(byte[] array,int offset,int count) . ReadReads a block of bytes from the stream and writes the data in a given buffer. It may be invoked thus: Read(in byte[] array,int offset, int count).LockPrevents access by other processes to all or part of a ﬁle. It may be invoked thus: Lock (long position, long length).2.2Streams29Chapter 22.2.3Binary and text streamsWhen data contained in streams is of a well-known format, such as XML,plain text, or primitive types, there are methods available to greatly simplifythe parsing of such data.Plain text is most commonly used in streams that are designed to behuman readable and editable. Plain-text streams exist in many network pro-tocols that were originally designed for text-only UNIX computers. A com-mon guise for plain-text ﬁles is the end-user modiﬁable applicationconﬁguration ﬁles such as the ubiquitous .INI or .CSV; however, these arebeing somewhat replaced by XML in .NET.A common feature of plain text is that each unit of information is termi-nated with an {enter}. This is actually a sequence of two UTF8 codes, 10and 13 (represented in C# by \n and by VBCrLf in VB.NET). This can betricky to parse out of a string, so methods such as ReadLine have beenimplemented in the textReader class.To read a ﬁle one line at a time to the end, you could use code similar tothe following application. Start a new project in Visual Studio .NET, anddraw a button on the form. Name this button btnRead. Click on this but-ton, and enter the following code:C#private void btnRead_Click(object sender, System.EventArgs e){ OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); FileStream fs = new FileStream(ofd.FileName, FileMode.OpenOrCreate); StreamReader sr = new StreamReader(fs); int lineCount=0; while (sr.ReadLine()!=null) {  lineCount++; } fs.Close(); MessageBox.Show("There are " + lineCount + " lines in " +  ofd.FileName);}302.2StreamsVB.NETPrivate Sub btnRead_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRead.Click Dim ofd As OpenFileDialog =  New OpenFileDialog()  ofd.ShowDialog() Dim fs As FileStream =  New _     FileStream(ofd.FileName,FileMode.OpenOrCreate)  Dim sr As StreamReader =  New StreamReader(fs)  Dim lineCount As Integer = 0  While Not sr.ReadLine() Is Nothing   lineCount = lineCount + 1 End While fs.Close() MessageBox.Show("There are " & lineCount & _    " lines in " &  ofd.FileName)End subThe following namespace must be included in the code in order for it tocompile correctly:C#using System.IO;VB.NETImports System.IOTo test the application, run it from Visual Studio .NET. Press the Readbutton, and then select a text ﬁle from the hard disk. Press OK, and a mes-sage box similar to Figure 2.2 will appear shortly.When porting a .NET application from a console application to a Win-dows application, you will notice that the familiar format of the Con-sole.WriteLine method is not reﬂected in standard string handling. It is,however, available in StringBuilder.AppendFormat and Stream-Writer.WriteLine. Not everything stored on disk or sent across a network has to be humanreadable. In many cases, signiﬁcantly more efﬁcient code can be written,which leverages the compact binary representations of variables. Forinstance, the number 65000 in a 16-bit unsigned Integer binary (Uint16) is11111101 11101000 (2 bytes); in text it is “6,” “5,” “0,” “0,” “0” (5 bytes). 2.2Streams31Chapter 2To save an array of variables to disk, you could use the following applica-tion. Start a new project in Visual Studio .NET and draw a button on theform. Name this button btnWrite. Click on this button and enter the fol-lowing code:C#private void btnWrite_Click(object sender, System.EventArgs e){ SaveFileDialog sfd = new SaveFileDialog(); sfd.ShowDialog(); FileStream fs = new FileStream(sfd.FileName, FileMode.CreateNew);Figure 2.2Using streams tohelp read ﬁles.Table 2.2The signiﬁcant methods and properties for StreamReader.Method or PropertyPurposeConstructorInitializes a new instance of the object. May be invoked thus:  StreamReader(Stream).PeekReturns the next available character, but does not consume it. Returns -1 at the end of a stream. Takes no parameters.ReadReads the next character or next set of characters from the input stream. It may be invoked thus: Read(char[], int, int).ReadBlockReads characters from the current stream and writes the data to buffer, beginning at index. It may be invoked thus: ReadBlock(in char[] buffer, int index, int count).ReadLineReads a line of characters from the current stream and returns the data as a string. Takes no parameters; returns string.ReadToEndReads the stream from the current position to the end of the stream. Takes no parameters; returns string.322.2Streams BinaryWriter bw = new BinaryWriter(fs); int[] myArray= new int[1000];       for(int i=0;i<1000;i++) {   myArray[i]=i;   bw.Write(myArray[i]); } bw.Close();}VB.NETPrivate Sub btnWrite_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnRead.Click Dim sfd As SaveFileDialog =  New SaveFileDialog()  sfd.ShowDialog() Dim fs As FileStream =  New _ FileStream(sfd.FileName,FileMode.CreateNew)  Dim bw As BinaryWriter =  New BinaryWriter(fs)  Dim myArray() As Integer =  New Integer(1000) {}  Dim i As Integer For  i = 1 to 1000   myArray(i)=i  bw.Write(myArray(i)) Next bw.Close()End SubThe following namespace must be included in the code in order for it tocompile correctly:C#using System.IO;VB.NETImports System.IOTo test the application, run it from Visual Studio .NET. Press the Writebutton and then select a location on the hard disk. Press OK, and a ﬁle willbe written to that location shortly. 2.2Streams33Chapter 2Note: int in C# is a signed 4-byte number; thus the resultant ﬁle is exactly4,000 bytes long.The signiﬁcant methods and properties for BinaryWriter are shown inTable 2.3.2.2.4SerializationSerialization is the process by which a .NET object can be converted into astream, 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 aprocess called deserialization. The following examples are modeled on a purchase order system. A pur-chase order is a request to supply goods on a credit basis. The process mustbe highly bug resilient because an error in purchasing information couldcost millions of dollars in unfulﬁlled sales and audits. This means that eachstage of the process must be recorded, from issuance to payment. The pro-cess must follow a set pattern, and dates must be recorded. These rules mustbe enforced by the object itself, so that any bugs can be traced quickly backto the offending class.To demonstrate serialization, you could use code similar to the followingapplication. Start a new project in Visual Studio .NET and draw two but-Table 2.3Signiﬁcant members of the BinaryWriter class.Method or PropertyPurposeConstructorInitializes a new instance of the object. May be invoked thus: BinaryWriter(Stream).CloseCloses the current BinaryWriter and the underlying stream. It takes no parameters.SeekSets the position within the current stream. It may be invoked thus: Seek(int offset, SeekOrigin origin).WriteWrites a value to the current stream. It may be invoked thus: Write(byte[]).Write7BitEncodedIntWrites a 32-bit integer in a compressed format. It may be invoked thus: Write7BitEncodedInt(int value).342.2Streamstons on the form. Name the buttons button1 and button2, respectively.Click on the form, and enter the following code:C#  public enum purchaseOrderStates  {    ISSUED,    DELIVERED,    INVOICED,    PAID  }  [Serializable()]  public class company  {    public string name;    public string address;    public string phone;  }  [Serializable()]  public class lineItem  {    public string description;    public int quantity;    public double cost;  }    [Serializable()]  public class purchaseOrder  {    private purchaseOrderStates _purchaseOrderStatus;    private DateTime _issuanceDate;    private DateTime _deliveryDate;    private DateTime _invoiceDate;    private DateTime _paymentDate;    public company buyer;    public company vendor;           public string reference;      public lineItem[] items;    public purchaseOrder()    {2.2Streams35Chapter 2      _purchaseOrderStatus=purchaseOrderStates.ISSUED;      _issuanceDate=DateTime.Now;    }    public void recordDelivery()    {      if (_purchaseOrderStatus==purchaseOrderStates.ISSUED)      {        _purchaseOrderStatus=purchaseOrderStates.DELIVERED;        _deliveryDate=DateTime.Now;      }    }    public void recordInvoice()    {      if (_purchaseOrderStatus==purchaseOrderStates.DELIVERED)      {        _purchaseOrderStatus=purchaseOrderStates.INVOICED;                _invoiceDate=DateTime.Now;      }    }    public void recordPayment()    {      if (_purchaseOrderStatus==purchaseOrderStates.INVOICED)      {        _purchaseOrderStatus=purchaseOrderStates.PAID;                _paymentDate=DateTime.Now;      }    }}VB.NETPublic Enum purchaseOrderStates     ISSUED    DELIVERED    INVOICED    PAIDEnd Enum  <Serializable()>  _  Public Class company    Public name As String    Public address As String362.2Streams    Public phone As String  End Class  <Serializable()> _   Public Class lineItem    Public description As String    Public quantity As Integer    Public cost As Double  End Class  <Serializable()> _   Public Class purchaseOrder    Private _purchaseOrderStatus As purchaseOrderStates    Private _issuanceDate As DateTime    Private _deliveryDate As DateTime    Private _invoiceDate As DateTime    Private _paymentDate As DateTime     Public buyer As company    Public vendor As company    Public reference As String      Public items() As lineItem     Public sub New()      _purchaseOrderStatus=purchaseOrderStates.ISSUED      _issuanceDate=DateTime.Now    End sub    Public sub recordDelivery()      if _purchaseOrderStatus=purchaseOrderStates.ISSUED        _purchaseOrderStatus=purchaseOrderStates.DELIVERED        _deliveryDate=DateTime.Now      end if    end sub    Public sub recordInvoice()      if _purchaseOrderStatus=purchaseOrderStates.DELIVERED        _purchaseOrderStatus=purchaseOrderStates.INVOICED        _invoiceDate=DateTime.Now      end if    end sub    Public sub recordPayment()      if _purchaseOrderStatus=purchaseOrderStates.INVOICED2.2Streams37Chapter 2        _purchaseOrderStatus=purchaseOrderStates.PAID        _invoiceDate=DateTime.Now      end if    end sub  End ClassNote: The use of the [Serializable()] tag facilitates deep seilalization. It ispossible to perform deep serialization without this tag by using surrogates. Asurrogate is where the a class implements ISerializationSurrogate, and ispassed to the AddSurrogate method of a SurrogateSelector object. TheSurrogateSelector property of the formatter is then set equal to this objectprior to serialization. The _purchaseOrderStatus variable is private and can only be modiﬁedby recordDelivery(), recordInvoice(), and recordPayment(). Thisensures that a bug elsewhere in the code will not cause undelivered goods tobe paid for (i.e., _purchaseOrderStatus cannot change directly fromISSUED to PAID). Similarly, the date recording is encapsulated within theobject and cannot be externally manipulated.To place a purchase order on a stream (either to disk or to the network),you could write each value one after the other as text, separated by commas,and have the receiver parse out the values and re-create the object; however,there is an easier way: serialization.To write the object to a stream and save the object to disk, you could usethe following code:C#private void button1_Click(object sender, System.EventArgs e){  company Vendor = new company();  company Buyer = new company();  lineItem Goods = new lineItem();  purchaseOrder po = new purchaseOrder();  Vendor.name = "Acme Inc.";  Buyer.name = "Wiley E. Coyote";  Goods.description = "anti-RoadRunner cannon";  Goods.quantity = 1;  Goods.cost = 599.99;382.2Streams  po.items = new lineItem[1];  po.items[0] = Goods;  po.buyer = Buyer;  po.vendor = Vendor;  SoapFormatter sf = new SoapFormatter();  FileStream fs = File.Create("C:\\po.xml");  sf.Serialize(fs,po);  fs.Close();            }VB.NETPrivate  Sub Button1_Click(ByVal sender As Object, ByVal e As _System.EventArgs) Handles Button1.Click  Dim Vendor As company =  New company()   Dim Buyer As company =  New company()   Dim Goods As lineItem =  New lineItem()   Dim po As purchaseOrder =  New purchaseOrder()    Vendor.name = "Acme Inc."  Buyer.name = "Wiley E. Coyote"  Goods.description = "anti-RoadRunner cannon"  Goods.quantity = 1  Goods.cost = 599.99   po.items = New lineItem(1) {}  po.items(0) = Goods  po.buyer = Buyer  po.vendor = Vendor   Dim sf As SoapFormatter =  New SoapFormatter()   Dim fs As FileStream =  File.Create("C:\po.xml")   sf.Serialize(fs,po)  fs.Close()End SubTo read the object back into memory, we can deserialize it thus:C#private void button2_Click(object sender, System.EventArgs e){  SoapFormatter sf = new SoapFormatter();2.2Streams39Chapter 2  FileStream fs = File.OpenRead("C:\\po.xml");  purchaseOrder po = (purchaseOrder)sf.Deserialize(fs);  fs.Close();  MessageBox.Show("Customer is " + po.buyer.name);}VB.NETPrivate  Sub button2_Click(ByVal sender As Object, ByVal e As_ System.EventArgs) Handles Button2.Click  Dim sf As SoapFormatter =  New SoapFormatter()   Dim fs As FileStream =  File.OpenRead("C:\po.xml")   Dim po As purchaseOrder = CType(sf.Deserialize(fs),_  purchaseOrder)  fs.Close()  MessageBox.Show("Customer is " + po.buyer.name)End SubBefore this code will work, you will need an assembly reference forSoapFormatter. This is done by clicking Project→→→→Add Reference and select-ing System.Runtime.Serialization.Formatters.Soap, then adding thisline to the top of the code:C#using System.IO; using System.Runtime.Serialization.Formatters.Soap;VB.NETimports System.IOimports System.Runtime.Serialization.Formatters.SoapTo test this application, run it from Visual Studio .NET. Press the Serial-ize button and then the Deserialize button. You will see the message “Cus-tomer is Wiley E. Coyote,” as depicted in Figure 2.3.If you open the ﬁle C:\PO.XML, you will see a human-readable represen-tation of the object, as shown in Figure 2.4.  This format is known as simpleobject access protocol (SOAP) and is very portable between platforms (e.g.,WebSphere for UNIX can read it). 402.2StreamsNote: The constructor is not called during deserialization. In the aboveexample, you will see that the issue date does not change when the object isre-created from disk.The signiﬁcant methods and properties for SoapFormatter are shown inTable 2.4.Figure 2.3Serializing .NETclasses.Figure 2.4XML view of aserialized object.2.2Streams41Chapter 2Serializing to binarySOAP formatting may be very impressive, but it is far from compact andmay be quite bandwidth consuming if sent over a slow network. We cantherefore use the native binary format to store the array by substitutingSoapFormatter with BinaryFormatter in the above example thus:C#BinaryFormatter bf = new BinaryFormatter();FileStream fs = File.Create("C:\\po.bin");bf.Serialize(fs,po);fs.Close();  VB.NETDim bf As BinaryFormatter =  New BinaryFormatter() Dim fs As FileStream =  File.Create("C:\po.bin") bf.Serialize(fs,po)fs.Close()And deserialize with this code:C#BinaryFormatter bf = new BinaryFormatter();FileStream fs = File.OpenRead("C:\\po.bin");Table 2.4Signiﬁcant members of SoapFormatter .Method or PropertyPurposeConstructorInitializes a new instance of the SoapFormatter class. It may be invoked without any parameters.DeserializeDeserializes a stream into an object graph. It may be invoked thus: Deserialize(Stream).SerializeSerializes an object or graph of connected objects. It may be invoked thus: Serialize(Stream, object).AssemblyFormatGets or sets the format in which assembly names are serialized. Returns FormatterAssemblyStyle.TypeFormatGets or sets the format in which type descriptions are laid out in the serialized stream. Returns FormatterTypeStyle. TopObjectGets or sets the ISoapMessage into which the SOAP top object is deserialized. Returns ISoapMessage. 422.2StreamspurchaseOrder po = (purchaseOrder)bf.Deserialize(fs);fs.Close();VB.NETDim bf As BinaryFormatter =  New BinaryFormatter() Dim fs As FileStream =  File.OpenRead("C:\po.bin") Dim po As purchaseOrder = CType(bf.Deserialize(fs), _    purchaseOrder)fs.Close()When substituting the SoapFormatter with the BinaryFormatter, a ref-erence to System.Runtime.Serialization.Formatters.Soap is no longerrequired. Instead, the Formatters.Binary namespace is required; it can beadded by inserting this line to the top of the code:C# using System.Runtime.Serialization.Formatters.Binary;VB.NET imports System.Runtime.Serialization.Formatters.BinaryThis produces a ﬁle that is considerably smaller than the previous SOAPversion. The resulting ﬁle is not human readable, and it is unfeasible to portto other platforms. Note: Binary representations, although difﬁcult to read, are not a secureway of protecting sensitive data.The BinaryFormatter object is programatically identical to the Soap-Formatter object, except that it does not support the topObject method.Shallow serializationWhenever an object is serialized without its private and protected members,this is known as shallow serialization. This may cause problems as a result ofinaccurate copies of objects; for instance, in the purchase order application,users would ﬁnd their orders reverting from PAID to ISSUED. Furthermore,shallow serialization cannot resolve circular references within objects. Forinstance, if a BookCatalog class has a member of type Book, and the Book2.2Streams43Chapter 2class has a member of type BookCatalog, then neither object can be serial-ized shallowly. One beneﬁt of shallow serialization is that it uses XML schema deﬁni-tion (XSD) to deﬁne types. The XSD standard ensures faithful representa-tions on other platforms. The SOAP formatter, as used in deepserialization, uses the CLR-type system and is not standardized across non-.NET platforms.Code for shallow serialization can be seen by the use of code similar tothe following:C#  XmlSerializer xs = new XmlSerializer(po.GetType());  FileStream fs = File.Create("C:\\po.xml");  xs.Serialize(fs,po);  fs.Close();  VB.NET  Dim xs As XmlSerializer =  New XmlSerializer(po.GetType())   Dim fs As FileStream =  File.Create("C:\po.xml")   xs.Serialize(fs,po)  fs.Close()Shallow deserialization is performed with the following code:C#  purchaseOrder po = new purchaseOrder();  XmlSerializer xs = new XmlSerializer(po.GetType());  FileStream fs = File.OpenRead("C:\\po.xml");  po = (purchaseOrder)xs.Deserialize(fs);  fs.Close();  MessageBox.Show("Customer is " + po.buyer.name);VB.NET  Dim po As purchaseOrder =  New purchaseOrder()   Dim xs As XmlSerializer =  New XmlSerializer(po.GetType())   Dim fs As FileStream =  File.OpenRead("C:\po.xml")   po = CType(xs.Deserialize(fs), purchaseOrder)  fs.Close()  MessageBox.Show("Customer is " + po.buyer.name)442.2StreamsThe following namespace is required for the XmlSerializer object:C#using System.Xml.Serialization;VB.NETimports System.Xml.SerializationThe signiﬁcant methods and properties for XMLSerializer are shown inTable 2.5.2.2.5Writing a database to a streamMost business applications use databases to store their data. In order totransport data from the database across a network, it must be written to astream. The easiest way of doing this is to serialize the dataset.Note: SQL Server and Oracle provide direct network access to their data-bases and should be used in preference to serialization.Table 2.5Signiﬁcant members of the XMLSerializer class.Method or PropertyPurposeConstructorInitializes a new instance of the object. It may be invoked thus:  XmlSerializer(Type).DeserializeDeserializes an XML document. May be invoked thus: Deserialize(Stream).FromTypesReturns an array of XmlSerializer objects created from an array of types. May be invoked thus: FromTypes(Type[] types).SerializeSerializes an object into an XML document. May be invoked thus: Serialize(Stream stream, object o).CanDeserializeGets a value indicating whether this XmlSerializer can deserialize a speciﬁed XML document. Can be invoked thus: CanDeserialize(XmlReader xmlReader).2.2Streams45Chapter 2Database programming overviewWhole books have been written on database programming, and it would beimpossible to do the topic justice in this chapter; however, a brief overviewis provided here to help explain the basics of database access in .NET andthe concept of dataset serialization.Database programming is centered on two key strings: the connectionstring and structured query language (SQL) statements. The connectionstring indicates the location and type of the database. The SQL statementdescribes the operation to be performed on the data.To open a connection to a database in .NET, you need to import theSystem.Data.OleDb namespace:C#using System.Data.OleDb;VB.NETimports System.Data.OleDbThis task is followed by the creation of an OleDbConnection object,where the constructor is passed the connection string (Table 2.6). Here thedatabase is a Microsoft Access ﬁle located at c:\purchaseOrder.mdbC#  string szDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" +             "Data Source=C:\\purchaseOrder.mdb";  OleDbConnection DSN =  new OleDbConnection(szDSN);Table 2.6Connection strings for common databases.Database typeConnection stringMicrosoft AccessProvider=Microsoft.Jet.OLEDB.4.0;      Data Source=<location of .mdb file>SQL ServerProvider=sqloledb; Network Library=DBMSSOCN;DataSource=<IP address>,1433; Initial Catalog=<database name>; User ID=<user>; Password=<password>;462.2StreamsVB.NET  String szDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" + _              "Data Source=C:\purchaseOrder.mdb"  Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)Once we have a connection to the database, SQL statements can be exe-cuted against it to read and manipulate data. The constructor of the OleDb-Command object is passed the SQL string.Depending on the intended use of the data, there are three ways to makethe OleDbCommand act upon the SQL: (1) data binding and serialization passthe object to the constructor of an OleDbDataAdapter; (2) manipulationstatements use the executeNonQuery method; and (3) everything else usesthe executeReader method.Four main operations can be performed on a database: reading data(Select), adding new rows to a table (Insert), removing rows from a table(Delete), and changing the contents of an existing row (Update).A select statement takes the formSelect * from tableWhere table is the name of a table in the database. The preceding state-ment would return all of the rows from the selected table. It is possible tolimit the amount of data returned by using where clauses:Select * from table where column=’some data’  Note: It is possible to increase the amount of data returned by using join tocombine two or more tables on a common ﬁeld.Update statements may take the following form:Update table set column=’new data’ where column=’old data’Delete statements may take the following form:Delete from table where column=’old data’Insert statements may take the following form:Insert into table (column) values (’new data’)2.2Streams47Chapter 2To perform an Update, Delete, or Insert function, we use the exe-cuteNonQuery method:C#Public void nonQuery(string szSQL,string szDSN){  OleDbConnection DSN = new OleDbConnection(szDSN);   DSN.Open();  OleDbCommand SQL = new OleDbCommand(SQL,DSN);   SQL.ExecuteNonQuery();   DSN.Close();}VB.NETPublic  Sub nonQuery(ByVal szSQL As String, ByVal szDSN _     As String)  Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)   DSN.Open()  Dim SQL As OleDbCommand =  New OleDbCommand(SQL,DSN)   SQL.ExecuteNonQuery()  DSN.Close()End SubTo perform a Select query, without requiring any serialization or databinding, the executeReader method is used:C#Public void Query(string szSQL,string szDSN){  OleDbConnection DSN = new OleDbConnection(szDSN);   DSN.Open();  OleDbCommand SQL = new OleDbCommand(szSQL,DSN);  OleDbDataReader dataReader = SQL.ExecuteReader();  While(dataReader.Read())  {    // process data  }  DSN.Close();}482.2StreamsVB.NETPublic sub Query(String szSQL,string szDSN)  Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)   DSN.Open()  Dim SQL As OleDbCommand =  New OleDbCommand(szSQL,DSN)   Dim dataReader As OleDbDataReader =  SQL.ExecuteReader()   Do while dataReader.Read()     ' process data.      loop  DSN.Close()end subTo perform a select query, requiring further serialization or data bind-ing, the OleDbDataAdapter object is used to ﬁll a dataset object with theSQL query results:C#Public DataSet Query(string szSQL,string szDSN){  DataSet ds = new DataSet();  OleDbConnection DSN = new OleDbConnection(szDSN);   DSN.Open();  OleDbCommand SQL = new OleDbCommand(szSQL,DSN);  OleDbDataAdapter Adapter = new OleDbDataAdapter(SQL);         Adapter.Fill(ds,"sql");     DSN.Close();  return(ds);}VB.NETPublic  Function Query(ByVal szSQL As String, ByVal szDSN _    As String) As DataSet  Dim ds As DataSet =  New DataSet()   Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)   DSN.Open()  Dim SQL As OleDbCommand =  New OleDbCommand(szSQL,DSN)   Dim Adapter As OleDbDataAdapter =  New OleDbDataAdapter(SQL)  Adapter.Fill(ds,"sql")  DSN.Close()  Return(ds)End Sub2.2Streams49Chapter 2Creating a databaseIn order to try out the following demo, you will need either Microsoft SQLServer 2000 Desktop Engine (available free at www.microsoft.com/sql/msde/downloads/download.asp) or Microsoft Access to create the database.If you are using SQL Server, you can set up the necessary tables and datausing the SQL statements below. Open Query Analyzer, log onto the data-base, and execute the following SQL code:SQLcreate table purchaseOrder(  id int identity(1,1) not null, purchaseOrderStatus int, issuanceDate datetime, deliveryDate datetime, invoiceDate datetime, paymentDate datetime, buyer int, vendor int, reference varchar(50))create table company( id int identity(1,1) not null, name varchar(50), address varchar(50))create table lineitem( id int identity(1,1) not null, description varchar(50), quantity int, cost money, purchaseOrderID int)insert into company (name,address) values ('Wiley E coyote','sandy desert')502.2Streamsinsert into company (name,address) values ('Acme corp.','big city')insert into purchaseorder ( issuanceDate, buyer,vendor) values (getDate(),1,2)insert into lineitem (description,quantity,cost,purchaseorderid) values('Road runner trap',1,100,1)If you are using Access, open Microsoft Access, select Blank Access data-base, and press OK (Figure 2.5). Save the ﬁle to c:\purchaseOrder.mdb, and press New to create a newtable. You should select Design View. Then press OK.Enter in the table ﬁelds as illustrated below. Set Allow Zero Length toYes for the reference ﬁeld.Close the window and save the table as purchaseOrder. Create twoother tables named company and lineItem.The company table should have the following ﬁelds: id, name, address,and phone. The lineItem table should have the following ﬁelds: id,description, quantity, cost, and purchaseOrderID. Figure 2.5Microsoft Access,new databasedialog.2.2Streams51Chapter 2Enter details for two companies into the company table by selecting thetable name and pressing “open.” A corresponding row in the purchaseOr-der table should also be entered, ensuring that the buyer and vendor ﬁeldsmatch the ID ﬁelds in the company table. Enter one item into the lineItemtable, where purchaseOrderID is equal to the ID of the newly entered rowin the purchaseOrder table.Dataset serializationThe following application runs SQL queries against the database just cre-ated in the previous section. The results of the queries are displayed as XMLin a browser window. The ability to convert datasets into XML is usefulbecause it is transferable across networks and can be read from other plat-forms without too much extra work.Start a new Visual Studio .NET project, and select a Windows applica-tion as before. Right-click on the toolbox, and select Customize toolbox (Visual Studio.NET 2002) or Add/Remove Items (Visual Studio .NET 2003). Thenselect Microsoft Web Browser, and press OK. Drag this onto the form, andname it WebBrowser. Also drag a button and textbox named btnQuery andtbSQL, respectively.You will need to add references to the required namespaces ﬁrst:C#using System.Data.OleDb;using System.IO;using System.Xml.Serialization;VB.NETimports System.Data.OleDbimports System.IOimports System.Xml.SerializationTo remove the unsightly error message on the Web browser, we can setthe initial page to be about:blank thus:C#private void Form1_Load(object sender, System.EventArgs e){  object notUsed = null;522.2Streams  WebBrowser.Navigate("about:blank",ref notUsed,ref notUsed, ref notUsed, ref notUsed);}VB.NETPrivate  Sub Form1_Load(ByVal sender As Object, ByVal e _ As System.EventArgs)  WebBrowser.Navigate("about:blank") End SubNow, click on the Query button, and enter the following code: C#private void button1_Click(object sender, System.EventArgs e){  string szDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" +                 "Data Source=C:\\purchaseOrder.mdb";  OleDbConnection DSN =  new OleDbConnection(szDSN);  XmlSerializer xs = new XmlSerializer(typeof(DataSet));  DataSet ds = new DataSet();  DSN.Open();  OleDbCommand odbc = new OleDbCommand(tbSQL.Text,DSN);     OleDbDataAdapter odda = new OleDbDataAdapter(odbc);         odda.Fill(ds,"sql");     TextWriter tw = new StreamWriter("c:\\sql.xml");  xs.Serialize(tw, ds);  tw.Close();  DSN.Close();  object notUsed = null;  WebBrowser.Navigate("c:\\sql.xml",ref notUsed,ref notUsed, ref notUsed, ref notUsed);}  VB.NETPrivate  Sub button1_Click(ByVal sender As Object, ByVal _e As System.EventArgs) Handles btnQuery.Click  Dim szDSN as String = _  "Provider=Microsoft.Jet.OLEDB.4.0;" + _             "Data Source=C:\purchaseOrder.mdb"  Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)   Dim xs As XmlSerializer =  New XmlSerializer((New _2.2Streams53Chapter 2  DataSet).GetType())   Dim ds As DataSet =  New DataSet()   DSN.Open()  Dim odbc As OleDbCommand =  New OleDbCommand(tbSQL.Text,DSN)  Dim odda As OleDbDataAdapter =  New OleDbDataAdapter(odbc) _     odda.Fill(ds,"sql")     Dim tw As TextWriter =  New StreamWriter("c:\sql.xml")  xs.Serialize(tw, ds)  tw.Close()  DSN.Close()  Dim notUsed As Object =  Nothing   WebBrowser.Navigate("c:\sql.xml") End SubNote: The dataset is shallowly serialized. This does not cause a problembecause there are no private members of interest in the dataset object.Please note that the above example assumes that you have used MicrosoftAccess rather than SQL Server and that the database was saved to C:\pur-chaseOrder.mdb. If you have used SQL Server, then you must change theFigure 2.6Serialization froman SQL query.542.3ConclusionszDSN string to “Provider=sqloledb;Network Library=DBMSSOCN;Data-Source=<IP>,1433;Initial Catalog=<database>;UserID=<user>;Pass-word=<password>;”, where <IP>, <database>, <user> and <password> aresubstituted as necessary.To test this application, run it from Visual Studio .NET, enter an SQLstatement in the box provided (e.g., “select * from company”), and press theQuery button. XML should appear in the browser window that representsthe set of data returned, as shown in Figure 2.6.2.3ConclusionThis chapter has introduced the concept of streams. These are used heavilythroughout the remainder of this book.Serialization was also explored and can clearly be seen as a powerful toolthat can be implemented in only a few lines of code. It certainly is a musthave for any object-oriented distributed application.To conclude the chapter, a brief introduction to databases was given.This provides a rudimentary grounding in using either SQL Server orMicrosoft Access in your .NET applications.Chapter 3 deals with sockets, the .NET implementation of the funda-mental Internet protocols, TCP/IP and UDP. 55 3 Working with Sockets  3.1Introduction This chapter explains the most important aspect of network programming,the socket. It is essential to fully understand how to use sockets in .NETbefore proceeding to the following chapters. Examples in this chapter willenable the user to send ﬁles and messages over the Internet, using simplenetwork applications.Two socket-level protocols are described in this chapter. Both protocolsare demonstrated as a pair of applications—one client, one server. This fol-lows the classic client/server model, which is prevalent in most distributedapplications. The chapter follows this structure, describing the client ﬁrst,followed immediately by an implementation of the server.  3.2What is a socket? A socket is an object that represents a low-level access point to the IP stack.This socket can be open or closed or one of a set number of intermediatestates. A socket can send and receive data down this connection. Data isgenerally sent in blocks of a few kilobytes at a time for efﬁciency; each ofthese blocks is called a  packet . Table 3.1 Well-known port numbers . Port NumberProtocol 20FTP data21FTP control 56 3.3 Creating a simple “hello world” application All packets that travel on the Internet must use the Internet protocol.This means that the source IP address, destination address must beincluded in the packet. Most packets also contain a port number. A port issimply a number between 1 and 65,535 that is used to differentiate higherprotocols, such as email or FTP (Table 3.1). Ports are important when itcomes to programming your own network applications because no twoapplications can use the same port. It is recommended that experimentalprograms use port numbers above 1024.Packets that contain port numbers come in two ﬂavors: UDP and TCP/IP. UDP has lower latency than TCP/IP, especially on startup. Where dataintegrity is not of the utmost concern, UDP can prove easier to use thanTCP, but it should never be used where data integrity is more importantthan performance; however, data sent via UDP can sometimes arrive in thewrong order and be effectively useless to the receiver. TCP/IP is more com-plex than UDP and has generally longer latencies, but it does guarantee thatdata does not become corrupted when traveling over the Internet. TCP isideal for ﬁle transfer, where a corrupt ﬁle is more unacceptable than a slowdownload; however, it is unsuited to Internet radio, where the odd soundout of place is more acceptable than long gaps of silence. 3.3Creating a simple “hello world” application This program will send the words “hello world” over a network. It consistsof two executables, one a client, the other a server. These two programscould be physically separated by thousands of kilometers, but as long as theIP addresses of both computers are known, the principle still works. 25SMTP (email, outgoing)53DNS 80HTTP (Web)110POP3 (email, incoming)143IMAP (email, incoming)Source:  www.iana.org/assignments/port-numbers.txt.   Table 3.1 Well-known port numbers (continued). Port NumberProtocol 3.3 Creating a simple “hello world” application57Chapter 3 In this example, the data will be sent using UDP. This means that thewords “hello world” will be bundled up with information that will be usedby IP routers to ensure that the data can travel anywhere it wishes in theworld. UDP data is not bundled with headers that track message integrityor security. Furthermore, the receiving end is not obliged to reply to thesender with acknowledgments as each packet arrives. The elimination ofthis requirement allows UDP data to travel with much lower latency thanTCP. UDP is useful for small payload transfers, where all of the data to besent can be contained within one network packet. If there is only onepacket, the out-of-sequence problems associated with UDP do not apply;therefore, UDP is the underlying protocol behind DNS. 3.3.1Writing a simple UDP client To get started, open Visual Studio .NET, click New Project, then clickVisual C# projects, and then Windows Application. Set the name to “ UDPClient ” and press OK. You could alternately click Visual Basic .NETprojects and follow the code labeled VB.NET in the examples. Now, design the form as shown in Figure 3.1. Name the button  button1 and the textbox  tbHost .Click the button and type in the source code as follows: C# private void button1_Click(object sender, System.EventArgs e){  UdpClient udpClient = new UdpClient();          udpClient.Connect(tbHost.Text, 8080);          Byte[] sendBytes = Encoding.ASCII.GetBytes("Hello World?");    udpClient.Send(sendBytes, sendBytes.Length);} VB.NET Private sub button1_Click(sender as object, e as _ System.EventArgs) Handles button1.Click  Dim udpClient as new UdpClient()      udpClient.Connect(tbHost.Text, 8080)          Dim sendBytes as Byte()   sendBytes = Encoding.ASCII.GetBytes("Hello World?")    udpClient.Send(sendBytes, sendBytes.Length)End sub 58 3.3 Creating a simple “hello world” application From the code, we can see that the ﬁrst task is creating a  UDP Client object. This is a socket that can send UDP packets. A port number is cho-sen arbitrarily. Here, the port number 8080 is used, simply because it is easyto remember and it is not in the ﬁrst 1024 port numbers, which arereserved for special use by IANA.The ﬁrst argument in the  Connect  method indicates where any datashould be sent. Here, I have used  tbHost.Text  (i.e., whatever is typed intothe textbox). If you have access to only one computer, you would type localhost  into this window; otherwise, if you are using two computers,type the IP address of the server. You also need to include some assemblies by adding these lines to justunder the lock of the  using  statements at the top of the code: C# using System.Net;using System.Net.Sockets;using System.Text;using System.IO; VB.NET imports System.Netimports System.Net.Socketsimports System.Textimports System.IO Now, press F5 to compile and run the application. You should see yourapplication resembling Figure 3.1.Table 3.2 shows the signiﬁcant methods and properties for  UdpClient .   3.3.2Writing a simple UDP server The purpose of the UDP server is to detect incoming data sent from theUDP client. Any new data will be displayed in a list box. Figure 3.1 UDP clientapplication. 3.3 Creating a simple “hello world” application59Chapter 3 As before, create a new C# project, but with a new user interface, asshown below. The list box should be named  lbConnections .A key feature of servers is multithreading (i.e., they can handle hundredsof simultaneous requests). In this case, our server must have at least twothreads: one handles incoming UDP data, and the main thread of execu-tion may continue to maintain the user interface, so that it does not appearhung. The details of threading are not within the scope of this book.First, we write the UDP data handling thread: C# public void serverThread(){ UdpClient udpClient = new UdpClient(8080); while(true) {  IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, Table 3.2 Signiﬁcant members of the UdpClient class. Method or PropertyPurpose Constructor 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) . Close() Closes the UDP connection. DropMulticastGroup() Leaves a multicast group.  JoinMulticastGroup() Adds a  UdpClient  to a multicast group. This may be invoked thus:  JoinMulticastGroup(IPAddress) .  Receive() Returns a UDP datagram that was sent by a remote host. This may be invoked thus:  Receive(ref IPEndPoint) . Returns  Byte[] . Send() Sends a UDP datagram to a remote host. This may be invoked thus  Send(byte[], int) . Active Gets or sets a value indicating whether a connection to a remote host has been made. Returns  Bool Client Gets or sets the underlying network sockets. Returns  Socket . 60 3.3 Creating a simple “hello world” application     0);  Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);   string returnData = Encoding.ASCII.GetString(receiveBytes);  lbConnections.Items.Add(   RemoteIpEndPoint.Address.ToString() + ":" +   returnData.ToString()  );       }} VB.NET Public Sub serverThread() Dim udpClient as new UdpClient(8080) While true  Dim RemoteIpEndPoint as new IPEndPoint(IPAddress.Any, 0)  Dim receiveBytes as Byte()    receiveBytes = udpClient.Receive(RemoteIpEndPoint)   Dim returnData As String = _    Encoding.ASCII.GetString(receiveBytes)  lbConnections.Items.Add _  RemoteIpEndPoint.Address.ToString() + ":" + _  returnData.ToString()   WendEnd Sub Again, we use the  UdpClient  object. Its constructor indicates that itshould be bound to port 8080, like in the client. The  Receive  method isblocking (i.e., the thread does not continue until UDP data is received). Ina real-world application, suitable timeout mechanisms should be in placebecause UDP does not guarantee packet delivery. Once received, the data isin byte array format, which is then converted to a string and displayed on-screen in the form  source address: data .There is then the matter of actually invoking the  serverThread  methodasynchronously, such that the blocking method,  Receive , does not hangthe application. This is solved using threads as follows: C# private void Form1_Load(object sender, System.EventArgs e){ 3.3 Creating a simple “hello world” application61Chapter 3  Thread thdUDPServer = new Thread(new ThreadStart(serverThread)); thdUDPServer.Start();} VB.NET Private Sub Form1_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load Dim thdUDPServer = new Thread(new ThreadStart(AddressOf _   serverThread)) thdUDPServer.Start()End Sub To ﬁnish off, the following assemblies are to be added: C# using System.Threading;using System.Net;using System.Net.Sockets;using System.Text; VB.NET imports System.Threadingimports System.Netimports System.Net.Socketsimports System.Text Figure 3.2 UDP Serverapplication. 62 3.4 Using TCP/IP to transfer ﬁles To test this application, execute it from Visual Studio .NET. On thesame computer, open the UDP client and execute it. Type  localhost  intothe textbox and press the button on the UDP client. A message“Localhost:Hello World?” should appear, such as shown in Figure 3.2.If you have a second PC, get its IP address and install the server on thissecond PC and execute it. Again open the client, but type the IP addressinto the textbox. When you press the button on the client, the server shoulddisplay the “Hello World” message. Voilà! You have used .NET to send dataacross a network. 3.4Using TCP/IP to transfer ﬁles Most networked applications use TCP/IP because there is no risk of databecoming corrupted while traveling over the Internet. It is said to be con-nection oriented; that is, both client and server after a setup phase treat a setof IP packets as being sent along a virtual channel, allowing for data that istoo large to ﬁt into a single IP packet to be sent and for retransmission tooccur when packets are lost. This sample application will allow you to send any ﬁle from one com-puter to another. Again, it is client/server based, so you will need either twocomputers or to run both the client and server on the same computer.  3.4.1Writing a simple TCP/IP 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 Filetextbox  tbFilename , and the Server textbox  tbServer .  Also add an OpenFile Dialog control named  openFileDialog . Click on the Browse button and add the following code: Figure 3.3 TCP clientapplication.3.4Using TCP/IP to transfer ﬁles63Chapter 3C#  private void btnBrowse_Click(object sender, System.EventArgs e)  {    openFileDialog.ShowDialog();    tbFilename.Text = openFileDialog.FileName;  }VB.NET  Private Sub btnBrowse_Click(ByVal sender As _     System.Object, ByVal e As System.EventArgs) _     HandlesbtnBrowse.Click   openFileDialog.ShowDialog()   tbFilename.Text = openFileDialog.FileName  end subThis code opens the default ﬁle open dialog box. If the user does notselect a ﬁle, openFileDialog.Filename will return an empty string. Click on the Send button and add the following code:C#private void btnSend_Click(object sender, System.EventArgs e){  Stream fileStream = File.OpenRead(tbFilename.Text);           // Alocate memory space for the file  byte[] fileBuffer = new byte[fileStream.Length];    fileStream.Read(fileBuffer, 0, (int)fileStream.Length);  // Open a TCP/IP Connection and send the data  TcpClient clientSocket = new TcpClient(tbServer.Text,8080);      NetworkStream networkStream =  clientSocket.GetStream();  networkStream.Write(fileBuffer,0,fileBuffer.GetLength(0));  networkStream.Close(); }VB.NETPrivate Sub btnSend_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSend.Click  Dim filebuffer As Byte()    Dim fileStream As Stream    fileStream = File.OpenRead(tbFilename.Text)643.4Using TCP/IP to transfer ﬁles    ' Alocate memory space for the file    ReDim filebuffer(fileStream.Length)    fileStream.Read(filebuffer, 0, fileStream.Length)    ' Open a TCP/IP Connection and send the data    Dim clientSocket As New TcpClient(tbServer.Text, 8080)    Dim networkStream As NetworkStream    networkStream = clientSocket.GetStream()    networkStream.Write(filebuffer, 0, fileStream.Length)end subThe above code reads in a ﬁle and sends it over a network connection.To read in a ﬁle, a stream for this ﬁle is created by passing the ﬁlename tothe OpenRead method. This stream is read into the ﬁle buffer array. An alter-nate means of reading this ﬁle would be to pass the ﬁle stream as a parame-ter to the constructor of a StreamReader, then to call the ReadToEndmethod, although this approach would only be useful for text-only ﬁles. It then opens a TCP/IP connection with the server on port 8080, asspeciﬁed in tbServer.Text. The TcpClient constructor is blocking, in thatcode execution will not continue until a connection is established. If a con-nection cannot be established, a SocketException will be thrown: “Noconnection could be made because the target machine actively refused it.”As usual, the following assemblies are added:C#using System.Threading;using System.Net;using System.Net.Sockets;using System.Text;using System.IO;VB.NETimports System.Threadingimports System.Netimports System.Net.Socketsimports System.Textimports System.IOTable 3.3 shows the signiﬁcant methods and properties for TcpClient.3.4Using TCP/IP to transfer ﬁles65Chapter 33.4.2Writing a simple TCP/IP serverOpen a new project as before, and design a user interface as depicted inFigure 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 multi-threaded. In this case, three threads are used: the main thread maintains theuser interface, a second thread listens for connections, and a third threadhandles the connections.One socket is required for each connection and will remain loaded inmemory until the connection is closed. These sockets need to be stored inan ArrayList rather than a standard array because it is impossible to predicthow many connections will be received.To start, declare a global ArrayList variable:Table 3.3Signiﬁcant methods and properties of TcpClient.Method or PropertyPurposeConstructorInitializes a new instance of the TcpClient class. It may be used thus: new TcpClient(string,Int).NoDelayWhen set to true, it increases efﬁciency if your application only transmits small amounts of data in bursts. Returns Bool.ReceiveBufferSizeGets or sets the size of the receive buffer. Returns Int.SendBufferSizeGets or sets the size of the send buffer. Returns Int.SendTimeoutGets or sets the amount of time a TcpClient will wait to receive conﬁrmation after you initiate a send. Returns Int.Close()Closes the TCP connection.Connect()Connects the client to a remote TCP host using the speciﬁed host name and port number. It may be invoked thus: Connect(string,Int).GetStream()Returns the stream used to send and receive data. Returns NetworkStream.663.4Using TCP/IP to transfer ﬁlesC#public class Form1 : System.Windows.Forms.Form{  private ArrayList alSockets;  ...VB.NETPublic Class Form1 Inherits System.Windows.Forms.Form  private alSockets as ArrayList  ...Because any client wishing to connect to this server would need to knowits IP address, it is helpful to display this on-screen. This is a cosmetic fea-ture, but it may come in handy in other applications. In order to retrievethe local IP address, we call the static method Dns.GetHostByName. Thisreturns an IPHostEntry object, which is a collection of IP addresses, toaccommodate multihomed computers, which many are. Element zero inthis array is commonly the external IP address for the computer.The Form1_Load method displays the local IP address on the form andstarts the thread that will wait for incoming connections. If thelistenerThread method were to be called directly, the program wouldbecome unresponsive and appear to hang, while the socket waited onincoming connections. This effect is avoided by executing thelistenerThread method in a separate thread of execution, which canblock without adversely affecting the user interface. Figure 3.4TCP Serverapplication.3.4Using TCP/IP to transfer ﬁles67Chapter 3C#private void Form1_Load(object sender, System.EventArgs e){  IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());           lblStatus.Text = "My IP address is " + IPHost.AddressList[0].ToString();  alSockets = new ArrayList();  Thread thdListener = new Thread(new ThreadStart(listenerThread));      thdListener.Start();}VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load  Dim IPHost as IPHostEntry  IPHost = Dns.GetHostByName(Dns.GetHostName())    lblStatus.Text = "My IP address is " + _       IPHost.AddressList(0).ToString()  alSockets = new ArrayList()  Dim thdListener As New Thread(New ThreadStart _  (AddressOf listenerThread))  thdListener.Start()End SubThe listenerThread method’s function is to wait indeﬁnitely for TCPconnections on port 8080 and then to redelegate the work of handlingthese requests to the handlerThread method. This function also reports thesource of the connections.This time, the reason for redelegating work to a thread is not to main-tain the responsiveness of the user interface, but rather to ensure that theapplication will continue to listen for new connections while it is handlinga previous client. The new thread will be required to have access to thesocket that is dealing with the current client. Otherwise, there would be nomeans of returning data. This thread will block on the call to AcceptSocket. Execution will notcontinue until an incoming connection has been detected; when it has, anew socket is created and dedicated to handling this particular client. Once683.4Using TCP/IP to transfer ﬁlesthis socket has established a connection, the socket is placed on top of thealSockets array list to await pickup by the handler thread. It may seem unusual that the socket is not passed directly to the thread.This is because it is not valid to specify parameters when deﬁning the start-ing point of a thread, for example, making an erroneous statement such asNew ThreadStart(AddressOf handlerThread(Parameter))Therefore, another means of passing parameters to threads is required. Inthis example, a public array list of sockets is used, where the top-most entryis used by the newest thread, and so forth. Another common technique forpassing parameters to threads is to encapsulate the thread’s methods in a sep-arate class, with public variables acting as parameters. When a new instanceof this class is created, it can be passed to the ThreadStart constructor.Once the socket has been added to the array list, the handler thread isinvoked, and this thread continues to listen for incoming connections. Note: You may notice a port number added to the end of the source IPaddress. This is an internally negotiated port number used by TCP/IP.More details on this topic can be found in Chapter 13.C#public void listenerThread(){  TcpListener tcpListener = new TcpListener(8080);  tcpListener.Start();  while(true)  {    Socket handlerSocket = tcpListener.AcceptSocket();    if (handlerSocket.Connected)    {     lbConnections.Items.Add(      handlerSocket.RemoteEndPoint.ToString() + " connected."     );     lock (this)     {      alSockets.Add(handlerSocket);     }     ThreadStart thdstHandler = new3.4Using TCP/IP to transfer ﬁles69Chapter 3     ThreadStart(handlerThread);     Thread thdHandler = new Thread(thdstHandler);     thdHandler.Start();    }    }      }VB.NETPublic sub listenerThread()  Dim tcpListener as new TcpListener(8080)  Dim handlerSocket as Socket    Dim thdstHandler as ThreadStart  Dim thdHandler as Thread   tcpListener.Start()  do    handlerSocket = tcpListener.AcceptSocket()    if handlerSocket.Connected then       lbConnections.Items.Add( _      handlerSocket.RemoteEndPoint.ToString() + _      "connected.")      SyncLock (Me)            alSockets.Add(handlerSocket)      end SyncLock      thdstHandler = New ThreadStart(AddressOf _       handlerThread)      thdHandler = New Thread(thdstHandler)      thdHandler.Start()    end if    LoopEnd sub      The remainder of the work is carried out in the handlerThread method.This function ﬁnds the last used socket and then retrieves the stream fromthis socket. An array is allocated to the same size as the stream, and once thestream is fully received, its contents are copied into this array.Once the connection closes, the data is written to ﬁle at c:\mydocuments\upload.txt. It is important to have the lock() keyword aroundthe lines of code associated with ﬁle access; otherwise, if two concurrent con-nections try to access the same ﬁle, the program will crash. The contents ofthe ﬁle are then displayed in the list box on-screen. The socket is then set to703.4Using TCP/IP to transfer ﬁlesnull to remove it from memory. If this point were omitted, the array listwould quickly ﬁll with sockets that had lost connection with their clients.Note that the constructor for TcpListener that takes only a single intfor a port number is now obsolete. To stop the compiler complaining aboutthis line of code, simply call the constructor thus:new TcpListener(IPAddress.Any,8080) C#public void handlerThread(){ Socket handlerSocket = (Socket)alSockets[alSockets.Count-1];  NetworkStream networkStream = new NetworkStream(handlerSocket);   int thisRead=0;   int blockSize=1024;   Byte[] dataByte = new Byte[blockSize];   lock(this)   {    // Only one process can access    // the same file at any given time    Stream fileStream = File.OpenWrite("c:\\my documents\    \upload.txt");    while(true)    {     thisRead=networkStream.Read(dataByte,0,blockSize);     fileStream.Write(dataByte,0,thisRead);          if (thisRead==0) break;    }     fileStream.Close();   }   lbConnections.Items.Add("File Written");    handlerSocket = null;  }VB.NET    Public Sub handlerThread()        Dim handlerSocket As Socket        handlerSocket = alSockets(alSockets.Count - 1)3.4Using TCP/IP to transfer ﬁles71Chapter 3        Dim networkStream As NetworkStream = New _           NetworkStream(handlerSocket)        Dim blockSize As Int16 = 1024        Dim thisRead As Int16        Dim dataByte(blockSize) As Byte        SyncLock Me        ' Only one process can access the        ' same file at any given time        Dim fileStream As Stream            fileStream = File.OpenWrite("c:\upload.txt")            While (True)                thisRead = networkStream.Read(dataByte, _                   0, blockSize)                fileStream.Write(dataByte, 0, dataByte.Length)                If thisRead = 0 Then Exit While            End While            fileStream.Close()        End SyncLock        lbConnections.Items.Add("File Written")        handlerSocket = Nothing    End SubAs before, add the namespace references to the head of the code:C#using System.Threading;using System.Net;using System.Net.Sockets;using System.Text;using System.IO;VB.NETimports System.Threadingimports System.Netimports System.Net.Socketsimports System.Textimports System.IOTo test the application, run the server application, and take note of the IPaddress displayed. Then, run the client application. Type the IP address intothe box provided. Click on browse to select a ﬁle. Press send to transfer the723.4Using TCP/IP to transfer ﬁlesﬁle. A ﬁle will soon appear on the server at c:\my documents\upload.txt,which is an exact copy of the ﬁle that was located on the client.To further demonstrate this principle, you can use a telnet program towrite text to c:\upload.txt remotely. On Windows 95, 98, or ME machines, click Start→→→→Run, then typeTelnet. Click Connect→→→→Remote System. Type the server IP address intothe host name textbox, and type 8080 into the port textbox. Press Con-nect. Type some text into the window, and when ﬁnished, press Connect,Disconnect. A ﬁle will soon appear on the server at c:\my documents\upload.txt.On Windows NT, 2000, and XP machines, click Start→→→→Run, then typeTelnet. Type Open 127.0.0.1 8080. Replace 127.0.0.1 with the IP addressof your server, if you have two computers. Type some text into the window,and when ﬁnished, close the window. A ﬁle will soon appear on the serverat c:\upload.txt.Table 3.4Signiﬁcant members of the TcpListener class.Method or PropertyPurposeConstructorInitializes a new instance of the TcpListenerClient class. It may be used thus: new TcpListener(int).LocalEndpointGets the underlying EndPoint of the current TcpListener. Returns EndPoint.AcceptSocket()Accepts a pending connection request. Returns Socket.AcceptTcpClient()Accepts a pending connection request. Returns TcpClient.Pending()Determines if there are pending connection requests. Returns Bool.Start()Starts listening to network requests.Stop()Closes the listener. ActiveGets a value that indicates whether TcpListener is actively listening for client connections. Returns Bool.ServerGets the underlying network socket. Returns Socket.3.5Debugging network code73Chapter 3Ways have already been developed to send ﬁles through the Internet.Anybody who has ever written a Web site would be familiar with programssuch as cuteFTP and smartFTP, which do exactly what was demonstratedin the previous example, albeit with a much more ﬂexible interface. It is rarely a good idea to try to reinvent the wheel and develop a newway to send data through the Internet. The global standardization of proto-cols has made the Internet what it is today. Table 3.4 shows the signiﬁcant methods and properties for TcpListener.3.5Debugging network codeNetwork connections can and do break, and other applications may bealready using the ports you want to use. It is therefore foolhardy to assumethat a call to a Connect or Listen method will always succeed. For this rea-son, the try/catch construct should be employed as demonstrated below:C#try{ serverSocket.Bind(ipepServer); serverSocket.Listen(-1);}catch(SocketException e){ MessageBox.Show(e.Message);}catch(Exception e){ MessageBox.Show(e.Message); Application.Exit();}VB.NETtry serverSocket.Bind(ipepServer) serverSocket.Listen(-1)catch e as SocketException MsgBox(e.Message)Catch e as Exception MsgBox(e.Message)743.5Debugging network code Application.Exit()End tryAnother type of problem that plagues network applications is scalability.This is where the software cannot cope with a large number of sequential orconcurrent connections, or both. To discover scalability problems, you caneither repetitively hit the Connect and Send buttons on your client or writea stress test program to do this for you over long periods. The program mayrun out of memory if sockets are not set to null after use, or it may crashbecause of simultaneous access to a limited resource, or start dropping con-nections, or work perfectly.To locate problems in multithreaded applications, tracing statementsare invaluable. A good mechanism for doing this is the System.Diagnostics.Trace class or simple Console.WriteLine statements at theentrance and exit of methods. Once the problem has been located, plac-ing Lock statements around non-thread-safe code usually aids system sta-bility; however, placing a Lock clause around a blocking statement maycause your application to hang. When developing an application that interfaces with a third-party dis-tributed application, it is sometimes quite difﬁcult to see exactly what isbeing sent between client and server. This matter can be further compli-cated if the protocol is proprietary, with little or no technical information. Many protocols are inherently text based and were originally designedfor users to access by typing the commands directly into the server, ratherthan using a GUI. Nowadays, nobody would have the patience to upload aﬁle via FTP by typing the FTP commands directly into the server, butbecause Internet standards are somewhat immortal, these old systems haveremained. This rather arcane way of accessing Web-based services may no longerbe relevant to the end-user, but it is a godsend to the developer. Say, forexample, you are developing a program that is designed to interface anIMAP (email) server. If the program is not receiving emails, after you’vemeticulously implemented the protocol as per RFC spec, you can alwaysopen up telnet and go through the paces of receiving an email by typingtext into telnet. If you can re-create the error manually, it should helpsolve the problem from a programmatic perspective. This approach wouldnot work with binary protocols such as Distributed Common ObjectModel (DCOM).3.6Socket-level networking in .NET75Chapter 3If you are working with an unofﬁcial or proprietary protocol, there maybe little chance you can guess how it works. The ﬁrst step in approachingany such protocol is to determine on which port it is operating. A usefultool in doing this is netstat. To see it in action, open the commandprompt and type netstat (Figure 3.5).This lists all of the current outgoing and incoming connections to yourcomputer at that time, along with the port in use. To isolate the port usedby any particular application, use the process of elimination. If you turn offall nonessential network services apart from the application that you aretrying to analyze, take note of the list of ports, then turn off the application,and compare the new list with the old list; whatever port is missing is theapplication’s port.Knowing the port number is only one step toward tapping into a proto-col. To see exactly what bits and bytes are being sent between the two appli-cations, you can use one of the example protocol analyzer programsdescribed in Chapter 13 or a ready-made application such as Trace Plusfrom www.sstinc.com.3.6Socket-level networking in .NETIt is often necessary to understand network code written by other develop-ers in order to debug it or adapt it to your own application. After all, noprogram is ever written without referring to some existing code. This book will consistently use the most concise code possible, but it isimportant to realize that there are many techniques to implement net-worked applications in .NET. It is equally important to be able to under-Figure 3.5Netstat utility.763.6Socket-level networking in .NETstand and recognize these techniques when they are used in code written byother developers.The most important class in .NET networking is the Socket class. Thiscan be used for either TCP/IP or UDP as either a client or server; however,it requires the help of the Dns class to resolve IP addresses and is quite difﬁ-cult to use. Three other classes exist, which are simpler to use, but less ﬂexi-ble: TcpListener, TcpClient, and UdpClient. To illustrate the differencesbetween the two techniques, listed below is code that demonstrates how asocket can be made to listen for incoming connections on port 8080 anddisplay any received data on screen.The example below shows how to create a single-threaded TCP serverusing only the Socket class. Begin a new project in Visual Studio .NET.Drag a textbox onto the form, named tbStatus, which has its multilineproperty set to true. Also add a button, named btnListen. Click on thisbutton and add the following code:C#private void btnListen_Click(object sender, System.EventArgs e){  int bytesReceived = 0;  byte[] recv = new byte[1];  Socket clientSocket;  Socket listenerSocket = new Socket(        AddressFamily.InterNetwork,        SocketType.Stream,        ProtocolType.Tcp        );  IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());  IPEndPoint ipepServer = new  IPEndPoint(IPHost.AddressList[0],8080);  listenerSocket.Bind(ipepServer);  listenerSocket.Listen(-1);  clientSocket = listenerSocket.Accept();  if (clientSocket.Connected)  {    do    {      bytesReceived = clientSocket.Receive(recv);      tbStatus.Text += Encoding.ASCII.GetString(recv);    }3.6Socket-level networking in .NET77Chapter 3    while (bytesReceived!=0);  }        }VB.NETPrivate  Sub btnListen_Click(ByVal sender As Object, _ ByVal e As System.EventArgs)  Dim bytesReceived As Integer = 0    Dim recv() As Byte = New Byte(1) {}    Dim clientSocket As Socket    Dim listenerSocket As New Socket( _           AddressFamily.InterNetwork, _           SocketType.Stream, _           ProtocolType.Tcp)    Dim IPHost As IPHostEntry = _    Dns.GetHostByName(Dns.GetHostName())    Dim ipepServer As IPEndPoint = New _    IPEndPoint(IPHost.AddressList(0), 8080)    listenerSocket.Bind(ipepServer)    listenerSocket.Listen(-1)    clientSocket = listenerSocket.Accept()    If clientSocket.Connected Then       Do           bytesReceived = clientSocket.Receive(recv)           tbStatus.Text += Encoding.ASCII.GetString(recv)       Loop While bytesReceived <> 0    End IfEnd SubSo far, the sockets we have dealt with have been abstracted to performspeciﬁc tasks, and as such provide specialized methods that make the cod-ing easier. The generic socket object can be either a server or client. The listener socket is created with a constructor that is passed threeparameters: addressing scheme, socket type, and protocol type.Table 3.5 shows supported addressing schemes.Most of these addressing schemes would rarely be used in a modernWindows environment, but they could be used when interfacing to mini-computers or legacy systems. Table 3.6 shows upported protocol types.783.6Socket-level networking in .NETTable 3.5Addressing schemes supported by Socket .Addressing schemeUsageAddressFamily.AppleTalk AppleTalk address, used for communications with Apple Macintosh computers.AddressFamily.Atm Native asynchronous transfer mode (ATM) services address.AddressFamily.Banyan Banyan VINES (Virtual Networking System) address.AddressFamily.Ccitt Addresses for protocols such as X.25.AddressFamily.Chaos Address for CHAOS protocols, in format 007.x.y.z.AddressFamily.Cluster Address for Microsoft cluster products, such as MSCS.AddressFamily.DataKit Address for Datakit protocols, such as the universal receiver protocol.AddressFamily.DataLink Direct data-link (MAC) interface address.AddressFamily.DecNet DECnet address, designed for DEC minicomputers.AddressFamily.Ecma European Computer Manufacturers Association (ECMA) address, used for circuit-switched call control.AddressFamily.FireFox FireFox address, runs over TCP 1689.AddressFamily.HyperChannel NSC hyperchannel address, deﬁned in RFC 1044.AddressFamily.Ieee12844 IEEE 1284.4 workgroup address, commonly known as DOT4 and used by HP printers.AddressFamily.ImpLink ARPANET interface message processor (IMP) address.AddressFamily.InterNetwork IPv4 address, most commonly used for Internet transfers.AddressFamily.InterNetworkV6 IPv6 address, used for the next version of IP.AddressFamily.Ipx Internetwork packet exchange (IPX) address.3.6Socket-level networking in .NET79Chapter 3AddressFamily.Irda Infrared data association address.AddressFamily.Iso Address for ISO protocols, such as ISO-IP.AddressFamily.Lat Local area transport protocol address, used with DEC minicomputers. AddressFamily.Max MAX address.AddressFamily.NetBios NetBios address, used for Windows ﬁle and printer sharing.AddressFamily.NetworkDesigners Address for Network Designers OSI gateway-enabled protocols.AddressFamily.NS Address for Xerox NS protocols, such as IDP.AddressFamily.Pup Address for PARC universal packet (PUP) protocols.AddressFamily.SnaIBM Systems Network Architecture address.AddressFamily.Unix UNIX local-to-host address.AddressFamily.VoiceView VoiceView address, used in voice and data telephony.Table 3.6Protocol types supported by socket .Addressing schemeUsageProtocolType.Ggp Gateway to gateway protocol (GGP), used for interrouter communicationsProtocolType.Icmp Internet control message protocol (ICMP), also known as Ping and used to report network errorsProtocolType.Idp Internet datagram protocol (IDP), the underlying transport for Xerox networking protocolsProtocolType.Igmp Internet group management protocol (IGMP), used in multicastingTable 3.5Addressing schemes supported by Socket (continued).Addressing schemeUsage803.6Socket-level networking in .NETThe next section of code following the socket constructor is used toresolve the local IP address of the computer. Using the same construct asbefore, Dns.GetHostByName returns an IPHostEntry object. Element num-ber 0 of the AddressList array is then assumed to be the external address. An IPEndPoint object is created from the local IP address and the portnumber 8080. The listener socket is then bound to the endpoint. Thesocket does not start listening until the Listen method is called. Theparameter speciﬁes the number of clients to keep on hold at any one time;-1 indicates an indeﬁnite holding time. As before, when the Accept method is called, execution stops until aconnection request is received. Once a connection request is received, a newsocket dedicated to this client is returned. Once a connection has beenProtocolType.IP Internet protocol (IP), the underlying transport for all communications on the InternetProtocolType.Ipx Internetwork packet exchange (IPX), Novell’s implementation of IDPProtocolType.ND Speciﬁes an unofﬁcial protocol named net disk (ND)ProtocolType.Pup PARC universal packet (PUP) protocol, a predecessor of routing information protocol (RIP)ProtocolType.Raw Raw socket data; excludes frame headersProtocolType.Spx Sequential packet exchange (SPX), Novell’s transport layer protocol that provides a packet delivery serviceProtocolType.SpxII Sequential packet exchange 2 (SPX2), a more modern implementation of SPXProtocolType.TcpTransmission control protocol (TCP), the most common protocol for Internet data transferProtocolType.Udp User datagram protocol (UDP), used for high-speed, low-integrity data transfers on the InternetTable 3.6Protocol types supported by socket (continued).Addressing schemeUsage3.6Socket-level networking in .NET81Chapter 3established, the socket will read incoming data one byte at a time andappend it to the textbox tbStatus. When the Receive method returns 0,the remote end will have closed the connection. Because this example doesnot use threading, it cannot handle more than one client at a time and willappear to hang during operation. To complete the program, you will also require the followingnamespaces:C#using System.Text;using System.Net.Sockets;using System.Net;VB.NETImports System.TextImports System.Net.SocketsImports System.NetTo test this application, run it from Visual Studio .NET. Press the listenbutton. At this point, the application will become unresponsive and appearto hang. Open telnet on the local machine with the following command:telnet localhost 8080Type some text, and then quit telnet. You should see that text on theapplication window, as depicted in Figure 3.6.Most networked applications deal with the interchange of commandsand data between client and server. Because TCP/IP requires connections tobe explicitly opened and closed, it is possible to locate where networkingcode starts by searching for phrases such as “new TcpListener” or “Listen”for servers, and “new TcpClient” or “Connect” for clients. It is both unprofessional and irritating to users if your applicationbecomes unresponsive during normal operation. To avoid this problem,you could use threading, as was demonstrated in examples earlier in thischapter; however, another technique is sometimes employed. Asynchronoussockets are arguably more complicated than threading, but can sometimesoffer higher performance when you are handling a large number of concur-rent connections. Asynchronous operation is mapped to low-level I/O com-pletion ports in the operating system.823.6Socket-level networking in .NETThe following code modiﬁes the above example such that it does notbecome unresponsive when waiting for incoming requests or data. Reopenthe previous example in Visual Studio .NET, and add the following publicvariables directly inside the Form class:C#private AsyncCallback acceptCallBack;private AsyncCallback receiveCallBack;public Socket listenerSocket;public Socket clientSocket;public byte[] recv;VB.NETPrivate acceptCallBack As AsyncCallbackPrivate receiveCallBack As AsyncCallbackPublic listenerSocket As SocketPublic clientSocket As SocketPublic recv() As ByteThese variables need to be accessible to any function within the formbecause server operation is split between three functions: btnListen_Clickuses a socket to listen on port 8080; acceptHandler accepts incoming con-nections; and receiveHandler handles incoming data. Double-click on the Listen button, and replace the code with the fol-lowing code:Figure 3.6TCP server usingsocket-level code.3.6Socket-level networking in .NET83Chapter 3C#private void btnListen_Click(object sender, System.EventArgs e){        acceptCallBack = new AsyncCallback(acceptHandler);        listenerSocket = new Socket(    AddressFamily.InterNetwork,    SocketType.Stream,    ProtocolType.Tcp    );  IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());  IPEndPoint ipepServer = new IPEndPoint(IPHost.AddressList[0],8080);  listenerSocket.Bind(ipepServer);  listenerSocket.Listen(-1);  listenerSocket.BeginAccept(acceptCallBack,null);}VB.NETPrivate  Sub btnListen_Click(ByVal sender As Object, _    ByVal e As System.EventArgs)         acceptCallBack = New AsyncCallback(AddressOf _   acceptHandler)   Dim listenerSocket As Socket = New Socket( _                        AddressFamily.InterNetwork, _                        SocketType.Stream, _                        ProtocolType.Tcp _                        )   Dim IPHost As IPHostEntry = _   Dns.GetHostByName(Dns.GetHostName())   Dim ipepServer As IPEndPoint = New _       IPEndPoint(IPHost.AddressList(0), 8080)   listenerSocket.Bind(ipepServer)   listenerSocket.Listen(-1)   listenerSocket.BeginAccept(acceptCallBack, Nothing)End SubInstead of calling Listen on the socket, BeginListen is called. By doingthis, the function will return immediately, and .NET knows that if anincoming connection appears on the port, the function acceptHandler isto be called. The second parameter passed to BeginAccept is Nothing, or843.6Socket-level networking in .NETnull because no extra information needs to be passed to the callback func-tion once it is called.Now, add the callback function to handle incoming connections:C#public void acceptHandler(IAsyncResult asyncResult){  receiveCallBack = new AsyncCallback(receiveHandler);    clientSocket = listenerSocket.EndAccept(asyncResult);  recv = new byte[1];  clientSocket.BeginReceive(recv,0,1,  SocketFlags.None,receiveCallBack,null);}VB.NETPublic Sub acceptHandler(ByVal asyncResult As IAsyncResult)  receiveCallBack = New AsyncCallback(receiveHandler)    clientSocket = listenerSocket.EndAccept(asyncResult)  recv = New Byte(1) {}  clientSocket.BeginReceive(recv,0,1, _  SocketFlags.None,receiveCallBack,Nothing)End SubThe EndAccept method returns the same socket as would be created bythe Accept method; however, EndAccept is nonblocking and will returnimmediately, unlike Accept.  Just as incoming connections are asynchronous by nature, incomingdata also arrives asynchronously. If the connection is held open for longerthan a few seconds, users will begin to notice that the application hasbecome unresponsive; therefore, a second asynchronous call is used here.Instead of calling Receive, BeginReceive is called on the socket. This ispassed an array buffer, which it populates asynchronously as data arrives.Again, an AsyncCallback object is passed to it because this object containsthe reference to the callback function: receiveHandler. Now, add the callback function to handle incoming data:C#public void receiveHandler(IAsyncResult asyncResult){      3.6Socket-level networking in .NET85Chapter 3  int bytesReceived = 0;  bytesReceived = clientSocket.EndReceive(asyncResult);  if (bytesReceived != 0)  {    tbStatus.Text += Encoding.UTF8.GetString(recv);    recv = new byte[1];    clientSocket.BeginReceive(recv,0,1,     SocketFlags.None,receiveCallBack,null);    }}VB.NETPublic  Sub receiveHandler(ByVal asyncResult As _ IAsyncResult)      Dim bytesReceived As Integer =  0   bytesReceived = clientSocket.EndReceive(asyncResult)  if bytesReceived <> 0 then      tbStatus.Text += Encoding.UTF8.GetString(recv)     recv = New Byte(1) {}     clientSocket.BeginReceive(recv,0,1, _      SocketFlags.None,receiveCallBack,Nothing)   End ifEnd SubIn this example, the array buffer is only one byte long, so this functionwill be called every time one byte of data appears on port 8080. This func-tion is also called when the connection closes, but in this case, the numberreturned from EndReceive is 0. If data is received, the asynchronous readmust be continued by calling BeginReceive again. To complete the program, you will also require the following namespaces:C#using System.Text;using System.Net.Sockets;using System.Net;VB.NETImports System.TextImports System.Net.SocketsImports System.Net863.7ConclusionTest the application in the same way as before. This time, you will noticethat the application does not become unresponsive once the Listen buttonis pressed. 3.7ConclusionSocket-level programming is the foundation of all network programming.This chapter should provide enough information to assist you in imple-menting any TCP- or UDP-based protocol, proprietary or otherwise. Not all network protocols need to be coded at the socket level; extensivesupport for HTTP is provided through classes provided by the .NET frame-work. Leveraging this ready-made functionality can cut down on the devel-opment time required for socket-level implementation.The next chapter takes a detailed look at HTTP and how to write pro-grams in .NET that communicate with Web servers. 87 4 HTTP: Communicating with Web Servers 4.1Introduction This chapter demonstrates how to pull data from the Web and use it withinyour own applications. As mentioned in Chapter 1, Web pages are hostedon computers that run Web server software such as Microsoft InternetInformation Services (IIS) or Apache. Hypertext transfer protocol (HTTP)is used to communicate with these applications and retrieve Web sites.There are many reasons why an application may interact with a Website, such as the following: (cid:1) To check for updates and to download patches and upgrades (cid:1) To retrieve information on data that changes from hour to hour (e.g.,shared values, currency conversion rates, weather) (cid:1) To automatically query data from services operated by third parties(e.g., zip code lookup, phone directories, language translation services) (cid:1) To build a search engine (cid:1) To cache Web pages for faster access or to act as a proxyThe ﬁrst half of this chapter describes how to send and receive data toweb servers. This includes an example of how to manipulate the HTMLdata received from the web server. The chapter is concluded with an imple-mentation of a custom web server, which could be used instead of IIS. 88 4.2 HTTP 4.1.1Data mining Data mining is where an application downloads a Web page and extractsspeciﬁc information from it automatically. It generally refers to the retrievalof large amounts of data from Web pages that were never designed for auto-mated reading.A sample application could be a TV guide program that would down-load scheduling information from TV Web sites and store it in a databasefor quick reference.  Note:  You should always check with Web site administrators whether theypermit data mining on their sites because it may infringe copyright or putexcessive load on their servers. Unauthorized data mining can result in a Web administrator blocking your IP address or worse!In order to extract useful data from this HTML, you will need to be wellacquainted with the language and good at spotting the patterns of HTMLthat contain the data required; however, several good commercial productsaid developers with data mining from HTML pages, and home-brewedsolutions are not always the best idea. 4.2HTTP HTTP operates on TCP/IP port 80 and is described deﬁnitively in RFC2616. The protocol is quite straightforward. The client opens TCP port 80to a server, the client sends an HTTP request, the server sends back anHTTP response, and the server closes the TCP connection. 4.2.1The HTTP request The simplest HTTP request is as follows: GET / <enter><enter> Tip:  On some servers, it is necessary to specify the DNS name of the server in the  GET  request. 4.2 HTTP89Chapter 4 This request will instruct the server to return the default Web page; how-ever, HTTP requests are generally more complex, such as the following: GET / HTTP/1.1Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*Accept-Language: en-gbAccept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)Host: 127.0.0.1:90Connection: Keep-Alive This tells the server several things about the client, such as the type ofbrowser and what sort of data the browser can render. Table 4.1 shows a complete list of standard HTTP request headers are asfollows:  Table 4.1 Standard HTTP request headers . HTTP headerMeaning Accept Used to specify which media (MIME) types are acceptable for the response. The  type */*  indicates all media types and   type/*  indicates all subtypes of that type. In the example above,  application/msword  indicates that the browser can display Word documents.  Accept-Charset Used to specify which character sets are acceptable in the response. In the case where a client issues  Accept-Charset: iso-8859-5 , the server should be aware that the client cannot render Japanese (Unicode) characters. Accept-Encoding  Used to specify if the client can handle compressed data. In the above example, the browser is capable of interpreting GZIP compressed data. Accept-Language Used to indicate the language preference of the user. This can be used to estimate the geographic location of a client;  en-gb  in the above example may indicate that the client is from the United Kingdom. 90 4.2 HTTP Authorization Used to provide authentication between clients and servers. Refer to RFC 2617 or Chapter 9 for more details. Host Host indicates the intended server IP address as typed in at the client. This could differ from the actual destination IP address if the request were to go via a proxy. The host address  127.0.0.1:90  in the above example indicates that the client was on the same computer as the server, which was running on port 90. If-Modified-Since  Indicates that the page is not to be returned if it has not been changed since a certain date. This permits a caching mechanism to work effectively. An example is  If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT . Proxy-Authorization This provides for authentication between clients and proxies. Refer to RFC 2617 or Chapter 9 for more details. Range This provides for a mechanism to retrieve a section of a Web page by specifying which ranges of bytes the server should return; this may not be implemented on all servers. An example is  bytes=500-600,601-999 . Referer This indicates the last page the client had visited before going to this speciﬁc URL. An example is  Referer: http://www.w3.org/index.html.  (The misspelling of  “referrer”  is not a typing mistake!) TE Transfer encoding (TE) indicates which extension transfer encoding it can accept in the response and if it can accept trailer ﬁelds in a chunked transfer encoding. User-Agent  Indicates the type of device the client is running from. In the above example, the browser was Internet Explorer 6.  Content-Type Used in  POST  requests. It indicates the MIME type of the posted data, which is usually  application/x-www-form-urlencoded . Content-Length Used in  POST  requests. It indicates the length of the data immediately following the double line.  Table 4.1 Standard HTTP request headers (continued). HTTP headerMeaning 4.2 HTTP91Chapter 4 Note:  Device-speciﬁc HTTP request headers are preﬁxed with “x-”.  GET  and  POST  are the most common HTTP commands. There are oth-ers, such as  HEAD ,  OPTIONS ,  PUT ,  DELETE , and  TRACE , and interested readerscan refer to RFC 2616 for information on these HTTP commands.Web developers may be familiar with  GET  and  POST  from the HTMLform tag, which takes the form: <form name="myForm" action="someDynamicPage" method="POST"> The difference from a user’s point of view is that form parameters do notappear in the URL bar of the browser when submitting this form. Theseparameters are contained in the region immediately following the double-line feed. A POST request resembles the following: POST / HTTP/1.1Content-Type: application/x-www-form-urlencodedContent-Length: 17myField=some+text 4.2.2The HTTP response When the server receives an HTTP request, it retrieves the requested pageand returns it along with an HTTP header. This is known as the HTTPresponse.A sample HTTP response is as follows: HTTP/1.1 200 OKServer: Microsoft-IIS/5.1Date: Sun, 05 Jan 2003 20:59:47 GMTConnection: Keep-AliveContent-Length: 25Content-Type: text/htmlSet-Cookie: ASPSESSIONIDQGGQQFCO=MEPLJPHDAGAEHENKAHIHGHGH; path=/Cache-control: privateThis is a test html page! 92 4.2 HTTP The client would display the message “This is a test html page!” onscreen in response to this command. Table 4.2 Standard HTTP request headers. HTTP request headerMeaning ETag The entity tag is used in conjunction with the  If-  sufﬁxed HTTP requests. Servers rarely return it. Location It is used in redirects, where the browser is requested to load a different page. Used in conjunction with HTTP 3xx responses.  Proxy-Authenticate This provides for authentication between clients and proxies. Refer to RFC 2617 Section 14.33 or Chapter 9 for more details. Server Indicates the server version and vendor. In the above example, the server was IIS running on Windows XP. WWW-Authenticate This provides for authentication between clients and proxies. Refer to RFC 2617 Section 14.47 or Chapter 9 for more details. Content-Type Indicates the MIME type of the content returned. In the above example, the type is HTML Content-Length Indicates the amount of data following the double-line feed. The server will close the connection once it has sent all of the data; therefore, it is not always necessary to process this command.  Set-Cookie A cookie is a small ﬁle that resides on the client. A cookie has a name and value. In the above example, the cookie name is  ASPSESSIONIDQGGQQFCO . Table 4.3 HTTP response codes . HTTP response code rangeMeaning 100–199 Informational: Request was received; continuing the process. 200–299Success: The action was successfully received, understood, and accepted.300–399Redirection: Further action must be taken in order to complete the request.4.2HTTP93Chapter 4Every HTTP response has a response code. In the above example, theresponse code was 200. This number is followed by some human-readabletext (i.e., OK). The response codes fall into ﬁve main categories shown in Table 4.3.4.2.3MIME typesMultipart Internet mail extensions (MIME) types are a means of describingthe type of data, such that another computer will know how to handle thedata and how to display it effectively to the user.To illustrate the example, if you changed the extension of a JPEG image(.JPG) to .TXT, and clicked on it, you would see a jumble of strange char-acters, not the image. This is because Windows contains a mapping fromﬁle extension to ﬁle type, and .JPG and .TXT are mapped to different ﬁletypes: image/jpeg for .JPG and text/plain for .TXT.To ﬁnd an MIME type for a particular ﬁle, such as .mp3, you can openthe registry editor by clicking on Start > Run, then typing REGEDIT. Thenclick on HKEY_CLASSES_ROOT, scroll down to .mp3, and the MIMEtype is written next to Content Type.Note: Not all ﬁle types have a MIME type (e.g., .hlp help ﬁles).4.2.4System.Web One of the most common uses of HTTP within applications is the abilityto download the HTML content of a page into a string. The followingapplication demonstrates this concept.It is certainly possible to implement HTTP at the socket level, but thereis a wealth of objects ready for use in HTTP client applications, and it400–499Redirection: Further action must be taken in order to complete the request.500-599Server error: The server failed to fulﬁll an apparently valid request.Table 4.3HTTP response codes (continued).HTTP response code rangeMeaning944.2HTTPmakes little sense to reinvent the wheel. The HTTP server in the next sec-tion is implemented using HTTPWebReqest. Start a new project in Visual Studio .NET, and drag on two textboxes,tbResult and tbUrl. TbResults should be set with multiline=true. Abutton, btnCapture, should also be added. Click on the Capture button,and enter the following code:C#private void btnCapture_Click(object sender, System.EventArgs e){ tbResult.Text = getHTTP(tbUrl.Text); }VB.NETPrivate Sub btnCapture_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnCapture.Click tbResult.Text = getHTTP(tbUrl.Text)End SubThen implement the getHTTP function:C#public string getHTTP(string szURL){  HttpWebRequest  httpRequest;  HttpWebResponse httpResponse;        string          bodyText = "";  Stream          responseStream;  Byte[] RecvBytes = new Byte[Byte.MaxValue];      Int32 bytes;                  httpRequest = (HttpWebRequest) WebRequest.Create(szURL);  httpResponse = (HttpWebResponse) httpRequest.GetResponse();  responseStream = httpResponse.GetResponseStream();        while(true)        {   bytes = responseStream.Read(RecvBytes,   0,RecvBytes.Length);   if (bytes<=0) break;   bodyText += System.Text.Encoding.UTF8.GetString(RecvBytes,   0, bytes);4.2HTTP95Chapter 4  }  return bodyText; }VB.NETPublic Function getHTTP(ByVal szURL As String) As String  Dim httprequest As HttpWebRequest  Dim httpresponse As HttpWebResponse  Dim bodytext As String = ""  Dim responsestream As Stream  Dim bytes As Int32  Dim RecvBytes(Byte.MaxValue) As Byte  httprequest = CType(WebRequest.Create(szURL), _  HttpWebRequest)  httpresponse = CType(httprequest.GetResponse(), _  HttpWebResponse)  responsestream = httpresponse.GetResponseStream()  Do While (True)   bytes = responsestream.Read(RecvBytes, 0, _   RecvBytes.Length)   If bytes <= 0 Then Exit Do   bodytext += System.Text.Encoding.UTF8.GetString _   (RecvBytes, 0, bytes)  Loop  Return bodytextEnd FunctionTaking a closer look at this code, it should be relatively easy to identifyhow it operates. The ﬁrst action taken as this code is executed is that a staticmethod on the WebRequest class is called and passed the string szURL as aparameter. This creates a webRequest object that can be cast to an HttpWe-bRequest object, which will handle outgoing HTTP connections. Once we have an HttpWebRequest object, we can then send the HTTPrequest to the server and start receiving data back from the server by callingthe GetResponse method. The return value is then cast to anHttpWebResponse object, which is then held in the httPresponse variable. A response from a Web server is asynchronous by nature, so it is naturalto create a stream from this returning data and read it in as it becomes avail-able. To do this, we can create a stream by calling the GetResponseStreammethod. Once the stream is obtained, we can read bytes from it in chunks964.2HTTPof 256 bytes (byte.Max). Reading data in chunks improves performance.The chunk size can be arbitrarily chosen, but 256 is efﬁcient.The code sits in an inﬁnite loop until all of the incoming data isreceived. In a production environment, therefore, this type of action shouldbe contained within a separate thread. Once we have a string containing allof the HTML, we can simply dump it to screen. No other processing isrequired. You will also need some extra namespaces:C#using System.Net;using System.IO;VB.NETImports System.NetImports System.IOTo test the application, run it from Visual Studio, type in a Web siteaddress (not forgetting the http:// preﬁx), and press Capture. The HTMLsource will appear in the body (Figure 4.1).This is a very simple HTTP client, with no error handling, and is singlethreaded; however, it should sufﬁce for simpler applications.Figure 4.1HTTP clientapplication.4.2HTTP97Chapter 4Table 4.4 shows the signiﬁcant methods of HttpWebResponse.4.2.5Posting dataMany dynamic Web sites contain forms for login details, search criteria, orsimilar data. These forms are usually submitted via the POST method. Thisposes a problem, however, for any application that needs to query a pagethat lies behind such a form because you cannot specify posted data in theURL line.Table 4.4Signiﬁcant members of the HttpWebResponse class.Method or propertyMeaningContentEncodingGets the method used to encode the body of the. response. Returns String.ContentLengthGets the length of the content returned by the request. Returns Long.ContentTypeGets the content type of the response. Returns String.Cookies  Gets or sets the cookies associated with this request. May be used thus: Cookies[“name”].ToString().HeadersGets the headers associated with this response from the server. May be invoked thus: Headers[“Content-Type”].ToString().ResponseUri  Gets the URI of the Internet resource that responded to the request. May be invoked thus: RequestURI.ToString().ServerGets the name of the server that sent the response. Returns String.StatusCodeGets the status of the response. Returns the HttpStatusCode enumerated type. The StatusDescription returns a descriptive String.GetResponseHeaderGets the speciﬁed header contents that were returned with the response. Returns String.GetResponseStream  Gets the stream used to read the body of the response. No asynchronous variant. Returns stream.984.2HTTPFirst, prepare a page that handles POST requests. In this case, type the fol-lowing lines into a ﬁle called postTest.aspx in c:\inetpub\wwwroot (yourHTTP root):ASP.NET<%@ Page language="c#" Debug="true"%><script language="C#" runat="server">  public void Page_Load(Object sender, EventArgs E)   {    if (Request.Form["tbPost"]!=null)   {    Response.Write(Request.Form["tbPost"].ToString());   }  }</script><form method="post"> <input type="text" name="tbpost"> <input type="submit"></form>ASP.NET is a vast subject that lies outside the scope of this book; how-ever, for the sake of explaining the above example, a quick introduction isnecessary. ASP.NET is an extension to IIS that enables .NET code to beexecuted on receipt of requests for Web pages. This also provides means for.NET code to dynamically generate responses to clients in the form ofHTML, viewable on Web browsers.Incoming requests and outgoing data are mapped to objects in .NET,which can easily be read and manipulated. The most fundamental of theseobjects are the Request and Response objects. The Request object encapsu-lates the data sent from the Web browser to the server; of its properties, twoof the most important are the Form and QueryString collections. The Formcollection reads data sent from the client via the POST method, whereas theQueryString collection reads data sent from the client via the GET method.The Response object places data on the outgoing HTTP stream to besent to the client. One of its most important methods is Write. Thismethod is passed a string that will be rendered as HTML on the client. One of the features that makes ASP.NET more powerful than its predeces-sor, classic ASP, is its ability to model HTML elements as objects, not merely4.2HTTP99Chapter 4as input and output streams. For example, an input box would be typicallywritten in ASP.NET as <ASP:TEXTBOX id=”tbText” runat=”server”/>, andthe properties of this textbox could then be modiﬁed from code by accessingthe tbText object. In classic ASP, the only way to achieve such an effectwould be to include code within the textbox declaration, such as <inputtype=”text” <%=someCode%>>, which is less desirable because functionalcode is intermixed with HTML. ASP.NET provides better performance than classic ASP because it iscompiled on ﬁrst access (in-line model) or precompiled (code-behindmodel). It also leverages the .NET framework, which is much richer thanthe scripting languages available to ASP. The example above is appropriate for demonstrating the postingmethod.  Every Web scripting language handles posted data in much thesame way, so the technique is applicable to interfacing with any Web form. Web scripting languages share a common feature: some sections of thepage are rendered on the browser screen as HTML, and some are processedby the server and not displayed on the client. In the example, anythingmarked runat=”server” or preﬁxed <% will be processed by the server. When the user presses the submit button (<input type=”submit”>), thebrowser packages any user-entered data that was contained within the<form> tags and passes it back to the server as a POST request. The server parses out the data in the POST request once it is received.The server-side script can retrieve this data by accessing the Request.Formcollection. The Response.Write command prints this data back out to thebrowser.To try the page out, open a browser and point it at http://localhost/post-Test.aspx; type something into the textbox, and press Submit. Then you willsee the page refresh, and the text you typed appears above the form.Reopen the previous example and add a new textbox named tbPost.Click on the Capture button and modify the code as follows:C#private void btnCapture_Click(object sender, System.EventArgs e){    tbPost.Text = HttpUtility.UrlEncode(tbPost.Text);    tbResult.Text = getHTTP(tbUrl.Text,"tbPost="+tbPost.Text); }1004.2HTTPVB.NETPrivate Sub btnCapture_Click(ByVal sender As Object, _ByVal e As System.EventArgs) Handles btnCapture.Click    tbPost.Text = HttpUtility.UrlEncode(tbPost.Text)    tbResult.Text = getHTTP(tbUrl.Text,"tbPost="+tbPost.Text)End SubThe reason for the call to HttpUtility.UrlEncode is to convert the textentered by the user into a string that is safe for transport by HTTP. Thismeans the removal of white space (spaces are converted to “+”) and the con-version of nonalphanumeric characters, which is a requirement of theHTTP protocol.Once the data to post is encoded, it can be passed to the getHTTP func-tion, which is described below. It is a modiﬁed version of the code previ-ously listed.C#public string getHTTP(string szURL,string szPost){  HttpWebRequest  httprequest;  HttpWebResponse httpresponse;  StreamReader    bodyreader;  string          bodytext = "";  Stream          responsestream;  Stream          requestStream;  httprequest = (HttpWebRequest) WebRequest.Create(szURL);  httprequest.Method = "POST";  httprequest.ContentType =   "application/x-www-form-urlencoded";  httprequest.ContentLength = szPost.Length;        requestStream = httprequest.GetRequestStream();  requestStream.Write(Encoding.ASCII.GetBytes(szPost),0,  szPost.Length);  requestStream.Close();  httpresponse = (HttpWebResponse) httprequest.GetResponse();  responsestream = httpresponse.GetResponseStream();  bodyreader = new StreamReader(responsestream);  bodytext = bodyreader.ReadToEnd();  return bodytext;            }4.2HTTP101Chapter 4VB.NETPublic Function getHTTP(ByVal szURL As String, _ ByVal szPost As String) As String  Dim httprequest As HttpWebRequest  Dim httpresponse As HttpWebResponse  Dim bodyreader As StreamReader  Dim bodytext As String =  ""   Dim responsestream As Stream  Dim requestStream As Stream   httprequest = CType(WebRequest.Create(szURL), _  HttpWebRequest)  httprequest.Method = "POST"  httprequest.ContentType = _  "application/x-www-form-urlencoded"  httprequest.ContentLength = szPost.Length        requestStream = httprequest.GetRequestStream()  requestStream.Write(Encoding.ASCII.GetBytes(szPost), _  0,szPost.Length)  requestStream.Close()  httpresponse = CType(httprequest.GetResponse(), _  HttpWebResponse)  responsestream = httpresponse.GetResponseStream()  bodyreader = New StreamReader(responsestream)  bodytext = bodyreader.ReadToEnd()  Return bodytextEnd FunctionThis differs from the code to simply retrieve a Web page in that once theHttpWebRequest has been created, several parameters are set such that therequest also includes the posted data. The chunked reader loop is alsoreplaced with the ReadToEnd() method of StreamReader. This method maybe elegant, but it is not compatible with binary data.  The three settings that need to be changed are the request method, con-tent type, and content length. The request method is usually GET but nowmust be set to POST. The content type should be set to the MIME typeapplication/x-www-form-urlencoded, although this is not strictly neces-sary. The content length is simply the length of the data being posted,including the variable names, and after URL encoding. 1024.2HTTPThe data to be posted must then be sent to the server using the Writemethod on the request stream. Once the request has been created, it is sim-ply a matter of receiving the stream from the remote server and reading tothe end of the stream.Finally, we need namespaces for the HttpUtility and Encoding objects.You will need to make a reference to System.Web.dll by selecting Project→→→→Add Reference, as shown in Figure 4.2.C#using System.Web;using System.Text;using System.IO;using System.Net;VB.NETImports System.WebImports System.TextImports System.IOImports System.NetFigure 4.2Visual Studio.NET, AddReference dialog.4.2HTTP103Chapter 4To test the application, run it through Visual Studio .NET, enter http://localhost/postTest.aspx into the URL textbox, and add some other text intothe POST textbox. When you press Capture, you will see that the postedtext appears as part of the Web page (Figure 4.3).Table 4.5 shows the signiﬁcant members of HttpWebRequest.Figure 4.3HTTP clientapplication withPOST facility.Table 4.5Signiﬁcant members of HttpWebRequest .Method or PropertyMeaningAcceptGets or sets the value of the Accept HTTP header. Returns String.AllowAutoRedirectGets or sets a Boolean value that indicates whether the request should follow redirection (3xx) responses. ContentLengthGets or sets the Content-length HTTP header.ContentTypeGets or sets the value of the Content-type HTTP header.CookieContainerGets or sets the cookies associated with the request. May be invoked thus: CookieContainer.getCookies[“name”].ToString().1044.2HTTP4.2.6A note on cookiesHTTP does not maintain state information. It is therefore difﬁcult to dif-ferentiate between two users accessing a server or one user making tworequests. From the server’s point of view, it is possible for both users to havethe same IP address (e.g., if they are both going through the same proxyserver). If the service being accessed contained personal information, theuser to whom this data pertains is legally entitled to view this data, butother users should not be allowed access.In this situation, the client side of the connection needs to differentiateitself from other clients. This can be done in several ways, but for Web sites,cookies are the best solution.HeadersGets a collection of strings that are contained in the HTTP header. May be invoked thus: Headers[“Content-Type”].ToString().MethodGets or sets the method for the request. Can be set to GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.ProxyGets or sets proxy information for the request. Returns WebProxy.RefererGets or sets the value of the Referer HTTP header. Returns String.RequestUriGets the original URI of the request. Address is the URI after redirections. May be invoked thus: RequestURI.ToString().TimeoutGets or sets the time-out value. May be invoked thus Timeout=(int) new TimeSpan(0,0,30).TotalMilliseconds.TransferEncodingGets or sets the value of the Transfer-encoding HTTP header. Returns String.UserAgentGets or sets the value of the User-agent HTTP header. Returns String.GetResponseReturns a webResponse from an Internet resource. Its asynchronous variant is BeginGetResponse and EndGetResponse.Table 4.5Signiﬁcant members of HttpWebRequest (continued).Method or PropertyMeaning4.2HTTP105Chapter 4Cookies are small ﬁles stored in c:\windows\cookies (depending onyour Windows installation). They are placed there in one of two ways: bythe JavaScript document.cookie object, or by the set-cookie header inHTTP requests. These cookies remain on the client’s machine for a set timeand can be retrieved in JavaScript or in HTTP responses.Cookies are supported in .NET via the HttpWebResponse.Cookies andthe HttpWebRequest.CookieContainer objects. Cookies are domain speciﬁc; therefore, a cookie stored on www.library.comcannot be retrieved by www.bookshop.com. In circumstances where both sitesare afﬁliated with each other, the two sites might need to share session stateinformation. In this example, it would be advantageous for bookshop.comto know a user’s reading preferences, so that it could advertise the most rel-evant titles. The trick to copying cookies across domains is to convert the cookiesinto text, pass the text between the servers, and pass the cookies back to theclient from the foreign server. .NET offers a facility to serialize cookies,which is ideal for the purpose.4.2.7A WYSIWYG editorWYSIWYG (what you see is what you get) is a term used to describe Weband graphics editors that enable you to naturally manipulate graphical out-put, without having to be concerned with the underlying code. This featureis a handy way to let users be more creative in the type of textual messagesor documents they create, without requiring them to take a crash course inHTML. Internet Explorer can run in a special design mode, which is acceptableas a WYSIWYG editor. The trick to accessing design mode in InternetExplorer is simply to set the property WebBrowser.Document.designMode toOn. Users can type directly into the Internet Explorer window and use well-known shortcut keys to format text (e.g., Ctrl + B, Bold; Ctrl + I, Italic;Ctrl + U, Underline). By right-clicking on Internet Explorer in designmode, a user can include images, add hyperlinks, and switch to browsermode. When an image is included in the design view, it can be moved andscaled by clicking and dragging on the edge of the image. More advanced features can be accessed via Internet Explorer’sexecCommand function. Only FontName, FontSize, and ForeColor are used inthe following sample program, but here is a list of the commands used byInternet Explorer.1064.2HTTPOther functionality not included in this list can be implemented bydynamically modifying the underlying HTML.To start coding this application, open a new project in Visual Studio.NET. Add a reference to Microsoft.mshtml by clicking Project→→→→Add Ref-erence. Scroll down the list until you ﬁnd Microsoft.mshtml, highlight it,and press OK. If you have not already done so from Chapter 1’s example,add Internet Explorer to the toolbox. To do this, right-click on the toolboxand select Customize Toolbox. Scroll down the list under the COM com-ponents tab until you see Microsoft Web Browser. Check the box oppositeit, and press OK.Table 4.6Parameters of Internet Explorer’s execCommand function .CommandMeaningBoldInserts a <B> tag in HTMLCopyCopies text into the clipboardPastePastes text from the clipboardInsertUnorderedList Creates a bulleted list, <UL> in HTMLIndentTabulates text farther right on the pageOutdentRetabulates text left on the pageItalicInserts an <I> tag in HTMLUnderlineInserts an <U> tag in HTMLCreateLinkCreates a hyperlink to another Web pageUnLinkRemoves a hyperlink from textFontNameSets the font family of a piece of textFontSizeSets the font size of a piece of textCreateBookmarkCreates a bookmark on a piece of textForeColorSets the color of the selected textSelectAllIs equivalent to pressing CTRL + AJustifyLeftMoves all text as far left as space allowsJustifyRightMoves all text as far right as space allowsJustifyCenterMoves all selected text as close to the center as possibleSaveAsSaves the page to disk4.2HTTP107Chapter 4Draw a Tab control on the form named tabControl. Click on thetabPages property in the properties window and add two tab pages, labeledPreview and HTML. Draw the Microsoft Web Browser control onto thepreview tab page and name the control WebBrowser. Add three buttons tothe Preview tab page, named btnViewHTML, btnFont, and btnColor. In theHTML tab page, add a textbox named tbHTML, and set its multiline prop-erty to true. Also add a button to the HTML tab page named btnPreview.Drag a Color Dialog control onto the form, and name it colorDialog.Drag a Font Dialog control onto the form and name it fontDialog.Double-click on the form, and add the following code:C#private void Form1_Load(object sender, System.EventArgs e){  object any = null;  object url = "about:blank";  WebBrowser.Navigate2(ref url,ref any,ref any,ref any,ref any);  Application.DoEvents();  ((HTMLDocument)WebBrowser.Document).designMode="On";}VB.NETPrivate  Sub Form1_Load(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim url As Object =  "about:blank"   WebBrowser.Navigate2( url)  Application.DoEvents()  (CType(WebBrowser.Document, HTMLDocument)).designMode="On"End SubIn order to access the HTML contained within the Web browser page, itmust ﬁrst point to a valid URL that contains some HTML source. In thiscase, the URL about:blank is used. This page contains nothing more than<HTML></HTML>, but is sufﬁcient for the needs of this application. TheDoEvents method releases a little processor time to allow the Web browserto load this page. The Document property of the Web browser contains theobject model for the page, but it must ﬁrst be cast to an HTMLDocumentobject to be of use. The designMode property of Internet Explorer is thenset to On to enable WYSIWYG editing. 1084.2HTTPClick on the view HTML button on the Preview tab page and enter thefollowing code:C#private void btnViewHTML_Click(object sender, System.EventArgs e){  tbHTML.Text=(  (HTMLDocument)WebBrowser.Document).body.innerHTML;}VB.NETPrivate  Sub btnViewHTML_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  tbHTML.Text= _  (CType(WebBrowser.Document, HTMLDocument)).body.innerHTMLEnd SubThis button extracts the HTML from the Web Browser control andplaces it into the HTML-viewer textbox. Again, the Document propertymust be cast to an HTMLDocument object in order to access the page objectmodel. In this case, the body.innerHTML property contains the page source.If you required the page source less the HTML tags, then body.innerTextwould be of interest. Click on the corresponding Preview button on the HTML tab page, andenter the following code:C#private void btnPreview_Click(object sender, System.EventArgs e){ ((HTMLDocument)WebBrowser.Document).body.innerHTML= tbHTML.Text;  }VB.NETPrivate Sub btnPreview_Click(ByVal sender As Object, _ByVal e As System.EventArgs) (CType(WebBrowser.Document, _ HTMLDocument)).body.innerHTML=tbHTML.TextEnd Sub4.2HTTP109Chapter 4This code simply performs the reverse of the preceding code, replacingthe HTML behind the Web browser with the HTML typed into the text-box. Click on the Font button on the Preview tab page, and enter the follow-ing code:C#private void btnFont_Click(object sender, System.EventArgs e){  fontDialog.ShowDialog();  HTMLDocument doc = (HTMLDocument)WebBrowser.Document;  object selection= doc.selection.createRange();  doc.execCommand("FontName",false,  fontDialog.Font.FontFamily.Name);  doc.execCommand("FontSize",false,fontDialog.Font.Size);  ((IHTMLTxtRange)selection).select();}VB.NETPrivate  Sub btnFont_Click(ByVal sender As Object, _ ByVal e As System.EventArgs)  fontDialog.ShowDialog()  Dim doc As HTMLDocument = CType(WebBrowser.Document, _    HTMLDocument)  Dim selection As Object =  doc.selection.createRange()   doc.execCommand("FontName",False,fontDialog.Font. _  FontFamily.Name)  doc.execCommand("FontSize",False,fontDialog.Font.Size)  (CType(selection, IHTMLTxtRange)).select()End SubPressing the Font button will bring up the standard font dialog box(Figure 4.4), which allows the user to select any font held on the system andits size. Other properties that may be available on this screen, such as sub-script, strikethrough, and so on, are not reﬂected in the WYSIWYG editor.This works by ﬁrst capturing a reference to any selected text on the screenusing the selection.createRange() method. The execCommand method iscalled twice, ﬁrst to apply the font family to the selected text and then thefont size. The selection is then cast to an IHTMLTxtRange interface, whichexposes the select method and commits the changes to memory.1104.2HTTPNow click on the Color button on the Preview tab page, and enter thefollowing code:C#private void btnColor_Click(object sender, System.EventArgs e){  colorDialog.ShowDialog();  string colorCode = "#" +       toHex(colorDialog.Color.R) +      toHex(colorDialog.Color.G) +      toHex(colorDialog.Color.B);  HTMLDocument doc = (HTMLDocument)WebBrowser.Document;  object selection = doc.selection.createRange();        doc.execCommand("ForeColor",false,colorCode);  ((IHTMLTxtRange)selection).select();}VB.NETPrivate  Sub btnColor_Click(ByVal sender As Object, _Figure 4.4Font-chooser dialogbox.4.2HTTP111Chapter 4  ByVal e As System.EventArgs)  colorDialog.ShowDialog()  String colorCode = "#" + _      toHex(colorDialog.Color.R) + _      toHex(colorDialog.Color.G) + _      toHex(colorDialog.Color.B)  Dim doc As HTMLDocument = CType(WebBrowser.Document, _  HTMLDocument)  Dim selection As Object =  doc.selection.createRange()   doc.execCommand("ForeColor",False,colorCode)  (CType(selection, IHTMLTxtRange)).select()End SubPressing the Color button brings up the standard Color dialog box (Fig-ure 4.5). When a color is chosen, the selected color is applied to anyselected text. This code brings up the Color dialog box by calling the Show-Dialog method. The color returned can be expressed in terms of its red (R),green (G), and blue (B) constituents. These values are in decimal format, inthe range 0 (least intense) to 255 (most intense). HTML expresses colors inthe form #RRGGBB, where RR, GG, and BB are hexadecimal equivalentsFigure 4.5Color-picker dialogbox.1124.2HTTPof the R, G, and B values. To give a few examples, #FF0000 is bright red,#FFFFFF is white, and #000000 is black. Once again, a handle to the selected text is obtained in the same way asbefore. The execCommand method is called and passed ForeColor, alongwith the HTML color code. The selected text is cast to an IHTMLTxtRangeinterface and committed to memory with the Select method as before. The above code calls the function toHex to convert the numeric valuesreturned from the colorDialog control to hexadecimal values, which arerequired by Internet Explorer. Enter the following code:C#public string toHex(int digit){  string hexDigit = digit.ToString("X");  if (hexDigit.length == 1){  hexDigit = "0" + hexDigit;  }    return hexDigit;}VB.NETPublic Function toHex(ByVal number As Integer) As String     Dim hexByte As String     hexByte = Hex(number).ToString()     If hexByte.Length = 1 Then         hexByte = "0" & hexByte     End If     Return hexByte End FunctionFinally, the relevant namespaces are required:C#using mshtml;VB.NETImports mshtml4.3Web servers113Chapter 4To test this application, run it from Visual Studio .NET. Type into theWeb Browser control under the Preview tab. Press the Font button tochange the style and size of any text that is selected. Press the Color buttonto change the color of selected text. You can insert images by right-clickingand selecting Insert image (special thanks to Bella for posing for this photo-graph!). Press the view HTML button, then switch to the HTML tab pageto view the autogenerated HTML (Figure 4.6).4.3Web serversOne may ask why you should develop a server in .NET when IIS is freelyavailable. An in-house-developed server has some advantages, such as thefollowing:(cid:1)Web server can be installed as part of an application, without requiringthe user to install IIS manually from the Windows installation CD.(cid:1)IIS will not install on the Windows XP Home Edition, which consti-tutes a signiﬁcant portion of Windows users.Figure 4.6HTML editorapplication.1144.3Web servers4.3.1Implementing a Web serverStart a new Visual Studio .NET project as usual. Draw two textboxes,tbPath and tbPort, onto the form, followed by a button, btnStart, and alist 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 anoverlap of code between this example and the TCP server in the previouschapter. The server has to be multithreaded, so the ﬁrst step is to declare anArray List of sockets:C#public class Form1 : System.Windows.Forms.Form{  private ArrayList alSockets;  ...VB.NETPublic Class Form1 Inherits System.Windows.Forms.Form     Private alSockets As ArrayList  ...Every HTTP server has an HTTP root, which is a path to a folder onyour hard disk from which the server will retrieve Web pages. IIS has adefault HTTP root of C:\inetpub\wwwroot; in this case, we shall use thepath in which the application is saved. To obtain the application path, we can use Application.Executable-Path, which returns not only the path but also the ﬁlename, and thus wecan trim off all characters after the last backslash. C#private void Form1_Load(object sender, System.EventArgs e){  tbPath.Text = Application.ExecutablePath;  // trim off filename, to get the path  tbPath.Text =  tbPath.Text.Substring(0,tbPath.Text.LastIndexOf("\\"));}4.3Web servers115Chapter 4VB.NETPrivate  Sub Form1_Load(ByVal sender As Object, _ByVal e As System.EventArgs)  tbPath.Text = Application.ExecutablePath  ' trim off filename, to get the path  tbPath.Text = _  tbPath.Text.Substring(0,tbPath.Text.LastIndexOf("\"))End SubClicking the Start button will initialize the Array List of sockets andstart the main server thread. Click btnStart:C#private void btnStart_Click(object sender, System.EventArgs e){  alSockets = new ArrayList();  Thread thdListener =   new Thread(new ThreadStart(listenerThread));  thdListener.Start();}VB.NETPrivate  Sub btnStart_Click(ByVal sender As Object, _ ByVal e As System.EventArgs)  alSockets = New ArrayList()  Dim thdListener As Thread =  New Thread(New _   ThreadStart( AddressOf listenerThread))   thdListener.Start()End SubThe listenerThread function manages new incoming connections,allocating each new connection to a new thread, where the client’s requestswill be handled. HTTP operates over port 80, but if any other application is using port80 at the same time (such as IIS), the code will crash. Therefore, the portfor this server is conﬁgurable. The ﬁrst step is to start the TcpListener onthe port speciﬁed in tbPort.Text.This thread runs in an inﬁnite loop, constantly blocking on theAcceptSocket method. Once the socket is connected, some text is writtento the screen, and a new thread calls the handlerSocket function.1164.3Web serversThe reason for the lock(this) command is that handlerSocketretrieves the socket by reading the last entry in ArrayList. In the case wheretwo connections arrive simultaneously, two entries will be written toArrayList, and one of the calls to handlerSocket will use the wrongsocket. Lock ensures that the spawning of the new thread cannot happen atthe same time as the acceptance of a new socket.C#public void listenerThread(){  int port =0;  port = Convert.ToInt16(tbPort.Text);  TcpListener tcpListener = new TcpListener(port);  tcpListener.Start();  while(true)  {    Socket handlerSocket = tcpListener.AcceptSocket();    if (handlerSocket.Connected)    {     lbConnections.Items.Add(     handlerSocket.RemoteEndPoint.ToString() + " connected."     );     lock(this)     {      alSockets.Add(handlerSocket);      ThreadStart thdstHandler = new      ThreadStart(handlerThread);      Thread thdHandler = new Thread(thdstHandler);      thdHandler.Start();     }    }  }  }      VB.NETPublic  Sub listenerThread()  Dim port As Integer = 0   port = Convert.ToInt16(tbPort.Text)  Dim tcpListener As TcpListener =  New TcpListener(port)   tcpListener.Start()  do4.3Web servers117Chapter 4    Dim handlerSocket As Socket =  tcpListener.AcceptSocket()     If handlerSocket.Connected = true then        lbConnections.Items.Add( _      handlerSocket.RemoteEndPoint.ToString() + " _       connected.")      syncLock(me)        alSockets.Add(handlerSocket)        Dim thdstHandler As ThreadStart =  New  _          ThreadStart(AddressOf handlerThread)         Dim thdHandler As Thread =  New  _          Thread(thdstHandler)         thdHandler.Start()      end syncLock    end if  loopEnd subThe handlerThread function is where HTTP is implemented, albeitminimally. Taking a closer look at the code should better explain what ishappening here. The ﬁrst task this thread must perform, before it can communicate withthe client to which it has been allocated, is to retrieve a socket from the topof the public ArrayList. Once this socket has been obtained, it can thencreate a stream to this client by passing the socket to the constructor of aNetworkStream. To make processing of the stream easier, a StreamReader is used to readone line from the incoming NetworkStream. This line is assumed to be:GET <some URL path> HTTP/1.1HTTP posts will be handled identically to HTTP gets. Because thisserver has no support for server-side scripting, there is no use for anythingelse in the HTTP POST data, or anything else in the HTTP Request headerfor that matter. Assuming that the HTTP request is properly formatted, we can extractthe requested page URL from this line by splitting it into an array of strings(verbs[]), delimited by the space character.  The next task is to convert a URL path into a physical path on the localhard drive. This involves four steps:1184.3Web servers1.Converting forward slashes to backslashes 2.Trimming off any query string (i.e., everything after the questionmark)3.Appending a default page, if none is speciﬁed; in this case,“index.htm”4.Preﬁxing the URL path with the HTTP root Once the physical path is resolved, it can be read from disk and sent outon the network stream. It is reported on screen, and then the socket isclosed. This server does not return any HTTP headers, which means theclient will have to determine how to display the data being sent to it. C#public void handlerThread(){  Socket handlerSocket = (  Socket)alSockets[alSockets.Count-1];  String streamData = "";  String filename = "";  String[] verbs;  StreamReader    quickRead;            NetworkStream networkStream =   new NetworkStream(handlerSocket);  quickRead = new StreamReader(networkStream);  streamData = quickRead.ReadLine();  verbs = streamData.Split(" ".ToCharArray());  // Assume verbs[0]=GET   filename = verbs[1].Replace("/","\\");  if (filename.IndexOf("?")!=-1)  {    // Trim of anything after a question mark (Querystring)    filename = filename.Substring(0,filename.IndexOf("?"));  }    if (filename.EndsWith("\\"))  {    // Add a default page if not specified    filename+="index.htm";  }4.3Web servers119Chapter 4  filename = tbPath.Text + filename;  FileStream  fs = new FileStream(filename,  FileMode.OpenOrCreate);  fs.Seek(0, SeekOrigin.Begin);  byte[] fileContents= new byte[fs.Length];  fs.Read(fileContents, 0, (int)fs.Length);  fs.Close();  // optional: modify fileContents to include HTTP header.      handlerSocket.Send(fileContents);  lbConnections.Items.Add(filename);   handlerSocket.Close();}VB.NETPublic  Sub handlerThread()  Dim handlerSocket As Socket = _  CType(alSockets(alSockets.Count-1), Socket)  Dim streamData As String =  ""   Dim filename As String =  ""   Dim verbs() As String  Dim quickRead As StreamReader  Dim networkStream As NetworkStream = New _  NetworkStream(handlerSocket)   quickRead = New StreamReader(networkStream)  streamData = quickRead.ReadLine()  verbs = streamData.Split(" ".ToCharArray())  ' Assume verbs[0]=GET   filename = verbs(1).Replace("/","\\")  If filename.IndexOf("?")<>-1 Then    ' Trim of anything after a question mark (Querystring)    filename = filename.Substring(0,filename.IndexOf("?"))  End If   If filename.EndsWith("\\") Then    ' Add a default page if not specified    filename+="index.htm"  End If  filename = tbPath.Text + filename  Dim fs As FileStream =  New _1204.3Web servers  FileStream(filename,FileMode.OpenOrCreate)   fs.Seek(0, SeekOrigin.Begin)  Dim fileContents() As Byte =  New Byte(fs.Length) {}   fs.Read(fileContents, 0, CType(fs.Length, Integer))  fs.Close()   ' optional: modify fileContents to include HTTP header.       handlerSocket.Send(fileContents)  lbConnections.Items.Add(filename)   handlerSocket.Close()End SubMost modern browsers can determine how best to display the data beingsent to them, without the need for Content-Type headers. For instance,Internet Explorer can tell the difference between JPEG image data andHTML by looking for the standard JPEG header in the received data; how-ever, this system is not perfect. A simple example is the difference between how XML is rendered on abrowser window and how HTML is displayed. Without the Content-Typeheader, Internet Explorer will mistake all XML (excluding the <?xml?> tag)as HTML. You can see this by viewing a simple XML ﬁle containing thetext <a><b/></a> through this server.And, the usual namespaces are thrown in:C#using System.Threading;using System.Net;using System.Net.Sockets;using System.Text;using System.IO;VB.NETImports System.ThreadingImports System.NetImports System.Net.SocketsImports System.TextImports System.IOTo test the server, you will need a simple HTML page. Save the follow-ing text as index.htm in the same folder where the executable is built (theHTTP root).4.3Web servers121Chapter 4HTML<html> Hello world!</html>Run the server from Visual Studio .NET, change the port to 90 if youare running IIS, and press Start. Open a browser and type in http://localhost:90.  Localhost should be replaced by the IP address of theserver, if you are running the server on a second computer (Figure 4.7).As mentioned previously, the server does not return HTTP headers. It isworthwhile to extend the example to include one of the more importantheaders, Content-Type, to save data from being misinterpreted at the client.Figure 4.7HTTP serverapplication.1224.3Web serversFirst, implement a new function called getMime(). This will retrieve aﬁle’s MIME type from the computer’s registry from its ﬁle extension:C#public string getMime(string filename){  FileInfo thisFile = new FileInfo(filename);  RegistryKey key = Registry.ClassesRoot;  key = key.OpenSubKey(thisFile.Extension);  return key.GetValue("Content Type").ToString();}VB.NETPublic Function getMime(ByVal filename As String) As String  Dim thisFile As FileInfo =  New FileInfo(filename)   Dim key As RegistryKey =  Registry.ClassesRoot   key = key.OpenSubKey(thisFile.Extension)  Return key.GetValue("Content Type").ToString()End FunctionIf you have never used Windows registry before, this code may need alittle explaining. The Windows registry is a repository for information thatholds the vast amount of settings and preferences that keep Windows tick-ing over. You can view and edit the registry using Registry Editor (Figure4.8); start this by clicking Start→→→→Run and typing regedit or regedt32.To view MIME types that correspond with ﬁle type extensions, click onHKEY_CLASSES_ROOT, scroll down to the ﬁle extension in question,and look at the Content Type key on the right-hand side of the screen. Figure 4.8Registry Editorutility.4.3Web servers123Chapter 4This data is accessed programmatically by ﬁrst extracting the ﬁle typeextension using the Extension property of a FileInfo object. The ﬁrst stepin drilling down through the registry data is to open the root key. In thiscase, it is Registry.ClassesRoot. The .html subkey is then opened using the openSubKey method.Finally, the Content Type value is retrieved using the getValue statementand returned as a string to the calling function. Now the ﬁnal call to the Send method must be replaced by a slightlymore elaborate sending procedure, which issues correct HTTP headers:C#handlerSocket.Send(fileContents);VB.NEThandlerSocket.Send(fileContents) These become:C#string responseString = "HTTP/1.1 200 OK\r\nContent-Type: " +        getMime(filename) + "\r\n\r\n";System.Collections.ArrayList al = new ArrayList();al.AddRange(Encoding.ASCII.GetBytes(responseString));al.AddRange(fileContents);            handlerSocket.Send((byte[])al.ToArray((new byte()).GetType()));VB.NETDim responseString As StringresponseString = "HTTP/1.1 200 OK" + vbCrLf + _ "Content-Type: " + getMime(filename) + vbCrLf + vbCrLf Dim al As System.Collections.ArrayList = New ArrayListal.AddRange(Encoding.ASCII.GetBytes(responseString))al.AddRange(fileContents)handlerSocket.Send(CType( _al.ToArray((New Byte).GetType()), Byte()))Finally, to support the registry access functionality, we need to includean extra namespace:1244.4System.Net.HttpWebListenerC#using Microsoft.Win32;VB.NETImports Microsoft.Win32To demonstrate the difference this makes to running the server, create twoﬁles, test.txt and test.xml, both containing the text <a><b/></a>. Savethem both in the HTTP root of your server and type in http:localhost/test.xmland http:localhost/test.txt. You will notice that test.xml will be rendered as acollapsible tree, and the text ﬁle will be shown as a series of characters.4.4System.Net.HttpWebListenerIn .NET 2 Whidbey, a more elegant solution for implementing Web serversexists, namely the HttpWebListener class. This class leverages the Http.sysdriver (where available) to deliver unprecedented performance, and inte-grates many features, such as SSL encryption and authentication, whichwould be difﬁcult to develop from the ground up. The HttpWebListener class consists of the signiﬁcant methods andproperties shown in Table 4.7.Table 4.7Signiﬁcant members of the HttpWebListener class .Method or Property DescriptionAbort / CloseDestroys the request queue.AddPrefixAdds a preﬁx to the Web listener.BeginGetRequestAwaits a client request asynchronously. Returns IasyncResult.EndGetRequestHandles client request. Returns ListenerWebRequest.GetPrefixesRetrieves all handled preﬁxes. Returns String[]v.GetRequestAwaits a client request synchronously. Returns ListenerWebRequest.RemoveAll Removes all preﬁxes.RemovePrefixRemoves a speciﬁed preﬁx.4.4System.Net.HttpWebListener125Chapter 4The ListenerWebRequest returned by GetRequest contains the signiﬁ-cant methods and properties shown in Table 4.8.Start Starts the Web server.Stop Stops the Web server.AuthenticationSchemeSets the means by which the server authenticates clients. Returns AuthenticationScheme (i.e., Basic, Digest, NTLM).IsListeningDetermines if the server is running. Returns Boolean.Realm stringIf Basic or Digest authentication schemes are selected, gets the realm directive. Returns String.Table 4.8Method or PropertyDescriptionAbort / CloseCloses the client connection.GetRequestStreamRetrieves a reference to the stream sent from the client. Returns Stream.GetResponse Retrieves a reference to the response to be sent to the client. Returns ListenerWebResponse.AcceptGets the Accept HTTP header sent in the client request. Returns String.ClientCertificateGets the digital certiﬁcate sent with the client request. Returns X509Certificate.ClientCertificateErrorDetermines if any errors were present in the client certiﬁcate. Returns int32.ConnectionGets the Connection HTTP header sent in the client request. Returns String.ContentLengthGets the length of any data posted in the client request. Returns int64.ContentType Gets the ContentType HTTP header sent in the client request. Returns String.Table 4.7Signiﬁcant members of the HttpWebListener class (continued).Method or Property Description1264.4System.Net.HttpWebListenerThe ListenerWebResponse returned by GetResponse contains the sig-niﬁcant methods and properties listed in Table 4.9.Expect Gets the Expect HTTP header sent in the client request. Returns String.HasEntityBody Determines if the client request had an Entity body. Returns Boolean.Headers  Gets a reference to the set of HTTP headers sent from the client. Returns WebHeaderCollection.HostGets the Host HTTP header sent in the client request. Returns String.IdentityDetermines the identity credentials in the client request. Returns Identity.IfModifiedSince Gets the IfModifiedSince header sent in the client request. Returns DateTime.KeepAlive BooleanDetermines if the client sent Connection: Keep-Alive in its request. Returns Boolean.LocalEndPoint  Determines the local logical endpoint of the communication. Returns IPEndPoint.Method Gets the HTTP send method (i.e., GET, POST) in the client request. Returns String.ProtocolVersionDetermines the HTTP version used by the client. Returns Version.RawUri Gets the URI requested by the client. Returns String.RefererGets the Referer HTTP header sent in the client request. Returns String.RemoteEndPointDetermines the remote logical endpoint of the communication. Returns IPEndPoint.RequestUriGets the URI requested by the client. Returns Uri.UserAgent Gets the UserAgent HTTP header sent in the client request. Returns String.Table 4.84.4System.Net.HttpWebListener127Chapter 4Table 4.9Method or PropertyDescriptionAbort / CloseDisconnects the client.GetResponseStream Retrieves a reference to the stream to be returned to the client. Returns Stream.ContentLengthSets the length of data to be sent back to the client. Returns int64.ContentTypeSets the ContentType HTTP header to be sent back the client. Returns String.DateSets the Date HTTP header to be sent back to the client. Returns DateTime.EntityDelimitationDetermines how the response content should be delimited (i.e., ContentLength, Chunked, Raw). Returns EntityDelimitation.Headers Retrieves a reference to the HTTP headers to be sent back to the client. Returns WebHeaderCollection.KeepAliveDetermines if Connection: Keep-Alive should be set in the HTTP headers returned to the client. Returns Boolean.LastModifiedSets the LastModified HTTP header to be sent back to the client. Returns DateTime.ProtocolVersion Sets the HTTP protocol version to be used in communicating with the client. Returns Version.RawHeadersRetrieves a reference to the HTTP headers to be sent back to the client. Returns Byte[].RequestRetrieves a reference to the request that initiated the response. Returns ListenerWebRequest.ServerSets the Server HTTP header to be sent back to the client. Returns String.StatusCodeSets the HTTP status code to be sent to the client. Returns httpstatuscode (e.g., OK, Moved, NotFound).StatusDescription Sets the HTTP status description to be sent to the client. Returns String.1284.5Mobile Web browsers4.5Mobile Web browsersNot all HTTP clients are PCs. Many people use their mobile phones toaccess the Internet. Some applications are inﬁnitely more useful when avail-able wirelessly. Even though mobile phones ferry data in a totally differentway from wired networks, a wireless application protocol (WAP) phone willcommunicate via a WAP gateway, which converts mobile phone signalsinto TCP/IP and accesses servers in much the same way as browsers.WAP runs over HTTP and wireless transfer protocol (WTP), with a fewextra headers thrown into the HTTP request. The following is a sampleHTTP request generated by a WAP phone:GET / HTTP/1.1Accept-Charset: ISO-8859-1Accept-Language: enContent-Type: application/x-www-form-urlencodedx-up-subno: Fiach_hopx-upfax-accepts: nonex-up-uplink: nonex-up-devcap-smartdialing: 1x-up-devcap-screendepth: 1x-up-devcap-iscolor: 0x-up-devcap-immed-alert: 1x-up-devcap-numsoftkeys: 3x-up-devcap-screenchars: 15,4Accept: application/x-hdmlc, application/x-up-alert, application/x-up-cacheop, application/x-up-device, application/x-up-digestentry, text/x-hdml;version=3.1, text/x-hdml;version=3.0, text/x-hdml;version=2.0, text/x-wap.wml, text/vnd.wap.wml, */*, image/bmp, text/htmlUser-Agent: UP.Browser/3.1-ALAV UP.Link/3.2Host: 127.0.0.1:50Note: x-up-subno is set to the computer username followed by the com-puter name.WAP clients and PC browsers differ most in the response. WAP clientscannot read HTML and use a simpler language, wireless markup language(WML), which has a MIME type text/vnd.wap.wml.4.5Mobile Web browsers129Chapter 4A minimal page in WML is as follows:WML<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"      "http://www.wapforum.org/DTD/wml_1.1.xml"><wml> <card>  <p align="left">   <b>Title</b><br/>    body  </p> </card></wml>To view this page on a WAP phone, save the above text to index.wml.Ensure that the MIME type is registered on your computer by adding a reg-istry key to HKEY_CLASSES_ROOT\.wml named Content Type with the valuetext/vnd.wap.wml. Run the server as described in the previous section, and copy index.wmlinto the HTTP root as displayed. Ensure that your computer is online andhas an externally visible IP address. Connect your mobile phone to the Inter-net and type your IP address into it, followed by /index.wml (Figure 4.9).Note: If you do not have a WAP phone, you can use a WAP emulator suchas the UP.SDK from www.openwave.com.Not all wireless HTTP clients read WML. A competing technology,iMode, which is the most widely used technology in Asia, offers a similar,yet incompatible, system. iMode reads compact HTML (cHTML), whichis a stripped-down version of the language with features such as frames,tables, and even JPEG images explicitly unsupported; however, iMode hasFigure 4.9Sample WMLpage.1304.6Conclusiongood support for Unicode and can adequately display many Web pagesdesigned for PCs. An iMode browser can be recognized by the word DoCoMo in the useragent HTTP request header.4.5.1Mobile Web SDKWhen implementing WAP compatibility in a Web application, it is worthconsidering the .NET Mobile Web SDK. This enables you to developapplications for WAP in the same way as an ASP.NET Web application.Therefore, there is no need to learn WML.Note: Utilities are available to convert HTML to WML on-the-ﬂy, but the.NET Mobile Web SDK is freely available. A sample page could be as follows:ASP.NET<%@ Page Inherits="System.Mobile.UI.MobilePage" language="c#" %><%@ Register TagPrefix="mobile" Namespace="System.Mobile.UI" %><mobile:Form runat="server"><mobile:Label runat="server"> Hello world!</mobile:Label></mobile:Form>To try this page out, save it as mobile.aspx in your IIS root (usually c:\inetpub\wwwroot). Ensure that your computer is online and has an exter-nally visible IP address. Connect your mobile phone to the Internet, andtype your IP address into it, followed by /mobile.aspx.4.6ConclusionThis chapter should have provided enough information to link your .NETapplication into data from the Web, to illustrate the point that HTTP isnot only used for Web browsing and the WAP.The next chapter deals with sending and receiving email from .NETapplications.  131 5 SMTP and POP3: Communicating with email Servers 5.1Introduction More emails are sent every day than postal mail. Why? Because email ischeap, informal, fast, and can be picked up at the receiver’s convenience.Emails can be automatically generated and sent, making them ideal forautomated status notiﬁcation. One day, you may receive an email fromyour home sprinkler system saying simply, “Your house is on ﬁre.”After reading this chapter you will be able to send and receive emailsfrom your .NET applications. These features can be useful for customersupport systems, collaborative personnel management, and many othertypes of applications. This chapter begins by describing how emails can be constructed andsent, using either a socket-level approach, or by using in-built .NET classes.Immediately following that, is a description on how emails may be received,again, by either using a socket level approach, or a higher-level methodol-ogy, leveraging Microsoft Outlook.  5.2Sending an email Every email must have a destination email address. An email address takesthe following form: < Username >@< domain name > The domain name in an email address generally does not include the“www” preﬁx, which is common for Web site addresses. Despite that, thedomain name is globally recognized under the DNS system. The usernameis recognized only by the recipient mail server.  132 5.3 SMTP Emails are not immediately delivered to the recipient; instead, they areinitially sent to your ISP’s or company’s mail server. From there, they areforwarded to the recipient’s mail server or held for a period of time until therecipient’s mail server accepts the email. Emails are sent using the simplemail transfer protocol (SMTP), which is described in detail later.In order to determine the recipient’s mail server, a DNS mail exchange(MX) query is issued to the local DNS server for that domain name. Thatcomputer will then return details of where the server or servers that handleincoming mail are located.  Note:  Most ISPs have only one incoming mail server, but Hotmail.com has more than 10 mail servers.You will always be told the IP address of your SMTP server. Unfortu-nately, you cannot use an SMTP server from another ISP because it willblock you with an error message such as “Relaying denied.” Microsoft virtual SMTP server is available for most versions of Win-dows and generally appears under IIS when installed. 5.3SMTP SMTP is used to send, but not receive, emails. Every mail server in theworld must conform to the SMTP standard in order to send emails reli-ably regardless of destination. The deﬁnitive guide to SMTP is held by theInternet Engineering Task Force (IETF) under RFC 821 at  www.ietf.org/rfc/rfc0821.txt. The deﬁnitive guides to most major protocols are held at the IETF.They are free to download and should be consulted when you are develop-ing network applications that are designed to work with preexisting orthird-party clients or servers. SMTP is not a difﬁcult protocol to implement from the ground up;however, it is natively supported from .NET and, thus, would be a waste oftime to redevelop. Also, many commercial email components are available,which can be imported into your application. One of the most popular isAspEmail from Persits Software. The demo version of this component isadequate for most applications. 5.3 SMTP133Chapter 5 5.3.1Implementing SMTP  SMTP operates on TCP port 25. Before sitting down to code, you shouldﬁrst ﬁnd out the IP address of your ISP’s SMTP server. In the examplesbelow, the SMTP server  smtp.ntlworld.com  is used. You should replacethis 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 con-nection, rather than a keyboard. A good way to test the protocol is to open telnet by clicking Start  →→→→ Runand type  telnet . In Windows NT, 2000, and XP, type  o smtp.ntl-world.com 25 .   In prior versions of Windows, click File  →→→→ Connect, andthen type  smtp.ntlworld.com  into the connection box and 25 into the portbox. Then press Connect.Once the client establishes a TCP connection to the server on port 25,the server will always reply with  220 < some greeting message ><enter> . Anumber is always included at the start of every server response. Any numberbeginning with 5 is an error and should be dealt with; everything else canbe ignored.The client must then send a greeting back to the server. This is merely aformality and does not contain any useful information. The format is  HELLOserver <enter> , and the server should reply with  250 server <enter> .The next step is to send a contact email address for the sender. This issent in the format  MAIL FROM:< email address ><enter> . The server shouldreply  250 OK<enter> . Following that, the recipient must be indicated. To do this,  RCPTTO:<email address><enter>  is used. The server should reply  250OK<enter> . To create the body of the email, the client sends the command DATA<enter> . To this the server should reply  354 < some instruc-tions ><enter> .The client can then send as much text as required to make up the bodyof the email. It is recommended to split the mail over several lines becauseof restrictions in some mail servers. To indicate the end of the mail body,send  <enter>.<enter> . The server should reply  250 OK<enter> . At this point, it is possible simply to close the TCP connection, but it isrecommended to send  QUIT<enter> . The following passage shows the chainof events between client and server when an email is sent from  smith@usc- 134 5.3 SMTP isif.arpa  to  jones@bbn-unix.arpa.  “S” indicates a transmission from server toclient, and “C” indicates a client-to-server transaction. S: 220 Simple Mail Transfer Service C: HELO SERVERS: 250 SERVERC: MAIL FROM:<Smith@USC-ISIF.ARPA>S: 250 OKC: RCPT TO:<Jones@BBN-UNIX.ARPA>S: 250 OKC: DATAC: 354 Start mail input; end with <CRLF>.<CRLF>C: Dear sirC:  Please give me a call to discuss your offerC: .S: 250 OKC: QUITS: 221 CLOSED Example: Complaints department SMTP server If you ever work in the complaints department of a company, this applica-tion will make your life a lot easier. It mimics the communications anSMTP server would make, but it thoughtfully ignores the email content,saving you a lot of stress. Of course, a real application would be to have it log the emails to a data-base, but, for the sake of clarity, that feature is not included in this example.Possible derivations of this project could be an email proxy server, whichcould ﬁlter emails for viruses, and so forth.Start a C# or VB.NET Windows form project as usual, and drag a text-box onto the form. Call it  tbStatus , and set  multiline  to  true .To start with, we must import all of the namespaces we intend to use inthis application. Put this code at the beginning of the program: C# using System.Threading;using System.Net;using System.Net.Sockets;using System.Text; 5.3 SMTP135Chapter 5 VB.NET Imports System.ThreadingImports System.NetImports System.Net.SocketsImports System.Text For simplicity, this server will be single threaded. The thread that listensfor incoming connections runs in the background and starts when the formloads. This means that, although the program won’t hang waiting for con-nections, it can only handle one email at a time.  C# private void Form1_Load(object sender, System.EventArgs e){  Thread thdSMTPServer = new Thread(new  ThreadStart(serverThread));  thdSMTPServer.Start();} VB.NET Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load        Dim thdSMTPServer As Thread        thdSMTPServer = New Thread(New ThreadStart( _          AddressOf serverThread))        thdSMTPServer.Start()End Sub This thread provides the functionality to receive emails sent via SMTP.It listens on port 25 and blocks until an incoming connection is detected.This connection is accepted, and a  250 hello<enter>  reply is sent back tothe client. Note that here it is possible to use  ASCII.GetBytes  becauseSMTP is a text-based protocol, and binary content is not sent at this level.The function  socketReadLine  is not deﬁned yet, but its purpose is tostore incoming data in a string until the termination character(s) is found.Data returned from the client is displayed in  tbStatus , but no otherprocessing takes place.   C# public void serverThread() 136 5.3 SMTP { Byte[] sendBytes; TcpListener tcpListener = new TcpListener(25); tcpListener.Start(); while(true) {  Socket handlerSocket = tcpListener.AcceptSocket();  if (handlerSocket.Connected)  {   // Reply 250 hello   sendBytes = Encoding.ASCII.GetBytes("250 hello\n");   handlerSocket.Send(sendBytes,0,   sendBytes.Length,SocketFlags.None);   // Wait for enter (hello)   tbStatus.Text += socketReadLine(handlerSocket,"\n");   // Reply 250 ok   sendBytes = Encoding.ASCII.GetBytes("250 ok\n");   handlerSocket.Send(sendBytes,0,   sendBytes.Length,SocketFlags.None);   // Wait for enter (mail from)   tbStatus.Text += socketReadLine(handlerSocket,"\n");   // Reply 250 ok   sendBytes = Encoding.ASCII.GetBytes("250 ok\n");   handlerSocket.Send(sendBytes,0,   sendBytes.Length,SocketFlags.None);   // Wait for enter (rcpt to)   tbStatus.Text += socketReadLine(handlerSocket,"\n");   // Reply 250 ok   sendBytes = Encoding.ASCII.GetBytes("250 ok\n");   handlerSocket.Send(sendBytes,0,   sendBytes.Length,SocketFlags.None);   // Wait for enter (data)   tbStatus.Text += socketReadLine(handlerSocket,"\n");   // Reply 354   sendBytes = Encoding.ASCII.GetBytes("354 proceed\n");   handlerSocket.Send(sendBytes,0,   sendBytes.Length,SocketFlags.None);   // Wait for enter.enter (email body)   tbStatus.Text += socketReadLine(handlerSocket,   "\r\n.\r\n");   // Reply 221 close 5.3 SMTP137Chapter 5    sendBytes = Encoding.ASCII.GetBytes("221 close\n");   handlerSocket.Send(sendBytes,0,    sendBytes.Length,SocketFlags.None);   handlerSocket.Close();  }   }      } VB.NET Public Sub serverThread() Dim sendBytes As Byte() Dim tcpListener As New TcpListener(25) Dim handlerSocket As Socket tcpListener.Start() Do  handlerSocket = tcpListener.AcceptSocket()  If handlerSocket.Connected = True Then   ' Reply 250 hello   sendBytes = Encoding.ASCII.GetBytes("250 hello" + vbCrLf)   handlerSocket.Send(sendBytes, 0, sendBytes.Length, _   SocketFlags.None)   ' Wait for enter (hello)   tbStatus.Text += socketReadLine(handlerSocket, vbCrLf)   ' Reply 250 ok   sendBytes = Encoding.ASCII.GetBytes("250 ok" + vbCrLf)   handlerSocket.Send(sendBytes, 0, sendBytes.Length, _   SocketFlags.None)   ' Wait for enter (mail from)   tbStatus.Text += socketReadLine(handlerSocket, vbCrLf)   ' Reply 250 ok   sendBytes = Encoding.ASCII.GetBytes("250 ok" + vbCrLf)   handlerSocket.Send(sendBytes, 0, sendBytes.Length, _   SocketFlags.None)   ' Wait for enter (rcpt to)   tbStatus.Text += socketReadLine(handlerSocket, vbCrLf)   ' Reply 250 ok   sendBytes = Encoding.ASCII.GetBytes("250 ok" + vbCrLf)   handlerSocket.Send(sendBytes, 0, sendBytes.Length, _   SocketFlags.None)   ' Wait for enter (data)   tbStatus.Text += socketReadLine(handlerSocket, vbCrLf) 138 5.3 SMTP    ' Reply 354   sendBytes = Encoding.ASCII.GetBytes("354 proceed" + _   vbCrLf)   handlerSocket.Send(sendBytes, 0, sendBytes.Length, _   SocketFlags.None)   ' Wait for enter.enter (email body)   tbStatus.Text += socketReadLine(handlerSocket, _   vbCrLf + "." + vbCrLf)   ' Reply 221 close   sendBytes = Encoding.ASCII.GetBytes("221 close" + vbCrLf)   handlerSocket.Send(sendBytes, 0, sendBytes.Length, _   SocketFlags.None)   handlerSocket.Close()  End If LoopEnd Sub This thread starts by listening on port 25 for incoming connections. Thethread blocks on the call to  AcceptSocket()  and waits indeﬁnitely until aconnection arrives. Once a connection arrives, it is stored in a socket objectnamed  handlerSocket . Once the connection is established, the serverimmediately responds with  250 hello . The server then waits for the clientto respond. In response to every command sent by the client, the serverresponds with  250 ok . The client is then expected to send a  mail from  com-mand, and the server will wait until the client does so. Once the server hasreplied, it will wait for a  rcpt to  command and ﬁnally a  data  command.The server will read in data from the socket until the end-of-message marker(a period on a line by itself) appears. The server then prompts the client toclose the connection before closing the connection itself. The  socketReadLine  function is called many times from  serverThread .It takes a socket and a terminator string as parameters. Again, it reads infrom the network stream one byte at a time and builds up the  streamData string. If the terminator string appears in the  streamData  string, or if  Read-Byte  fails because of a network error, then the function returns. C# public String socketReadLine(Socket socket,String terminator){  int lastRead=0;        String streamData = "";  NetworkStream networkStream = new NetworkStream(socket); 5.3 SMTP139Chapter 5   do  {    lastRead = networkStream.ReadByte();    if (lastRead==-1) break;     streamData+=(Convert.ToChar(lastRead));    if (streamData.EndsWith(terminator)) break;  }  while(true);  return streamData;} VB.NET Public Function socketReadLine(ByVal socket As Socket, _ ByVal terminator As String) As String Dim lastRead As Int16 Dim streamData As String Dim networkStream As New NetworkStream(socket) Do  lastRead = networkStream.ReadByte()  If lastRead = -1 Then Exit Do  streamData += (Convert.ToChar(lastRead))  If streamData.EndsWith(terminator) Then Exit Do Loop Return streamDataEnd FunctionThe socketReadLine function may look a little verbose, especially becausethe StreamReader already has a ReadLine method; however, this function isdesigned to be generic enough such that it can detect both new-line (\n orvbcrlf) message terminators and end-of-message markers (a period on a lineby itself). This function creates a NetworkStream to the socket and then readsfrom the stream one byte at a time, appending the byte to a string, which isreturned once the message terminator has been found. Before running this example, ensure that no other SMTP server is run-ning at the same time. You can check for the default virtual SMTP server byopening IIS from Administrative Tools and expanding your local computername from within the console. You can stop the SMTP server (if it isinstalled) by right-clicking on its icon and selecting stop. 1405.4Post ofﬁce protocol 3To test this example, run it from Visual Studio .NET. Then open anemail program (e.g., Microsoft Outlook). Press Tools→→→→Accounts (Figure5.1), then click Add→→→→Mail, and click Next twice.Type anything in the POP3 box, and type the IP address of the com-puter on which you are running the SMTP Server, or 127.0.0.1 if youonly have one computer. Keep pressing Next until you arrive back at theprevious screen.Create a new email as usual, and select your new account to send from.On Outlook, this is selected from an arrow to the right of the Send button;on Outlook Express, this is selected from a drop-down list in the “to” ﬁeld.Now press Send.You will see the raw TCP data written as text in the application’s win-dow, as shown in Figure 5.2.5.4Post ofﬁce protocol 3Post ofﬁce protocol 3 (POP3) is used to receive, but not send, emails. EveryISP has a POP3 server, and many Web hosting companies offer access to aFigure 5.1Microsoft Outlook,new account.5.4Post ofﬁce protocol 3141Chapter 5POP3 server to provide personalized email addresses such as joeDoe@exam-ple.com (ﬁctitious). POP3 is described deﬁnitively in RFC 1939, which isdownloadable at www.ietf.org/rfc/rfc1939.txt and operates on TCP port 110.POP3 is used to store emails on behalf of users. Users can then down-load these emails selectively from the server. Some service providers limitthe amount of space devoted to any one user on a POP3 server. Therefore,POP3 also facilitates message deletion.Again, before rushing into implementing POP3, be aware that there arealternatives; for instance, you can use Microsoft Exchange as a POP3 server,and commercial components by IP*Works or SoftArtisans can be used asPOP3 clients.5.4.1Implementing POP3Like SMTP, POP3 is a command-line-based protocol, where each line isterminated with a line-feed (<enter>) character. For variable length lines,the command is terminated by <enter>.<enter> as in SMTP.When the server is operating normally, each line will start with +OK. Ifan error occurs, the line begins with –ERR <some explanation>. Once theclient establishes a TCP connection to the server on port 110, the serverwill always reply with +OK <some greeting message><enter>.Figure 5.2SMTP serverapplication.1425.4Post ofﬁce protocol 3To access a mailbox, the client must authenticate itself with a usernameand password. The client sends USER <username><enter>. The server thenreplies with +OK <welcome><enter>. The password is sent as USER <pass-word><enter> with the same response from the server.To get summary information about the mailbox, the commandSTAT<enter> is issued. To this the server will reply +OK <number of mes-sages> <total size><enter>. Unlike the previous messages, where thetext after the +OK could be ignored, here it must be read and stored forfuture use.To read back an email, the client sends the RETR <number> command;Number must be between 1 and the number received in response to the STATcommand. The server will respond +OK <some message><enter><mailbody><enter>.<enter>. The only piece of important information is themail body; everything else can be ignored.To delete emails, the client sends the DELE <number> command. Theserver will respond +OK <some message><enter>. At this point, it is possi-ble simply to close the TCP connection, but it is recommended to sendQUIT<enter>.To illustrate the protocol more simply, the following text shows thechain of events that occur between a POP3 server and client. As before, “S”indicates a transmission from server to client, and “C” indicates a client-to-server transaction. Here, user Bob is checking his emails, when he receivestwo messages from Alice and Terry.S:    +OK POP3 server ready C:    USER bobS:    +OK user validC:    PASS secretS:    +OK pass validC:    STATS:    +OK 2 170C:    RETR 1S:    +OK 120 octetsS:    hello, how are you bob?, haven’t seen you in S:    ages, any chance you could give me a call S:    sometime? I’d love to see you. Alice S:    .C:    DELE 1S:    +OK message 1 deleted5.4Post ofﬁce protocol 3143Chapter 5C:    RETR 2S:    +OK 50 octetsS:    Hi bob, I got the order of 500 widgets placedS:    with Acme. TerryS:    .C:    DELE 2S:    +OK message 2 deletedC:    QUITS:    +OK This transcript has been simpliﬁed for reasons of clarity. Modern mailmessages contain headers, including the subject, date, natural names of thesender and recipient, and technical information concerning what softwarewas used to send the email and how it was relayed.This is a message header sent from ﬁach_reid@hotmail.com to ﬁach@eir-com.net. Return-Path: <fiach_reid@hotmail.com>Delivered-To: eircom.net-fiach@eircom.netReceived: (vpopmail 31497 invoked by uid 16); 11 Jan 2004 21:51:58 +0000Received: (qmail 31491 messnum 229855 invoked from network[64.4.19.76/law12-f76.law12.hotmail.com]); 11 Jan 2004 21:51:57 -0000Received: from law12-f76.law12.hotmail.com (HELO hotmail.com) (64.4.19.76)  by mail09.svc.cra.dublin.eircom.net (qp 31491) with SMTP; 11 Jan 2004 21:51:57 -0000Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;   Sun, 11 Jan 2004 13:51:56 -0800Received: from 195.92.168.176 by lw12fd.law12.hotmail.msn.com with HTTP;  Sun, 11 Jan 2004 21:51:56 GMTX-Originating-IP: [195.92.168.176]X-Originating-Email: [fiach_reid@hotmail.com]X-Sender: fiach_reid@hotmail.comFrom: "Fiach Reid" <fiach_reid@hotmail.com>To: fiach@eircom.netBcc: Subject: test message1445.4Post ofﬁce protocol 3Date: Sun, 11 Jan 2004 21:51:56 +0000Mime-Version: 1.0Status:  UX-UIDL: 1073857917.31497.mail09.svc.cra.dublin.eircom.net,S=1118Content-Type: text/plain; format=flowedMessage-ID: <Law12-F76F1HkikieqX000054e5@hotmail.com>X-OriginalArrivalTime: 11 Jan 2004 21:51:56.0469 (UTC) FILETIME=[21BF7650:01C3D88D]Two line-feed characters separate the message header from the body.Example:  POP3 client SPAM ﬁlter SPAM is the term used for mass, unsolicited email. These emails are some-times accompanied by attached viruses, which can be accidentally openedby unwitting users. This application could be used to safely delete emailscontaining message fragments indicative of a SPAM email; in this case, thestring “free money.”This simple program scans your mailbox for emails containing the text“free money” and deletes them. This is obviously overly simplistic, but theexample is here for illustration, not practicality.The ﬁrst step is to draw the user interface; you will need three textboxes,labeled tbServer, tbUsername, and tbPassword. Another textbox isrequired, named tbStatus; this textbox should be set with multiline totrue. Finally, place a button on the form, and call it btnClean.First, import the required namespaces:C#using System.Threading;using System.Net;using System.Net.Sockets;using System.Text;using System.IO;VB.NETImports System.ThreadingImports System.NetImports System.Net.SocketsImports System.TextImports System.IO5.4Post ofﬁce protocol 3145Chapter 5Double-click on the Clean button and type the following code:C#private void btnClean_Click(object sender, System.EventArgs e){  TcpClient clientSocket = new TcpClient(tbServer.Text,110);      NetworkStream NetStrm = clientSocket.GetStream();  StreamReader RdStrm= new StreamReader(NetStrm);  tbStatus.Text += RdStrm.ReadLine();         sendPOP3cmd("USER "+ tbUsername.Text + "\r\n",NetStrm);  sendPOP3cmd("PASS "+ tbPassword.Text+ "\r\n",NetStrm);  string Data = sendPOP3cmd("STAT\r\n",NetStrm);              string[] BreakDown = Data.Split(" ".ToCharArray());  int messageCount = Convert.ToInt16(BreakDown[1]);              for (int i=1;i<= messageCount;i++)  {  StringBuilder message = new StringBuilder("");  Data = "RETR " + Convert.ToString(i) + "\r\n";        byte[] szData=  System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());  NetStrm.Write(szData,0,szData.Length);        string szTemp = RdStrm.ReadLine();  while(szTemp!=".")  {    message.Append(szTemp);    tbStatus.Text += szTemp+"\r\n";    szTemp = RdStrm.ReadLine();            }  if (message.ToString().IndexOf("free money")>0)  {    sendPOP3cmd("DELE " + Convert.ToString(i) +     "\r\n",NetStrm);  }  }  clientSocket.Close();  }1465.4Post ofﬁce protocol 3VB.NETPrivate Sub btnClean_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles _ btnClean.Click Dim clientSocket As TcpClient Dim NetStrm As NetworkStream Dim RdStrm As StreamReader Dim Data As String Dim BreakDown() As String Dim messageCount As Int16 Dim message As StringBuilder Dim szData() As Byte Dim i As Int16 Dim szTemp As String clientSocket = New TcpClient(tbServer.Text, 110) NetStrm = clientSocket.GetStream() RdStrm = New StreamReader(NetStrm) tbStatus.Text += RdStrm.ReadLine() sendPOP3cmd("USER " + tbUsername.Text + vbCrLf, NetStrm) sendPOP3cmd("PASS " + tbPassword.Text + vbCrLf, NetStrm) Data = sendPOP3cmd("STAT" + vbCrLf, NetStrm) BreakDown = Data.Split(" ".ToCharArray()) messageCount = Convert.ToInt16(BreakDown(1)) For i = 1 To messageCount  message = New StringBuilder("")  Data = "RETR " + Convert.ToString(i) + vbCrLf  szData = _  System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray())  NetStrm.Write(szData, 0, szData.Length)  szTemp = RdStrm.ReadLine()  Do While szTemp <> "."   message.Append(szTemp)   tbStatus.Text += szTemp + vbCrLf   szTemp = RdStrm.ReadLine()  Loop  If message.ToString().IndexOf("free money") > 0 Then   sendPOP3cmd("DELE " + Convert.ToString(i) + vbCrLf, _   NetStrm)  End If Next i clientSocket.Close()End Sub5.4Post ofﬁce protocol 3147Chapter 5Note that the sendPOP3cmd function is not yet implemented.This piece of code uses a different method from the code for the SMTPserver to read in lines of data from the network. In this case, the ReadLinemethod is used for single-line responses and an iterative loop reads multi-ple-line responses. The chain of events is that the client reads the welcomemessage from the server, then sends the USER and PASS commands. After itissues the STAT command, the server stores the response in Data.  Data is in the format +OK n1 n2, where n1 is the number of messagesand n2 is the total size of the messages. To extract n1 from this string, it issplit into an array of strings, delimited by the space character. The secondelement in this array is now n1.The program then loops through the messages, issuing the RETR com-mand for each one. The contents of the messages returned are built upusing a stringBuilder object, rather than a string, for performance pur-poses. When it reaches a message that has the string “free money” containedwithin it, it issues the DELE command.This code implements the sendPOP3cmd function:C#public string sendPOP3cmd(string cmd,NetworkStream NetStrm){  byte[] szData;  string returnedData = "";  StreamReader RdStrm= new StreamReader(NetStrm);  szData = System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray());  NetStrm.Write(szData,0,szData.Length);  returnedData = RdStrm.ReadLine();  tbStatus.Text += cmd + "\r\n" + returnedData + "\r\n";   return returnedData;}VB.NETPublic Function sendPOP3cmd(ByVal cmd As String, _ByVal NetStrm As NetworkStream) As String  Dim szData() As Byte  Dim returnedData As String  Dim RdStrm As StreamReader  RdStrm = New StreamReader(NetStrm)  szData = _1485.5System.Web.Mail  System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray())  NetStrm.Write(szData, 0, szData.Length)  returnedData = RdStrm.ReadLine()  tbStatus.Text += cmd + vbCrLf + returnedData + vbCrLf  Return returnedDataEnd FunctionIt sends the speciﬁed command to the POP3 server and reads back datauntil it encounters the end-of-line marker \r\n or vbCrLf. The data that isread back is displayed on screen and returned to the calling function.To test this application, run it from Visual Studio .NET. Type yourPOP3 server’s IP address into the ﬁeld provided. You will also need to pro-vide your email account username and password.Using your email program, send an email to yourself with the words“free money” in the subject line. Press Send. Now press Clean out. If youscroll the text to the bottom, you will see the POP3 command DELE, signi-fying that the email was deleted as shown in Figure 5.3.5.5System.Web.MailThere is a built-in mechanism for Windows 2000 and later to send emails.This is called CDOSYS (Microsoft Collaboration Data Objects for Win-Figure 5.3POP3 clientapplication.5.5System.Web.Mail149Chapter 5dows 2000). It is much simpler than implementing SMTP, especially whereattachments and rich-text emails are involved; however, CDOSYS can onlyprovide functionality for the client side of the email service.The following example shows how to send a simple email fromsource@here.com to destination@there.com via the SMTP server smtp.ntl-world.com (change this to your own SMTP server).You must ﬁrst make a reference to System.Web.dll before you canimport the System.Web.Mail namespace. This DLL is a .NET assembly,not .COM. To do so, click Project→→→→Add Reference, and then click on theDLL (Figure 5.4).With that, you can draw your GUI. Drag three textboxes onto the form,name them tbTo, tbFrom, and tbServer. Drag another textbox onto theform, name it tbMessage, and set multiline to true. Finally, place a but-ton on the form, and name it btnSend.C#using System.Web.Mail;Figure 5.4Visual Studio.NET, AddReference.1505.5System.Web.MailVB.NETImports System.Web.MailNow click on the Send button and type in the following code:C#private void btnSend_Click(object sender, System.EventArgs e){        MailMessage email = new MailMessage();        email.From = tbFrom.Text;        email.To = tbTo.Text;        email.Subject = "email from .NET";        email.Body = tbMessage.Text;        SmtpMail.SmtpServer = tbServer.Text;        SmtpMail.Send(email);}VB.NETPrivate Sub btnSend_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSend.Click        Dim email As New MailMessage()        With email            .From = tbFrom.Text            .To = tbTo.Text            .Subject = "email from .NET"            .Body = tbMessage.Text        End With        SmtpMail.SmtpServer = tbServer.Text        SmtpMail.Send(email)End SubThis code simply sets the various properties of a MailMessage object andpasses it to the SmtpMail object. To test the application, run it from VisualStudio .NET. Fill in your own email address in the “To:” ﬁeld, your SMTPserver in the “Server” ﬁeld, and then ﬁll in whatever you wish in the otherﬁelds and press Send. A few moments later, check your email, and youshould have received the message (Figure 5.5). 5.5System.Web.Mail151Chapter 55.5.1AttachmentsTo elaborate on this example, let’s add an attachment box and change theformat to HTML. Drag in the Open File Dialog control, name itofdAttachment, and then add in a textbox, tbAttachment, and a button,btnAttachment.Click on the Browse button and type the following code:C#private void btnBrowse_Click(object sender, System.EventArgs e){        ofdAttachment.ShowDialog();        tbAttachment.Text = ofdAttachment.FileName;}VB.NETSub btnBrowse_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnBrowse.Click        ofdAttachment.ShowDialog()        tbAttachment.Text = ofdAttachment.FileNameEnd SubFigure 5.5SMTP clientapplication.1525.5System.Web.MailClick on the Send button, and modify the code as follows:C#private void btnSend_Click(object sender, System.EventArgs e){        MailMessage email = new MailMessage();        MailAttachment fileAttachment=new        MailAttachment(tbAttachment.Text);        email.Priority = MailPriority.High;        email.BodyFormat = MailFormat.Html;        email.From = tbFrom.Text;        email.To = tbTo.Text;        email.Subject = "email from .NET";        email.Body = tbMessage.Text;        email.Attachments.Add(fileAttachment);        SmtpMail.SmtpServer = tbServer.Text;        SmtpMail.Send(email);}VB.NETPrivate Sub btnSend_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnSend.Click        Dim email As New MailMessage()        Dim fileAttachment As New _        MailAttachment(tbAttachment.Text)        With email            .Priority = MailPriority.High            .BodyFormat = MailFormat.Html            .From = tbFrom.Text            .To = tbTo.Text            .Subject = "email from .NET"            .Body = "<html>" + tbMessage.Text + "</html>"            .Attachments.Add(fileAttachment)        End With        SmtpMail.SmtpServer = tbServer.Text        SmtpMail.Send(email)End Sub5.6Mail application programming interface153Chapter 55.5.2ImagesAnyone who is familiar with HTML will instantly notice a snag here. On aWeb site, if you want to display an image, you use a piece of HTML such as<img src=”picture.jpg”>; however, where can HTML in an email bodylook for images? First, use the following HTML to represent an in-line picture in anemail, <img src=”cid:picture1”>, and then, before calling the sendmethod on the system.web.mail.mailmessage object, call the following:attachInlineFile("c:\picture.jpg", "", "picture1")where c:\picture.jpg is the image you wish to display.5.6Mail application programming interfaceMicrosoft Outlook provides an interface to applications to access emailsstored within its message store. This interface is called the mail applicationprogramming interface (MAPI), and it’s based on legacy COM interfaces,but nevertheless can still be accessed from .NET.The following example lists the subject lines of all the emails in yourOutlook inbox.Start a new project as usual, draw a list view onto the form, and name itlvOutlook. Set the view to Details, and create two column headers labeledFrom and Subject. Click on the Project→→→→Add Reference. Click COM,scroll down the list, and select Microsoft Outlook 10.0 Object Library, andthen click Select. Note: You do not need to have version 10.0 of the Microsoft Outlook ObjectLibrary; this demonstration program will work ﬁne with older versions.Add the following code:C#private void Form1_Load(object sender, System.EventArgs e){        ListViewItem liEmail;        Outlook.Application  App;1545.6Mail application programming interface        Outlook.MailItem Msg;        Outlook.NameSpace NS;        Outlook.MAPIFolder Inbox;        Outlook.Items Items;         int I;        App = new Outlook.Application();        NS= App.GetNamespace("mapi");        Inbox = NS.GetDefaultFolder        (Outlook.OlDefaultFolders.olFolderInbox);        Items = Inbox.Items;        for (I=1;I<Items.Count;I++)        {            Msg = (Outlook.MailItem)Items.Item(I);            liEmail = lvOutlook.Items.Add(Msg.SenderName);            liEmail.SubItems.Add(Msg.Subject);        } }VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load        Dim liEmail As ListViewItem                        Dim App As Outlook.Application         Dim Msg As Outlook.MailItem        Dim NS As Outlook.NameSpace                 Dim Inbox As Outlook.MAPIFolder         Dim Items As Outlook.Items         Dim i As Integer        App = New Outlook.Application()        NS= App.GetNamespace("mapi")        Inbox = NS.GetDefaultFolder _        (Outlook.OlDefaultFolders.olFolderInbox)        Items = Inbox.Items        For i = 1 To Items.Count            Msg = Items.Item(i)            liEmail = lvOutlook.Items.Add(Msg.SenderName)            liEmail.SubItems.Add(Msg.Subject)        Next    End Sub5.6Mail application programming interface155Chapter 5The procedure for receiving emails from outlook via MAPI is relativelystraightforward; however, the MAPI interface is huge and offers anextremely ﬂexible means of leveraging Outlook’s functionality. In the aboveexample, a new instance of Outlook Express is created, and a handle toMAPI is obtained using the GetNamespace() method. The inbox folder isthen picked up and its contents examined by iterating through its Itemscollection. Here, only two pieces of information are extracted from eachemail: the name of the sender and the message subject (Figure 5.6).This application may take a few seconds to start because Microsoft Out-look must start when the Outlook.Application() object is created.It is good programming practice to set these types of objects to nothingor null after use to prevent hidden instances of Outlook hogging systemresources.You will note in the above example that some sender names are fullyqualiﬁed email addresses, whereas some are aliases. To specify emailaddresses only, the following command should be used in preference to theSenderName property:Msg.Recipients(1).AddressFigure 5.6MAPI clientapplication.1565.6Mail application programming interface5.6.1Accessing the address bookMAPI can be used to access most features of Microsoft Outlook, some ofwhich may be useful for developers working on plug-in applications forOutlook.The address book can be accessed via the AddressLists collection in theMAPI namespace (NS in the example above). Each element in the collectioncontains an AddressEntries collection. Each entry in the latter collectioncontains a Name and Address property that can be used to extract emailaddresses and proper names from the Outlook address book. To create an application that reads the Outlook address book, reopenthe example shown above and alter the column headers to read Alias andemail address. Now click on the form and enter the following code:C#private void Form1_Load(object sender, System.EventArgs e){ ListViewItem liEmail; Outlook.Application  App; Outlook.NameSpace NS;       App = new Outlook.Application(); NS= App.GetNamespace("mapi"); int ListsIndexer; int EntriesIndexer; Outlook.AddressList CurrentList; Outlook.AddressEntry CurrentEntry; for(ListsIndexer = 1; ListsIndexer<=NS.AddressLists.Count;ListsIndexer++) {  CurrentList = NS.AddressLists.Item(ListsIndexer);  for(EntriesIndexer=1;      EntriesIndexer<=CurrentList.AddressEntries.Count;      EntriesIndexer++)  {  CurrentEntry =  CurrentList.AddressEntries.Item(EntriesIndexer);  liEmail = lvOutlook.Items.Add(CurrentEntry.Name);  liEmail.SubItems.Add(CurrentEntry.Address);  }5.6Mail application programming interface157Chapter 5 }}VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim liEmail As ListViewItem lvOutlook.View = View.Details        Dim App As Outlook.Application = New Outlook.Application() Dim NS As Outlook.NameSpace = App.GetNamespace("mapi") Dim ListsIndexer As Integer Dim EntriesIndexer As Integer Dim CurrentList As Outlook.AddressList Dim CurrentEntry As Outlook.AddressEntry For ListsIndexer = 1 To NS.AddressLists.Count  CurrentList = NS.AddressLists.Item(ListsIndexer)  For EntriesIndexer = 1 To CurrentList.AddressEntries.Count   CurrentEntry = _ CurrentList.AddressEntries.Item(EntriesIndexer)   liEmail = lvOutlook.Items.Add(CurrentEntry.Name)   liEmail.SubItems.Add(CurrentEntry.Address)  Next NextEnd SubTo test this code, ﬁrst check that there are entries in the Outlook addressbook by pressing Tools→→→→Address Book in Outlook. If there are no entries,add one by pressing the New→→→→New Contact button. Now run the aboveapplication from Visual Studio .NET, and the contact’s name and emailaddress will appear as shown in Figure 5.7.Figure 5.7MAPI address bookapplication.1585.6Mail application programming interface5.6.2IMAPThe Internet message access protocol (IMAP) runs over port 143 and isdescribed deﬁnitively in RFC 1730. Although SMTP and POP3 are the de facto standards for email com-munication on the Internet, they are both very simple protocols, and somecontenders exist for their place on people’s desktops. IMAP is a competingtechnology for POP3. IMAP is much more richly featured than POP3, butfor some reason it is less popular. Messages stored in an IMAP server can be marked as being answered,ﬂagged, deleted, seen, draft, or recent (fetch only). In POP3, a message iseither stored or not deleted. These ﬂags help manage an IMAP account overmultiple clients. If a single POP3 account is accessed by numerous clients,it is difﬁcult to keep track of who has seen or sent what. The protocol itself is line-based, similar to the POP3 protocol. It uses amore complicated, but ﬂexible syntax. Following is an overview of the pro-tocol. It is recommended that you review RFC 1730 for a deﬁnitive guideto IMAP. To access a mailbox, the client must authenticate itself with a usernameand password. The client sends login <username> <password>, to whichthe server replies with OK LOGIN completed, assuming the username andpassword are correct.To get summary information about the mailbox, the command selectinbox is issued.  To this the server replies * <number of messages>EXISTS. To read back an email, the client sends the fetch <number> full com-mand; number must be between 1 and the number received in response tothe select inbox command. The server responds with the message body inRFC 822 format, followed by an end-of-message marker, OK FETCH com-pleted. To delete emails, the client sends the store <number> +flags \deletedcommand. The server responds with OK +FLAGS completed.To illustrate the protocol more simply, the following text shows thechain of events that occurs between an IMAP server and client. As before,“S” indicates a transmission from server to client, and “C” indicates a cli-ent-to-server transaction. Here, user Marc is checking his emails, when hereceives 18 new messages. One of these emails is from Terry Gray, which hedeletes after reading the subject line. 5.6Mail application programming interface159Chapter 5   S:   * OK IMAP4 Service Ready   C:   a001 login marc secret   S:   a001 OK LOGIN completed     C:   a002 select inbox   S:   * 18 EXISTS   S:   * FLAGS (\Answered \Flagged \Deleted \Seen        \Draft)   S:   * 2 RECENT   S:   * OK [UNSEEN 17] Message 17 is the first         unseen message   S:   * OK [UIDVALIDITY 3857529045] UIDs valid   S:   a002 OK [READ-WRITE] SELECT completed     C:   a004 fetch 12 rfc822.header   S:   * 12 FETCH (RFC822.HEADER {346}   S:   Date: Wed, 14 Jul 1993 02:23:25 -0700 (PDT)   S:   From: Terry Gray <gray@cac.washington.edu>   S:   Subject: IMAP4 WG mtg summary and minutes   S:   To: imap@cac.washington.edu   S:   cc: minutes@CNRI.Reston.VA.US, John Klensin         <KLENSIN@INFOODS.MIT.EDU>   S:   Message-Id: <B27397-        0100000@cac.washington.edu>   S:   MIME-Version: 1.0   S:   Content-Type: TEXT/PLAIN; CHARSET=US-ASCII   S:   )   S:   a004 OK FETCH completed      C:   a005 store 12 +flags \deleted   S:   * 12 FETCH (FLAGS (\Seen \Deleted))   S:   a005 OK +FLAGS completed      C:   a006 logout   S:   * BYE IMAP4 server terminating connection   S:   a006 OK LOGOUT completedBecause of its low prevalence in everyday computing, a full implementa-tion of IMAP is not included here.5.6.3Network news transfer protocolThe network news transfer protocol (NNTP) runs over port 119 and isdescribed deﬁnitively in RFC 977. 1605.6Mail application programming interfaceThis protocol is used for efﬁcient management of mailing lists and isgradually becoming obsolete and being replaced by email-based systems. Itis based on the idea that many users can send and receive undirected email,which is sorted into subjects of interest.Two basic tasks can be performed with NNTP: reading postings andcreating new postings. To read posts from a newsgroup, a client connectsto the news server and retrieves a list of newsgroups by using the LISTcommand. To select a group, the client issues the GROUP command fol-lowed by the group name. The server response to this command includesthe number of messages stored for that group. To download one of thesemessages, the client sends the STAT command, followed by the messagenumber. To view the downloaded message, the client can use either theHEAD or BODY command. To better explain the procedure, in this example a client wishes to viewmessage number 10,110 in a group named net.unix-wizards. As before,“S” indicates a transmission from server to client, and “C” indicates a cli-ent-to-server transaction:S:      200 wombatvax news server ready - posting okC:      LISTS:      215 list of newsgroups followsS:      net.wombats 00543 00501 yS:      net.unix-wizards 10125 10011 y        (more information here)S:      net.idiots 00100 00001 nS:      .C:      GROUP net.unix-wizardsS:      211 104 10011 10125 net.unix-wizards group        Selected (there are 104 articles on file,        from 10011 to 10125)C:      STAT 10110S:      223 10110 <23445@sdcsvax.ARPA> article        retrieved - statistics only (article 10110        selected, its message-id is         <23445@sdcsvax.ARPA>)C:      BODYS:      222 10110 <23445@sdcsvax.ARPA> article        retrieved – body follows (body text here)S:      . 5.7Conclusion161Chapter 5The second operation that can be performed through NNTP is postingto newsgroups. Not all newsgroups allow this function, but for those thatdo, this is the procedure. Here the user is posting a message to a servernamed BANZAIVAX:S:      200 BANZAIVAX news server ready, posting         allowed.C:      POSTS:      340 Continue posting; Period on a line by        itself to endC:      (transmits news article in RFC850 format)C:      .S:      240 Article posted successfully.C:      QUITS:      205 BANZAIVAX closing connection.  Goodbye.5.7ConclusionThis chapter has explained how to send and receive emails from your .NETapplication, either from high-level code or socket-level operations. Thischapter outlined the key facets of SMTP and POP3, in summary:(cid:1)SMTP is used to send emails from client to server.(cid:1)POP3 is used to receive emails from server to client.(cid:1)POP3 can be used to delete emails from the server once received.Chapter 12 deals with the issue of determining mail exchange serversfrom domain names. This helps improve the performance of email-drivenapplications. The next chapter deals with the ﬁle transfer protocol (FTP). This is thede facto standard for transferring ﬁles over the Internet and is well worthknowing about.This page intentionally left blank  163 6 FTP: Communicating with File Servers 6.1Background Anybody with experience in Web design knows that in order to put the site“live,” the Web page ﬁles need to be sent to a Web server provided by yourhosting company or ISP. Most people never get to see the physical machinethat their Web site is hosted on, and their only contact with it is through aﬁle transfer protocol, or FTP, program such as cuteFTP or smartFTP.FTP is the most common cross-platform ﬁle transfer mechanismbetween computers over the Internet. FTP software is freely available for allmajor operating systems, including Windows, UNIX, and Mac OS X. Thiscross-platform interoperability is very important for Web site developmentbecause most Web designers work on Windows and most Web servers runfrom UNIX, Linux, and Netware OS.FTP as deﬁned in RFC 1350 supersedes an older protocol known astrivial ﬁle transfer protocol (TFTP). This system is very seldom used on theInternet, but it can be used for procedures such as diskless booting on a net-work. It has no authentication facilities. 6.2Microsoft ﬁle sharing A competing technology developed by Microsoft is the Common InternetFile (CIF) system. This is the native ﬁle-sharing protocol of Windows2000 and XP. It is an extension of the earlier server message block (SMB)protocol used in prior versions of Windows. It is used to provide for thenetwork drive functionality and print sharing. It is more secure than FTP,because of NTLM encryption, and generally faster; however, non-Win-dows implementations are not commonplace, but do exist for VMS and 164 6.3 Netware ﬁle sharing UNIX. The protocol is largely proprietary, which is often a deterrent tonon-Microsoft developers.Windows ﬁle sharing is most commonplace within ofﬁce networks,where many employees share a printer or a central repository for ﬁles. Froma programmer’s perspective, it is an ideal technology to use as a once-offsolution at a company where all of the system users would be on the sameinternal network. If, for instance, an architecture ﬁrm were looking for acentral repository for drawings, network share would be ideal because itrequires no programming. The equivalent system using FTP would beslower, more awkward, and less secure; however, if the same ﬁrm wanted toshare drawings with other ﬁrms, then FTP would be more suitable becauseof its interoperability and ease of deployment on Internet (rather thanintranet) environments.The terms  NETBIOS  and  NETBEUI  are the more correct names forMicrosoft ﬁle and print sharing. A ﬂavor of NETBIOS, NBT runs over IP,but all other forms are not based on IP addresses; they use NETBIOS host-names. These hostnames are resolved into physical addresses in one of fourways. They can broadcast the request on the network (B-Node). Alternately,they may query a WINS server (P-Node). Using a combination of thesemethods, by broadcasting before querying, is M-Node operation, and thereverse is H-Node operation. 6.3Netware ﬁle sharing This is somewhat of a dinosaur of ﬁle-transfer mechanisms, but it regularlyappears in networks that have been in place for decades. It is, however, oneof the fastest ﬁle transfer protocols over internal networks. It is built on topof the Internetworking packet exchange / Sequenced Packet Exchange(IPX/SPX) protocols and is thus nonroutable. Translators are available toconvert these packets to TCP/IP, but the performance factor is lost.The Netware system (also referred to as  IntranetWare ) is centered on acentral Netware server. This server runs the Novell operating system, whichis started from a bootstrap DOS application. The server hosts the Netwaredirectory service (NDS), which is used to control authentication and privi-leges. Older Novell servers (3.x) use a bindery instead of NDS. The differ-ence between the two systems is that the NDS is a relational database andcan replicate among other servers, whereas the bindery cannot.Novell clients are available for almost any platform, from DOS andWindows to Macintosh and UNIX. The clients locate the server by using 6.4 An overview of FTP165Chapter 6 the Novell core protocol (NCP). When a remote ﬁle server is found, it ismapped to a local drive on the client’s machine.There is no native support for interoperating with Netware in .NET, andit is no small undertaking to integrate a .NET application with a Novell net-work. If you have to do so, look at the DOS command-line interfaces to thenetwork, or failing that, try interfacing at the IPX level using raw sockets. 6.4An overview of FTP FTP operates on two ports: 21, the control socket, and a data socket, whichcan exist on port 20 or some other, high port. The deﬁnitive description ofthe protocol is found in RFC 959 at  www.ietf.org/rfc/rfc959.txt.  Like the email protocols, the commands that ﬂow between client andserver are quite human readable and are broken up into lines, like Englishtext; however, it is not feasible to upload or download ﬁles using FTPthrough telnet. If, however, you require a simple batch program to performa routine upload of ﬁles to an FTP server, it may save time to look at theFTP.exe utility.The FTP utility is a DOS-based program with a command-line inter-face. It can, however, accept script ﬁles with the  –s  command-line parame-ter, such that it can run autonomously. To try this utility, create a ﬁle named script.ftp  containing the following text: open www.eej.ulst.ac.ukanonymousme@myemail.comcd libget libtermcap.so.2.0.8quit This FTP script ﬁle will open a connection to a server named www.eej.ulst.ac.uk . Log in anonymously, navigate to the  lib  folder, anddownload a ﬁle named  libtermcap.so.2.0.8 , and then exit. The down-loaded ﬁle will be stored in the current local directory.To run the script as shown in Figure 6.1, go to the command prompt,navigate to the location where  script.ftp  was saved, and then type the fol-lowing keywords: ftp –s:script.ftp 166 6.4 An overview of FTP The technique of using the FTP utility is not the best-practice means oftransferring ﬁles, but it is a simple and straightforward way to perform rou-tine uploads when aesthetics and performance are not important. To lever-age FTP from within a .NET application properly, it is necessary to be well-acquainted with the FTP protocol at a socket level, which is not all that dis-similar to learning to use the FTP utility command-line interface. The FTP protocol facilitates more than uploading and downloading: Itmust also be able to accommodate all manner of ﬁle-manipulation tasks.This includes deleting, renaming, and navigating through folders. You can-not, however, edit the contents of ﬁles using FTP, unless you replace themcompletely.Commands issued from client to server take the form < keyword > < parameter > <enter> Commands from server to client take the form: < status code > < human and/or computer readable message > <enter> Table 6.1 lists the main groups for status codes.When you open an FTP connection to a server using an FTP client, yousometimes will be shown the raw data being sent to and from the server onthe command socket. The text may resemble the following: Figure 6.1 FTP MS-DOSutility. 6.4 An overview of FTP167Chapter 6 220 Serv-U FTP-Server v2.5k for WinSock ready...    USER secret331 User name okay, need password.    PASS (hidden)230 User logged in, proceed.    PWD257 "/" is current directory.    TYPE A200 Type set to A.    PASV227 Entering Passive Mode (212,17,38,3,11,144)    LIST -aL150 Opening ASCII mode data connection for /bin/ls.226 Transfer complete. This is a dump of the trafﬁc on the command port. The data port is notshown. 6.4.1How FTP uses ports In the email protocols, sections of data of variable length (i.e., emails) couldbe sufﬁxed with  <enter>.<enter>  to mark the end of the data. If this char-acter sequence is detected within the body of the email, it could be removedbefore sending without any real degradation of the legibility of the email;however, in FTP, an executable ﬁle could quite easily have this sequence of Table 6.1 FTP status codes. Status code rangeMeaning 1xx Positive preliminary reply. The command has begun on the server. 2xx Positive completion reply. The command has been completed successfully. 3xx Positive intermediate reply. The command has been accepted, but no action has been taken. 4xx Transient negative completion reply. The command has been denied, but can be reissued later. 5xx Permanent negative completion reply. The command has been denied and should not be reissued. 168 6.4 An overview of FTP characters embedded within it, and the removal of those characters couldcause the ﬁle to corrupt.To avoid this problem, port 21 is used to send and receive commandsand responses, each terminated by an  <enter> . When variable length data issent between client and server, such as ﬁles or directory listings, a temporaryconnection is opened on port 20, the data is transferred, and the port isclosed again. In most real-world FTP client implementations, however, theFTP client may be behind a ﬁrewall, so the server should do all the servingand the client should do all the requesting.Passive-mode FTP is where the client instructs the server to listen on aport other than the default data port. The client will then connect to thisport and use it for uploading and downloading as usual.The response to the  PASV  command will always include a bracketed listof six numbers separated by commas. The ﬁrst four digit groups representthe IP address of the server, and the ﬁnal two groups represent the port theserver is listening on for its data connection.In the previous example, the four digits are 212,17,38,3,11,144. Thismeans that the server is located at IP address 212.17.38.3 and listening onport 2960 (11  ×  256 + 144).The server will begin listening on the port as soon as it receives the  PASV command. It will return a 227 message to indicate that it has begun listen-ing on this port. Once the client connects to this port, the server will returna 150 message. If the client does not connect to the port in a timely fashion(a few seconds), the server will issue a 425 timeout message. The server willsend the requested data on that port and close the connection once all ofthe data is sent, and then issue a 226 message.The same process happens in reverse when uploading to the server. Inthis case, the  PASV  command is issued, and the client connects to the portspeciﬁed by the server. The client then places the contents of the ﬁle on thenew socket and closes the connection once the ﬁle is sent.  6.4.2The FTP handshake In the same way, FTP uses a basic authentication mechanism: It accepts ausername and password in plain text, which can be seen by anyone usinga protocol analyzer at any point between the client and the server. FTPover SSL is recommended when a Web site carries information of sub-stantial value.  6.4 An overview of FTP169Chapter 6 An FTP server may allow anonymous access. This is where the usernameis set to  anonymous  and the password can be anything. This is the defaultsetup of the Microsoft FTP service. When you connect to an FTP server on port 21, the server will respondas follows: 220 < some message ><enter> Using the same format as the POP3 handshake, the next commands tosend are  USER  and  PASS  (in that order). The  USER  command is of this for-mat: USER < username ><enter> The server will generally respond with 331 and request a password,whether there is any record of that user on the system or not. This is tomake brute-force attacks more difﬁcult. 331 < some message ><enter> The  PASS  command must then be sent: PASS < password ><enter> The server will either respond with a 530 message for a failed login or230 for a successful login.  230 < some message ><enter> At this point, the user should have access to the FTP server. Dependingon the privileges set on the FTP server, the user will be able to read or writeoperations within a limited section of the remote computer’s disk drives. Some FTP servers will disconnect inactive users to save resources. There-fore, a periodic  NOOP  command will keep the FTP server from closing theconnection. A  NOOP  command has no effect on the server beyond this task. NOOP<enter> 170 6.4 An overview of FTP 200 <message><enter> To close the connection, the client may simply close the underlyingTCP connection, or issue a QUIT command as follows: QUIT<enter>221 <message><enter> 6.4.3Navigating folders In order to navigate around a remote computer’s ﬁle system, you need toknow what ﬁles and subfolders are contained within each folder. Like ﬁles, this data is returned on the data socket. The process for receiv-ing folder listings is as follows: (cid:1) Client issues  LIST  command. (cid:1) Server waits for data socket to be created. A timeout will occur with a425 response. Otherwise, a 125 response is received. (cid:1) Server transfers ﬁle data, as illustrated below. (cid:1) Server closes data connection and issues a 226 response on the con-trol socket.On the Windows FTP service, the default directory listing style is DOS.A listing would resemble the following: 01-18-03  03:22PM                    0 ge.bmp01-18-03  11:40PM                  733 Project1.vbp01-18-03  05:00PM                 2498 Readme.txt01-18-03  03:40PM       <DIR>          watThe ﬁve columns are last modiﬁed date, time, folder or ﬁle, size, andname, respectively.For UNIX FTP servers, the directory listing style is in this format:d---rw-rw-   1 user   group        0 Jan 18  2003 .d---rw-rw-   1 user   group        0 Jan 18  2003 ..----rw-rw-   1 user   group        0 Jan 18 15:22 ge.bmp6.4An overview of FTP171Chapter 6----rw-rw-   1 user   group      733 Jan 18 23:40 Project1.vbp----rw-rw-   1 user   group     2498 Jan 18 17:00 Readme.txtd---rw-rw-   1 user   group        0 Jan 18  2003 watNote: The Cerberus FTP server for Windows (www.cerberusftp.com) willalso return ﬁle data in a UNIX format. The directory listing style is inter-changeable in IIS. This is an unfortunate lack of standardization, but something that devel-opers must be aware of. A quick-and-dirty approach is to read the last wordat the end of each line and assume it to be a ﬁle if there is a period in it.A more foolproof implementation is to issue a SYST command to theserver and read the response, either 215 UNIX<version><enter> or 215Windows<version><enter>. Alternately, the NLST command may be used toreceive a list of ﬁles (only) from the server.The folder system in FTP is navigated in much the same way as inDOS. To move to a subfolder, the client issues CWD /<foldername><enter>, to which the server replies 250 for success or 550 for failure.To move to the parent folder, the client issues CDUP. To retrieve the current folder, the client may issue PWD, to which theserver replies:257 "<folder name>"<message><enter>6.4.4FTP command referenceFollowing is a comprehensive list of FTP commands as would be issued bya client.Table 6.2FTP commands .FTP CommandActionRETRDownloadsSTORUploadsSTOUUploads, where the server chooses the name of the remote ﬁle; this name is speciﬁed in the 250 response1726.4An overview of FTP6.4.5Implementing FTPTo access an FTP server, you need to know its IP address and have a user-name and password with it. Most ISPs provide you with a small amount ofWeb space on their servers when you sign up, and you should be able to getthese details if you call your ISP. APPE       AppendsREST      Restarts ﬁle transfer at a speciﬁed positionRNFR      Renames a ﬁle (RNFR <old name>); must be followed by RNTORNTO      Renames a ﬁle (RNTO <new name>); must be preceded by RNFRABORAborts the current data transferDELE      Deletes the speciﬁed ﬁleRMD       Deletes the speciﬁed folderMKD       Creates a new folderPWD       Responds with the current working directoryLIST      Responds with the contents of the current working directory in human-readable formatNLST      Responds with a list of ﬁles in the current working directorySITE      Provides proprietary FTP servicesSYST      Responds with the name of the operating system (or the OS being emulated)STAT      Responds with the status of a data transferHELP      Responds with human-readable text with information about the serverNOOP      No effectUSERSpeciﬁes the username of the userPASSSpeciﬁes the password of the userTYPEIndicates the format of the data, either A for ASCII, E for EBCDIC, I for Binary, or L n to select a custom byte size (where n is the length of the byte)Table 6.2FTP commands (continued).FTP CommandAction6.4An overview of FTP173Chapter 6Some versions of Windows come with an option to install an FTPserver. Click Control Panel→→→→Add/Remove Programs→→→→Add or Remove Win-dows Components→→→→Internet Information Services→→→→Details→→→→FTP Service(Figure 6.2). To administer the FTP server once it is installed, click ControlPanel→→→→Administrative Tools→→→→Internet Information Services→→→→FTPSite→→→→Default FTP site. Then right-click and go to Properties. Click on the Home Directory tab (Figure 6.3). This is where you can setthe FTP site directory path, which is where uploaded FTP ﬁles are storedon your local hard disk. For the purposes of the code examples in this chap-ter, you should check both the read and write options.To test out your FTP server, type ftp://localhost into Internet Explorer.You can download various FTP clients from the Internet (e.g., smartFTP,www.smartftp.com, or cuteFTP, www.globalscape.com).Figure 6.2Windows: Add orremove componentsfor IIS.1746.4An overview of FTP6.4.6Implementing FTP with the Internet Transfer ControlA full implementation of FTP is quite an undertaking. It may be worth-while to consider the Microsoft Internet Transfer Control if you need toperform this task. It is a legacy COM control (and thus carries a lot of over-head for .NET applications). Native .NET components are available com-mercially 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 writinga feature to an application to perform a scheduled upload of ﬁles to a server,you probably don’t want to confuse the user with details of the remote com-puter’s directory structure. All you may need is a few lines of code to trans-fer the ﬁle to a predetermined location.Create a new Windows application project in Visual Studio .NET asusual, and draw two textboxes, one named tbServer and the other tbFile.Figure 6.3FTP siteadministration.6.4An overview of FTP175Chapter 6Add two buttons, btnBrowse and btnUpload. You will also require an OpenFile Dialog control named openFileDialog.Click on the Browse button, and add the following code:C#private void btnBrowse_Click(object sender, System.EventArgs e){  openfileDialog.ShowDialog();  tbFile.Text = openfileDialog.FileName;}VB.NETPrivate  Sub btnBrowse_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnBrowse.Click  openfileDialog.ShowDialog()  tbFile.Text = openfileDialog.FileNameEnd SubNow, double-click on the Upload button, and add this code:C#private void btnUpload_Click(object sender, System.EventArgs e){  FileInfo thisFile = new FileInfo(tbFile.Text);  Type ITC;  object[] parameter= new object[2];        object ITCObject;  ITC = Type.GetTypeFromProgID("InetCtls.Inet");  ITCObject = Activator.CreateInstance(ITC);     parameter[0] = (string)tbServer.Text;   parameter[1] = (string)"PUT " + thisFile.FullName + " /" +                    thisFile.Name;   ITC.InvokeMember("execute", BindingFlags.InvokeMethod,                      null, ITCObject, parameter);}1766.4An overview of FTPVB.NETPrivate Sub btnUpload_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnUpload.Click  Dim thisFile As FileInfo = New FileInfo(tbFile.Text)    Dim ITC As Type    Dim parameter() As Object = New Object(1) {}    Dim ITCObject As Object    ITC = Type.GetTypeFromProgID("InetCtls.Inet")    ITCObject = Activator.CreateInstance(ITC)    parameter(0) = CType(tbServer.Text, String)    parameter(1) = CType("PUT " + thisFile.FullName + _       " /" + thisFile.Name, String)    ITC.InvokeMember("execute", BindingFlags.InvokeMethod, _       Nothing, ITCObject, parameter)End SubAs mentioned earlier, the Internet Transfer Control (ITC) is a legacyCOM control rather than a native .NET control. In Chapter 1, the Inter-net Explorer component (which was also COM) was used as part of anapplication to form a custom Web browser. This time, instead of includingthe COM control in the project by right-clicking on the toolbox and add-ing it there, we call the COM control directly through code. This is slightly more complex, but offers the advantage of late binding(i.e., the object is loaded at run time rather than compile time). This givesthe beneﬁt of fault tolerance; in case the external COM control is acciden-tally deleted, the host application will still operate, albeit with degradedfunctionality. Late binding does incur a performance penalty because thecode will need to determine the object’s supported methods and types atrun time by interrogating the object’s IDispatch interface. The environ-ment would already know the object’s interface if it had been early bound.Late binding is not strictly required for use with the ITC, but it is useful tolearn new techniques. Every COM control has a unique programmatic ID, or ProgID. Thisstring is stored in the registry and is in the format <project name>.<Classname>.<version>. In this instance, the programmatic ID isInetCtls.Inet, with no version number. The Activator creates an instance of the class at run time. At designtime, there is no way of knowing the methods and properties of the object;6.4An overview of FTP177Chapter 6therefore, the return value is of the generic object type. In order to callmethods on an object that has unknown type (at design time at least), weuse the InvokeMember method. In order to invoke the execute method on the object and pass twoparameters to it, we need ﬁrst to deﬁne the two parameters. The ﬁrst is theFTP address to which the object will connect. The second is the FTP com-mand that the object will execute on the server. These two parameters arecast to strings and stored in an array. Finally, the InvokeMember method iscalled, passing the method name as the ﬁrst parameter and the parametersto be sent to the COM control as the last parameter.You will also need the relevant namespaces:C#using System.IO;using System.Reflection;using System.Threading;VB.NETImports System.IOImports System.ReflectionImports System.ThreadingTo test this piece of code, ﬁrst ensure that your FTP server is running.After you have checked this, run the application from Visual Studio .NET.Type the IP address, username, and password into the Server textbox in thestandard URL format (i.e., ftp://username:password@myserver.com). Choosea ﬁle from your hard disk by pressing the Browse button, then press Upload(Figure 6.4). Now check your FTP root to ensure that the ﬁle is there. Figure 6.4FTP client usingCOM.1786.4An overview of FTPThere is an important limitation in the ITC; that is that the ﬁle must bein the old DOS 8.3 format, where C:\program files\myile.txt becomesc:\progra~1\myfile.txt. Please note that you must have write access tothe root of the FTP server; otherwise, the code example will not work. The second parameter passed to the execute method of the ITC deter-mines the action to be performed on the remote FTP server. Table 6.3 liststhe possible actions.6.4.7A more substantial implementation of FTPThe ITC has several limitations, contains quite a few well-known bugs, andis far from being a high-performance implementation. Furthermore, it isnot native to .NET, and many software houses will demand that a .NETproject is 100% managed code.Table 6.3FTP command usage.FTP commandActionDIR /anyFolderRetrieves the directory listing tree from the speciﬁed folder at the remote machine; list-ing can be retrieved using the GetChunk methodCD anyFolderMoves to the speciﬁed folder on the remote machineCDUPMoves to the parent folder (if one exists) on the remote machineGET anyFolder/anyFile.txt c:\anyFile.txtDownloads a remote ﬁle to a local ﬁle PUT c:\anyFile.txt anyFolder/anyFile.txtUploads a local ﬁle to a remote ﬁleMKDIR /anyFolderNameCreates a directory on the remote machineRMDIR anyFolderNameRemoves a directory from the remote machineRENAME oldFileName.txt newFileName.txtChanges the name of a ﬁle on the remote machineSIZE /anyFile.txtRetrieves the size of a speciﬁed ﬁle QUITCloses the connection to the FTP server6.4An overview of FTP179Chapter 6By following the code on the next few pages, you will have a full-ﬂedgedFTP client, with the ability to browse a remote ﬁle system, upload, anddownload.Start a new project in Visual Studio .NET and add two forms, frmLogonand frmMain. On the Logon form, draw four textboxes: tbServer, tbUser-name, tbPassword, and tbStatus. The latter should be set with multi-line=true and greyed out appropriately. A button, btnLogon, should alsobe added.On the Main form, draw two list boxes: lbFiles and lbFolders. Add atextbox named tbStatus in the same style as in the Logon form. Add threebuttons: btnUpload, btnDownload, and btnRoot. Also add an File Open Dia-log control  named OpenFileDialog and a Save File Dialog control namedSaveFileDialog.In the Main form, add a few public variables:C#public frmLogon LogonForm = new frmLogon();  public NetworkStream NetStrm;    public string RemotePath = "";    public string server = "";VB.NETPublic LogonForm As frmLogon = New frmLogon() Public NetStrm As NetworkStreamPublic RemotePath As String = "" Public server As String = ""In the Logon form, add the following public variable:C#public frmMain MainForm;VB.NETPublic MainForm as frmMainThe call to new frmLogon() does not make the Logon form visible;instead, it is used to help ensure that only one instance of the Logon formoccurs. The NetworkStream variable is used to represent the commandsocket connection. The data connection is less permanent and does not1806.4An overview of FTPneed to be deﬁned globally. The two strings maintain information aboutwhere the server is and what the working folder is on the server. Double-click on the Main form and add these lines:C#private void frmMain_Load(object sender, System.EventArgs e){  LogonForm.MainForm = this;  LogonForm.Visible = true;}VB.NETPrivate  Sub frmMain_Load(ByVal sender As Object, _ByVal e As System.EventArgs)  LogonForm.MainForm = Me  LogonForm.Visible = TrueEnd SubThis shows the Logon form and provides a means for the Logon form tocall public methods on this particular instance of the Main form. On the Logon from, comment out the Dispose method to ensure that itcannot be deleted from memory unless the Main form is closed. You mayneed to expand the “Windows Form Designer generated code” region toview this method.C#protected override void Dispose( bool disposing ){ }VB.NETProtected Overrides  Sub Dispose(ByVal disposing As Boolean)End SubOn the logon form, click on the Logon button, and enter the followingcode:C#private void btnLogon_Click(object sender, System.EventArgs e)6.4An overview of FTP181Chapter 6{        TcpClient clientSocket = new TcpClient(tbServer.Text,21);      MainForm.NetStrm = clientSocket.GetStream();  StreamReader RdStrm= new StreamReader(MainForm.NetStrm);  tbStatus.Text = RdStrm.ReadLine();       tbStatus.Text = MainForm.sendFTPcmd("USER "+  tbUsername.Text + "\r\n");  tbStatus.Text = MainForm.sendFTPcmd("PASS "+  tbPassword.Text+ "\r\n");        if (tbStatus.Text.Substring(0,3)!="230")  {    MessageBox.Show ("Failed to log in");  }  else  {    MainForm.server = tbServer.Text;    MainForm.getRemoteFolders();    MainForm.Text += "[logged in]";    Visible=false;  }}VB.NETPrivate  Sub btnLogon_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  Handles btnLogon.Click      Dim clientSocket As TcpClient =  New _  TcpClient(tbServer.Text,21)   MainForm.NetStrm = clientSocket.GetStream()  Dim RdStrm As StreamReader =  New _  StreamReader(MainForm.NetStrm)   tbStatus.Text = RdStrm.ReadLine()    tbStatus.Text = MainForm.sendFTPcmd("USER "+ _  tbUsername.Text + vbcrlf)  tbStatus.Text = MainForm.sendFTPcmd("PASS "+ _  tbPassword.Text+ vbcrlf)  If tbStatus.Text.Substring(0,3)<>"230" Then    MessageBox.Show ("Failed to log in")1826.4An overview of FTP  Else     MainForm.server = tbServer.Text    MainForm.getRemoteFolders()    MainForm.Text += "[logged in]"    Visible=False  End IfEnd SubThis code opens a TCP connection to the FTP server on port 21. Oncethe connection has been made, a stream to the remote host is established.This stream is then extended through a StreamReader. The welcome mes-sage from the server is read in and displayed on-screen. The program then attempts to log in using the username and passwordsupplied. If the FTP server responds with a 230 message, then a messagebox is displayed. Otherwise, the getRemoteFolders() method is called onthe main form, a “Logged in” message appears in the form caption, and thelogon form disappears. The function sendFTPcmd() has not yet been implemented, so insertthis code into the Main form:C#public string sendFTPcmd(string cmd){  byte[] szData;  string returnedData = "";        StreamReader RdStrm= new StreamReader(NetStrm);  szData = Encoding.ASCII.GetBytes(cmd.ToCharArray());  NetStrm.Write(szData,0,szData.Length);  tbStatus.Text += "\r\nSent:" + cmd;  returnedData = RdStrm.ReadLine();  tbStatus.Text += "\r\nRcvd:" + returnedData;        return returnedData;}VB.NETPublic Function sendFTPcmd(ByVal cmd As String) As String  Dim szData() As Byte  Dim ReturnedData As String =  ""   Dim RdStrm As StreamReader =  New StreamReader(NetStrm)   szData = Encoding.ASCII.GetBytes(cmd.ToCharArray())6.4An overview of FTP183Chapter 6  NetStrm.Write(szData,0,szData.Length)  tbStatus.Text += vbcrlf +"Sent:" + cmd  ReturnedData = RdStrm.ReadLine()  tbStatus.Text += vbcrlf +"Rcvd:" + ReturnedData        Return ReturnedDataEnd FunctionThis code sends a string on the command socket via the publicNetworkStream NetStrm. This stream is passed to the constructor of aStreamReader to facilitate easier reading of the stream. The string passedinto the function is converted to a character array and sent over the wireusing the Write method. The outgoing command is printed on screen.The StreamReader reads incoming data up to the end-of-line characterand then displays this on-screen. Finally, the data received is returned tothe calling function.As seen earlier, when the Logon button is pressed, a call is made togetRemoteFolders(). We can now implement this in the Main form:C#public void getRemoteFolders(){  string[] filesAndFolders;        string fileOrFolder;  string folderList="";      int lastSpace=0;    folderList =  Encoding.ASCII.GetString(sendPassiveFTPcmd("LIST\r\n"));  lbFiles.Items.Clear();  lbFolders.Items.Clear();  filesAndFolders = folderList.Split("\n".ToCharArray());  for(int i=0;i<filesAndFolders.GetUpperBound(0);i++)  {    if (filesAndFolders[i].StartsWith("-") ||     filesAndFolders[i].StartsWith("d"))    {      lastSpace=59; // UNIX format    }    else    {      lastSpace=39; // DOS format                }        1846.4An overview of FTP    fileOrFolder =  filesAndFolders[i].Substring(lastSpace);    if (fileOrFolder.IndexOf(".")!=-1)    {      lbFiles.Items.Add(fileOrFolder.Trim());    }    else    {      lbFolders.Items.Add(fileOrFolder.Trim());    }  }}VB.NETPublic  Sub getRemoteFolders()  Dim filesAndFolders() As String  Dim fileOrFolder As String  Dim folderList As String = ""  Dim lastSpace As Integer = 0  folderList = Encoding.ASCII.GetString(sendPassiveFTPcmd _    ("LIST" + vbCrLf))  lbFiles.Items.Clear()  lbFolders.Items.Clear()  filesAndFolders = folderList.Split(vbCr.ToCharArray())  Dim i As Integer  For i = 0 To filesAndFolders.GetUpperBound(0) - 1    If filesAndFolders(i).StartsWith("-") Or _    filesAndFolders(i).StartsWith("d") Then      lastSpace = 59 ' UNIX format    Else      lastSpace = 39 ' DOS format                End If    fileOrFolder = filesAndFolders(i).Substring(lastSpace)    If fileOrFolder.IndexOf(".") <> -1 Then      lbFiles.Items.Add(fileOrFolder.Trim())    Else      lbFolders.Items.Add(fileOrFolder.Trim())    End If  NextEnd Sub6.4An overview of FTP185Chapter 6This uses the quick-and-dirty method of pulling ﬁle and folder detailsout of the ﬁle data received from the FTP server. It issues the LIST FTPcommand via a passive connection. The passive connection is requiredbecause the data returned could potentially be quite large and containsmany lines of information.The data returned is then split into lines by delimiting the string by theend-of-line character and applying the Split method. Going through theselines one by one, everything is stripped off that precedes the ﬁnal space. Ifthe remaining string contains a period, then it is added to the ﬁle list; if not,it is added to the folder list.In order to receive data from a passive connection, we need to imple-ment the sendPassiveFTPcmd() function:C#public byte[] sendPassiveFTPcmd(string cmd){  byte[] szData;      System.Collections.ArrayList al = new ArrayList();  byte[] RecvBytes = new byte[Byte.MaxValue];      Int32 bytes;            Int32 totalLength=0;  szData =  System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray());  NetworkStream passiveConnection;          passiveConnection = createPassiveConnection();        tbStatus.Text += "\r\nSent:" + cmd;  StreamReader commandStream= new StreamReader(NetStrm);    NetStrm.Write(szData,0,szData.Length);  while(true)        {    bytes = passiveConnection.Read(RecvBytes,    0,RecvBytes.Length);    if (bytes<=0) break;    totalLength += bytes;    al.AddRange(RecvBytes);          }  al = al.GetRange(0,totalLength);              tbStatus.Text+="\r\nRcvd:"+commandStream.ReadLine(); // 125  tbStatus.Text+="\r\nRcvd:"+commandStream.ReadLine(); // 226  return (byte[])al.ToArray((new byte()).GetType());}1866.4An overview of FTPVB.NETPublic Function sendPassiveFTPcmd(ByVal cmd As String) _ As Byte()  Dim szData() As Byte  Dim al As New System.Collections.ArrayList  Dim bytes As Int32  Dim RecvBytes(Byte.MaxValue) As Byte  szData = _  System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray())  Dim totalLength As Int32 = 0  Dim passiveConnection As NetworkStream  passiveConnection = createPassiveConnection()  tbStatus.Text += vbCrLf + "Sent:" + cmd  Dim commandStream As StreamReader = New _  StreamReader(NetStrm)  NetStrm.Write(szData, 0, szData.Length)  Do While (True)    bytes = passiveConnection.Read(RecvBytes, 0, _    RecvBytes.Length)    If bytes <= 0 Then Exit Do    totalLength += bytes    al.AddRange(RecvBytes)  Loop  al = al.GetRange(0, totalLength)  tbStatus.Text += vbCrLf + "Rcvd:" + _  commandStream.ReadLine() ' 125  tbStatus.Text += vbCrLf + "Rcvd:" + _   commandStream.ReadLine() ' 226    Return CType(al.ToArray((New Byte).GetType()), Byte())End FunctionThis code requests a passive connection to the server via the createPas-siveConnection() function. It then sends the string on the commandsocket, and then reads everything sent on the passive connection until theserver closes it. This data is then returned to the calling function. Any datasent back on the command socket is more or less ignored, apart from someon-screen reporting.The next step is to implement the createPassiveConnection() function:6.4An overview of FTP187Chapter 6C#private NetworkStream createPassiveConnection(){  string[] commaSeperatedValues;        int highByte =0;  int lowByte =0;        int passivePort =0;        string response="";    response = sendFTPcmd("PASV\r\n");        // 227 Entering Passive Mode (127,0,0,1,4,147).  commaSeperatedValues = response.Split(",".ToCharArray());  highByte = Convert.ToInt16(commaSeperatedValues[4]) * 256;  commaSeperatedValues[5] =  commaSeperatedValues[5].Substring(0,  commaSeperatedValues[5].IndexOf(")"));  lowByte = Convert.ToInt16(commaSeperatedValues[5]);  passivePort = lowByte + highByte;        TcpClient clientSocket = new TcpClient(server,passivePort);  NetworkStream pasvStrm = clientSocket.GetStream();        return pasvStrm;}VB.NETPrivate Function createPassiveConnection() As NetworkStream  Dim commaSeperatedValues() As String  Dim highByte As Integer = 0   Dim lowByte As Integer = 0   Dim passivePort As Integer = 0   Dim response As String = ""   response = sendFTPcmd("PASV"+vbCrLf)        ' 227 Entering Passive Mode (127,0,0,1,4,147).  commaSeperatedValues = response.Split(",".ToCharArray())  highByte = Convert.ToInt16(commaSeperatedValues(4)) * 256  commaSeperatedValues(5) = _  commaSeperatedValues(5).Substring(0, _  commaSeperatedValues(5).IndexOf(")"))  lowByte = Convert.ToInt16(commaSeperatedValues(5))  passivePort = lowByte + highByte        Dim clientSocket As TcpClient= New _  TcpClient(server,passivePort)  Dim pasvStrm As NetworkStream=clientSocket.GetStream() 1886.4An overview of FTP  Return pasvStrmEnd FunctionThis function issues a PASV command on the command socket to theserver. The received data should resemble the following:227 Entering Passive Mode (127,0,0,1,4,147)The ﬁnal two numbers indicate the port number of the socket that theserver has just begun to listen on. In this case, it is 1171 (4 × 256 + 147). Toextract these numbers, the string is ﬁrst split into smaller strings, which aredivided up using the comma character in the Split method. The low-order byte of the new port number is followed by a closedbracket. This bracket must be stripped off before it will convert to an inte-ger. To do this, the IndexOf method locates the superﬂuous bracket, andSubstring removes everything following the ﬁnal digit. Once the passive port has been determined, the function then opens aTCP connection to the server on this port. The resultant NertworkStream issent back to the calling function.At this point, you can compile and run the code from Visual Studio.NET and check that you can log onto any FTP server and view the rootdirectory listing. The next step is to add the folder-browsing capabilities.We can implement the event that is ﬁred when a user clicks on a folder inthe folder list. Click on the folder list, and type this code:C#private void lbFolders_SelectedIndexChanged(object sender, System.EventArgs e){  RemotePath += "/" + lbFolders.SelectedItem.ToString();  sendFTPcmd("CWD /" + RemotePath +"\r\n");  getRemoteFolders();}VB.NETPrivate  Sub lbFolders_SelectedIndexChanged(ByVal _sender As Object, ByVal e As System.EventArgs) Handles _ lbFolders.SelectedIndexChanged  RemotePath += "/" + lbFolders.SelectedItem.ToString()6.4An overview of FTP189Chapter 6  sendFTPcmd("CWD /" + RemotePath + vbcrlf)  getRemoteFolders()End SubThe purpose of building up the RemotePath variable is that when thenext list of folders is shown, the application must remember that each list-ing corresponds to /folder/subfolder rather than /subfolder. The folderand ﬁle lists are refreshed once the operation is complete. We could, ofcourse, have used the PWD command to get the path from the server, but it isprobably easier and quicker to store this information locally. To instruct theFTP server to move to a working directory, the CWD command is used. This gives a user the means to drill down directories, but no means ofreturning back up. In this case, we can double-click on btnRoot:C#private void btnRoot_Click(object sender, System.EventArgs e){  RemotePath = "/";  sendFTPcmd("CWD /\r\n");  getRemoteFolders();}VB.NETPrivate  Sub btnRoot_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  RemotePath = "/"  sendFTPcmd("CWD /" + vbcrlf)  getRemoteFolders()End SubThis resets the working folder to the root and sends a command to theFTP server to return to the FTP root. It then refreshes the ﬁle and folderlists. Again, the CWD command is issued to instruct the FTP server to moveworking folders.Now, to implement the core purpose of this application, double-clickthe Upload button:C#private void btnUpload_Click(object sender, System.EventArgs e)1906.4An overview of FTP{  openFileDialog.ShowDialog();        NetworkStream passiveConnection;  FileInfo fileParse = new FileInfo(openFileDialog.FileName);  FileStream fs = new  FileStream(openFileDialog.FileName,FileMode.Open);  byte[] fileData = new byte[fs.Length];  fs.Read(fileData,0,(int)fs.Length);             passiveConnection = createPassiveConnection();  string cmd = "STOR " + fileParse.Name + "\r\n";  tbStatus.Text += "\r\nSent:" + cmd;   string response = sendFTPcmd(cmd);        tbStatus.Text += "\r\nRcvd:" + response;    passiveConnection.Write(fileData,0,(int)fs.Length);        passiveConnection.Close();        MessageBox.Show("Uploaded");  tbStatus.Text += "\r\nRcvd:" + new  StreamReader(NetStrm).ReadLine();  getRemoteFolders();      }VB.NETPrivate  Sub btnUpload_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  openFileDialog.ShowDialog()  Dim passiveConnection As NetworkStream  Dim fileParse As FileInfo = New _  FileInfo(openFileDialog.FileName)  Dim fs As New FileStream(openFileDialog.FileName, _  FileMode.Open)  Dim fileData(fs.Length) As Byte  fs.Read(fileData, 0, fs.Length)  passiveConnection = createPassiveConnection()  Dim cmd As String = "STOR " + fileParse.Name + vbCrLf  tbStatus.Text += vbCrLf + "Sent:" + cmd  Dim response As String = sendFTPcmd(cmd)  tbStatus.Text += vbCrLf + "Rcvd:" + response  passiveConnection.Write(fileData, 0, fs.Length)  passiveConnection.Close()  MessageBox.Show("Uploaded")  tbStatus.Text += vbCrLf + "Rcvd:" + New _  StreamReader(NetStrm).ReadLine()  getRemoteFolders()End Sub6.4An overview of FTP191Chapter 6This function opens the standard File Open dialog and then reads thecontents of the speciﬁed ﬁle into a string by passing the ﬁlename to aStreamReader, and reading to the end of the ﬁle while storing the data inthe fileData string. A passive connection is then opened to the server, and a STOR commandis issued on the command socket. Once the server has responded to theSTOR command, the ﬁle contents are placed on the passive connection.Once all of the data is sent, the connection is closed. A message is displayedon screen, and the ﬁle and folder list is refreshed.Note that you do not have to pass the local path of the ﬁle to the FTPserver because the ﬁle will be stored in the current working folder. The ﬁle-name, minus its path, is obtained from the FileName property of theFileInfo class. Finally, the download functionality can be implemented by clicking onthe Download button:C#private void btnDownload_Click(object sender, System.EventArgs e){  byte[] fileData;  saveFileDialog.ShowDialog();        fileData = sendPassiveFTPcmd(         "RETR " + lbFiles.SelectedItem.ToString()+ "\r\n");  FileStream fs = new FileStream(            saveFileDialog.FileName,FileMode.CreateNew);  fs.Write(fileData,0,fileData.Length);  fs.Close();  MessageBox.Show("Downloaded");}VB.NETPrivate  Sub btnDownload_Click(ByVal sender As Object, _ ByVal e As System.EventArgs)  Dim fileData As Byte()  saveFileDialog.ShowDialog()  fileData = sendPassiveFTPcmd( _   "RETR " + lbFiles.SelectedItem.ToString() + vbCrLf)  Dim fs As FileStream = New FileStream( _    saveFileDialog.FileName, FileMode.CreateNew)1926.4An overview of FTP  fs.Write(fileData, 0, fileData.Length)  fs.Close()  MessageBox.Show("Downloaded")End SubThis event opens the standard File Save dialog window. The ﬁle to bedownloaded is the one that is currently selected in the ﬁles list. A RETR com-mand is issued via a passive connection. The returned data is the contentsof the remote ﬁle. In order to write this data to the local disk, a newFileStream is created. The FileStream constructor includes the name ofthe local ﬁle. The data to be written to disk is converted from a string into abyte array and then passed to the write method of the stream. A message isshown to signify the end of the operation.Add these namespaces to both forms, and we’re ready to go:C#using System.Net;using System.Net.Sockets;using System.Text;using System.IO;VB.NETImports System.NetImports System.Net.SocketsImports System.TextImports System.IOTo test the application, ensure that you have an FTP server running, andthen execute the application from Visual Studio .NET. Note that whencompiling this application, if you are prompted for “Sub Main” (VB.NET),Figure 6.5FTP client Logondialog.6.4An overview of FTP193Chapter 6please select the Main form for this purpose. When the application is run-ning, you may enter your FTP account details into the Logon window (Fig-ure 6.5) and press Logon.On the next screen (Figure 6.6), you can navigate around the remotecomputer’s ﬁle system by clicking on the folder names in the list provided.To return to where you started, press the root button.To upload a ﬁle, click Upload, then select a ﬁle. Wait until you receive amessage box, and then you should see the ﬁle in the ﬁles list. To download aﬁle, click on a ﬁle in the ﬁles list, and then press Save. Choose a destinationfor the ﬁle (e.g., the desktop). When you press OK, wait until you see amessage box, and then you should see the ﬁle on the desktop. 6.4.8FTP support in .NET 2.0In the .NET Framework version 2.0 (Whidbey), FTP support is includedin the WebClient class, thus negating the need to use either socket-levelprogramming or COM objects. The following code illustrates the simplic-ity of this new feature:C#public void downloadFile(){ string filename = "ftp://ms.com/files/dotnetfx.exe"; WebClient client = new WebClient();Figure 6.6FTP client ﬁle-managementdialog.1946.5Conclusion client.DownloadFile(filename,"dotnetfx.exe"); }VB.NETSub downloadFile() Dim Filename as String = "ftp://ms.com/file/dotnetfx.exe" Dim client as new WebClient Client.DownloadFile(filename,"dotnetfx.exe")End sub6.5ConclusionThis chapter gave a brief overview of Microsoft ﬁle sharing and two imple-mentations of FTP clients in .NET: one for a ﬁve-minute solution and oneas a substantial, ﬂexible implementation of the protocol. The next chapter deals with the practical issues faced by developersworking with real-world networks. It is designed to help solve networkproblems for individual scenarios and provide tips and tricks to keep yoursoftware from crashing on unusual network setups. 195 7 Securing a Network: Firewalls, Proxy Servers, and Routers    7.1Introduction This chapter deals with the practical issues of setting up a network and net-work architecture in general. Knowing how networks differ from a pro-grammatic perspective can help ﬁx a lot of network-application-relatedbugs. Furthermore, basic working knowledge of network setup is essentialin the day-to-day life of many developers.This chapter is structured in two sections. The ﬁrst section explains howto create a network from autonomous, stand-alone machines. Immediatelyfollowing that is a discussion of common devices that form gatewaysbetween your network and the Internet. These gateway devices can oftencreate problems for your software by imposing their own restrictions andregulations. By being able to detect and work around these problems, yourapplication will be more stable in a mass-market environment.  7.1.1Building a network from scratch If you are developing a point-of-sale system for a supermarket, each ter-minal will need to communicate with a central server to consolidate theday’s takings and process stock levels. This is not easily achievable withouta network. In many cases, you can’t just give a shopkeeper a CD andexpect him to ﬁgure out how to get every computer in his business tied toa single network. Choosing a topology If you have only two computers that you want to network, and there is noneed for a third, then the most economic solution is a unshielded twistedpair (UTP) crossover cable (not UTP patch cable). This can be used to linktwo computers directly. 196 7.1 Introduction There are three main types of physical connections in modern network-ing: UTP, BNC, and wireless. The latter uses radio waves to transmit databetween terminals, whereas the other two systems use wired connections.The beneﬁts of a wireless network are quite readily apparent. Users canmove within the radius of the transmitter and maintain a connection to theInternet; however, wireless networks are slower than their wired counter-parts. For instance, a typical network card can support a 100-Mbps connec-tion, whereas the equivalent wireless card will operate at 11 or 54 Mbps,and the actual throughput may only be a fraction of that. A network cablecan easily stretch for 100 meters, but wireless hubs have a radius substan-tially smaller than that. Wireless networks are more expensive but are simi-lar in architecture to a UTP network.The differences between UTP and BNC are most apparent in the typeof cable used to connect the computers. UTP cable resembles a phone line,only thinner, whereas BNC is coaxial, like a television cable. BNC plugs arecircular, whereas UTP plugs (RJ45 connectors) are rectangular. UTP is laid out in a star topology, where each computer has a dedicatedline to its nearest hub or router. In smaller networks, one of the computerson the network uses a modem (or other device) to connect to the ISP. Everyother machine on the network then shares this Internet connection. Onlarger networks, a router connects directly to a line provided by the serviceprovider. This arrangement provides better performance because the routerhelps steer the data, as well as being dedicated to the task of providing a net-work connection; however, it adds extra cost to the network. BNC is laid out in a bus topology. This is where all computers on thenetwork share a single line of communications. In a BNC network, eachcomputer has a T-shaped connection attached to its network card. At eachend of the wire is a terminator. BNC is rare nowadays, and it is more com-mon to use either UTP or wireless.Other networks, based on Universal Serial Bus (USB) and serial connec-tions, are available, but they should be avoided because of possible interop-erability problems.    Setting up a network When building a UTP network, ensure that each computer is wired to ahub, and make sure the hub is powered. In a BNC network, each computeris connected to its neighbor, and a BNC terminator should be afﬁxed to theend of the wire.  7.1 Introduction197Chapter 7 Users will expect a ﬁle-sharing mechanism on the network, so you shouldprovide this from the outset. To provide this mechanism, you have to choosea unique name for each computer on the network. To name a computer onWindows 2000, right-click My Computer  →→→→ Properties  →→→→ Network Identiﬁca-tion, and select Properties. On Windows XP, right-click My Com-puter  →→→→ Properties  →→→→ Computer Name  →→→→ Change (Figure 7.1).Enter in a computer name, and if required, a workgroup. Then pressOK. You may need to restart the computer for these changes to take effect. You will need to bind some protocols and services to your new networkcard. To do this in Windows 2000, right-click My NetworkPlaces  →→→→ Properties  →→→→ Local Area Connection  →→→→ Properties. On Windows XP,click Control Panel  →→→→ Network Connections  →→→→ Local Area Connection.In this box, you need to see three things: Client for Microsoft Networks,File and Printer Sharing for Microsoft Networks, and Internet Protocol(TCP/IP). If any of these is missing, press the Install button. Figure 7.1 Windows,Computer NameChanges Dialog. 198 7.1 Introduction The next task is to set up the TCP/IP settings for the computer. To openthe dialog box, highlight Internet Protocol (TCP/IP) and click Properties. If this computer is part of a larger network, there may be a DHCP serveron the network, which automatically assigns IP addresses. In this case,choose the options “Obtain an IP address automatically” and “Obtain DNSserver address automatically.” Otherwise, set the ﬁelds manually.You have to set the IP addresses as nonpublic addresses, and each com-puter must be assigned a unique IP address. A series of IP addresses couldbe 10.0.0.1, 10.0.0.2, 10.0.0.3, and so on. The subnet mask should be255.255.255.0. Press OK to save the settings. To share a folder, right-click on the folder, click Properties  →→→→ Sharing.Click on Share this folder (on Windows XP, you will need to click on a dis-claimer message, “If you understand the risk but still want to share the con-tents of this folder”).  Note:  If you intend to accept Windows 9x clients, you will need to have a guest account on your system.To limit remote users’ access to your ﬁles on Windows 2000, click Per-missions. On the next window you can grant and revoke read, write, andchange permissions to any or all users on the network. On Windows XP, thishas been simpliﬁed to a checkbox, “Allow network users to change my ﬁles.”Another useful feature of networks is the ability to remotely print docu-ments. This section assumes that you have a printer attached to a computeron your network. On Windows 2000, click Start  →→→→ Settings  →→→→ Printers. OnWindows XP, press Start  →→→→ Control Panel  →→→→ Printers and Faxes. Right-clickon a printer that you would like to share, and select the Sharing option.Then select Shared As, enter a unique name, and a descriptive name for theprinter. You can set the level of control users will have over the printer fromthe Security tab. Press OK to complete the process. How to set up a virtual private network A virtual private network (VPN) is used to give a remote client secureaccess to a LAN. The remote client will have transparent (albeit, slower)access to the LAN and will be able to share ﬁles and use remote printers,and so forth. 7.2 Building an enterprise network199Chapter 7 A VPN operates over the point-to-point tunneling protocol (PPTP) orlayer 2 tunneling protocol (L2TP). The local trafﬁc is layered on top of this tosupport true transparency and support for nonroutable protocols such as IPX.A VPN has some advantages over dial-in connections to a network.These are security, where every transmission is encrypted, and transparency,because the client can retain its own IP address.To become a VPN client, on Windows 2000, click Start  →→→→ Settings  →→→→     Net-work Connections, and then click New Connection wizard. On WindowsXP, click Start  →→→→ Control Panel  →→→→ Network Connections  →→→→ Create a New Con-nection  →→→→ Next. Click on “Connect to a private network through the Internet” on Win-dows 2000 or “Connect to the network at my workplace,” then Virtual Pri-vate Network Connection on Windows XP.  When prompted, type in the IP address of the VPN gateway. Thisshould be as supplied by the administrator of the VPN. Press Finish toﬁnalize the settings. 7.2Building an enterprise network Up to now, private IP addresses have been mentioned in passing, more byway of highlighting the fact that they exist, how to recognize them, andhow to understand their limitations. In enterprise networks, it is unfeasibleto supply every user with a separate direct connection to the Internet. It isnormal to channel each user’s network connection to a gateway, and fromhere, a direct connection to the Internet exists. The term  gateway  is generic. It simply means the device that is con-nected to both the internal network and the Internet. This can be either acomputer or a stand-alone device. Both proxies and routers can function asa gateway. A proxy would be in the form of software running on a com-puter, and a router being a stand-alone device. A router is always preferableto a proxy in every respect, apart from cost.  7.2.1Routers If you have inherited a network running a proxy server that is experiencingperformance problems or on which users are ﬁnding it difﬁcult to run cer-tain applications, then you should consider using a router instead of aproxy server.  200 7.2 Building an enterprise network A router is generally a piece of hardware. It performs minimal processingof packets. This means that a router can operate at speeds far exceeding thoseof a proxy server. It also steers packets in the right direction, instead of blindlysending them out to the next router up the chain. Furthermore, its presence ismuch more transparent to clients, and it has much higher resiliency.If you look at the rear panel of a router, you will see several LAN con-nections: one marked WAN, a power lead, and possibly a serial connection.To wire one up, you connect the WAN port to the cable provided by yourISP. Each LAN port can be connected to a computer, or hub. You need toobtain the following information from your ISP: (cid:1) What ﬁxed IP address to use, or whether to obtain one via DHCP  (cid:1) The IP address of the default gateway (cid:1) What subnet mask to use (cid:1) The primary and secondary DNS Each computer behind the router must then set its default gateway andDNS servers to the IP address of the gateway and set the IP addresses to pri-vate addresses.  7.2.2Firewalls A good analogy for a ﬁrewall is a switchboard operator for a company. If anunsolicited salesperson rings, chances are the operator will not forward thecall through; however, if an employee makes an outgoing call to the sales-person, the operator will not block the call.  Calls made from employeeswithin the company go through the switchboard, so the caller ID thatappears on the recipient’s phone will be that of the switchboard rather thanthe direct line. A ﬁrewall performs this function, only at very high speeds, either in soft-ware or hardware. It is possible to buy stand-alone ﬁrewalls, but every mod-ern router will contain some sort of ﬁrewall (sometimes referred to as  packetﬁltering ). A ﬁrewall can also come in the guise of software. In Administrative Tools  →→→→ Services, you will see the Internet ConnectionFirewall (ICF) service. You can press Start to enable this service. This willsufﬁce to protect a single computer from the ravages of the Internet. Thereis no need to use this service if your local gateway uses a ﬁrewall. 7.2 Building an enterprise network201Chapter 7 Proxies Proxies should only be considered when you have no budget to develop anetwork or only two or three computers require an Internet connection.Proxies will slow down your Internet connection considerably. First, if you expect to have multiple users sharing an Internet connec-tion, you will need something more than a dial-up connection. ISDNwould be the minimum, with DSL being a preferred option. You will havealready created your LAN, with one computer equipped with a DSLmodem of some description. This computer runs the proxy server software.All other computers on the network have to use this computer as a viapoint to request Web pages and so forth. This means that every Internet-connecting program needs to know the IP address of the proxy. In InternetExplorer, this is set from Tools  →→→→ Internet Options  →→→→ Connections  →→→→ LAN Set-tings  →→→→ Use a proxy server.Proxies come in two ﬂavors: application proxies and circuit-level proxies.Application proxies normally accept only one protocol, such as HTTP. Cir-cuit-level proxies can accept any protocol over IP. The most popular circuit-level proxy is known as SOCKS; a popular HTTP proxy is Wingate(www.wingate.com).  Some applications will only work with an HTTP proxy or SOCKS. It isgenerally a case of determining which applications you need to use and get-ting a proxy server to suit. The SOCKS protocol is deﬁned in RFC 1928. In order to use a SOCKSproxy, the client must ﬁrst authenticate itself. This consists of an initialshort (3-byte) negotiation followed by a vendor-speciﬁc subnegotiation.Once the client is authenticated, a packet to the outside world can be sentwhen preceded by a short (10-byte) header. This header includes the portand IP address of the destination. Responses are tagged with the sameheader, only reversed.  Network address translators All gateways perform some sort of network address translation, or NAT. Forsimplicity’s sake, any device that implements NAT will also be referred to asa NAT. A NAT rewrites the IP header of packets leaving the network with anew, public IP address. When the response packet returns, the NAT willhave remembered what computer had originally issued the request andrewrite the IP header with the appropriate private IP address.  202 7.2 Building an enterprise network A proxy server, although it can provide HTTP requests that emanatefrom a different IP address than the source, is not considered to implementNAT. This is because the input is different from the output in more waysthan just the IP address. More speciﬁcally, a proxy server expects a header inthe data sent to it to indicate the destination host and port. True NATdevices do not require this identiﬁcation. When conﬁguring a computer touse a NAT, it is only necessary to change the gateway and DNS settings(under TCP/IP settings) to allow all applications to communicate transpar-ently through the NAT. With a proxy, there is no such global setting, andeach application has to be conﬁgured independently. NAT was developed by Cisco, but it is now an Internet standard (RFC1631). Several different translations can be performed on networkaddresses, which can be used to provide more ﬂexible gateways to the Inter-net. Not all gateway devices support the full range of NAT operations.Static NAT is where every private IP address has its own correspondingpublic IP address. This means that each computer is distinguishable fromthe outside world, yet not necessarily accessible.Dynamic NAT is where every private IP address is mapped to a uniquepublic IP address, although not always the same one every time. Overloading is the most common form of NAT (sometimes called  portaddress translation ). It maps every private IP address to a single public IP,but differentiates the connections by placing them on different local ports(multiplexing).Overlapping is used when two LANs with different subnets are joinedtogether. Every private IP on one network is mapped to a unique private IPon the second network, and vice versa. Overlapping can be done by usingstatic or dynamic NAT. The latter is a more complex undertaking.When there is a mixture of public and private IP addresses on the stubdomain (a private LAN), the NAT will not perform any translation on pub-lic IP addresses, but the packets still pass through the device.As mentioned previously, a NAT needs to store information about whatpackets it has sent out, so that it can appropriately return the replies. Indynamic NAT, an IP address mapping cannot change midway through aTCP/IP session. Therefore, a NAT also needs to store which TCP/IP ses-sion is mapped to each IP address. Because a computer can theoreticallymaintain a TCP/IP session on each port, a network of 100 computerscould maintain 6 million concurrent sessions.  7.3 Tunneling out of an enterprise network203Chapter 7 The number of clients a router can process should be stated by the man-ufacturer; however, as a rough estimate, every entry in the NAT translationtable is 160 bytes long; therefore a router with 2 Mb of RAM could handleabout 12,000 sessions, which is more than enough for any ofﬁce network.A device that implements NAT will probably also include some sort ofpacket ﬁltering and logging to compliment it. After all, what is the point ofproviding the ultimate network if the users spend all of their time browsingpornographic Web sites (unless of course you’re in that industry)? Filteringcan block various destination addresses, port numbers, and so forth. Log-ging will record packets entering and leaving the router, but not the internalnonroutable trafﬁc. On large networks, a packet analyzer will have difﬁ-culty recording the activities of 100 users who all decide to ghost theirmachines at once. Note:  Ghost is a product developed by Symantec that can replicate hard disks over a network  http://www.symantec.com/ghost/ . Even with its complexity, NAT eases system administration (e.g., if yourserver goes down, and you can’t get physical access to it). You can use theremote-access facility that comes standard on most routers to change theinbound mapping to point to the IP address of a server that you do haveaccess to, and the problem will be solved, for Internet clients anyway.In order to provide a backup Internet connection, you will require a sec-ond router. This router ensures that outgoing trafﬁc to the backup ISP willbe appropriately mapped. Providing both routers are interconnected, whenone ISP fails, the other router will take all of the trafﬁc from the other, andwill do so without any human intervention. This type of arrangement isknown as a  multihomed network . This is made possible because of the vari-ous ways routers interoperate. They use the interborder gateway protocol(IGBP) to talk to each other inside a LAN and the exterior gateway proto-col (EGP) to communicate with the ISP’s routers.A piece of NAT software named Sygate is freely available, but hardwareimplementations are recommended 7.3Tunneling out of an enterprise network If your customer already has a functioning network, but your softwaredoesn’t work on it, you can’t ignore the problem, or you will lose the sale. 204 7.3 Tunneling out of an enterprise network There are always two ways to ﬁx a problem: address it or avoid it. Bothmethods are equally valid and equally applicable to different situations.Take the situation where a teleconferencing application does not operatebehind a ﬁrewall. You can either move the server outside the ﬁrewall, set upport forwarding to tunnel through the ﬁrewall (or router), or bounce dataoff a proxy server to avoid the ﬁrewall. The ﬁrst two options may involveyou going on-site to ﬁx the problem, whereas the latter involves renting adedicated server and doing some programming.  Proxy tunneling If you write an application for the mass market, you have to bear in mindthat not all software users will have either direct or transparent connectionsto the Internet. In some cases, users may access the Internet via a proxy.Unfortunately, there is no foolproof means of detecting if a proxy is in useon a network, where it is, or what type it is. Unlike routers, proxies are not transparent to clients.  You will need tomodify your code to account for a proxy. If you are using the  HTTPWebRe-quest  and are trying to navigate an application proxy, then this is relativelystraightforward:  C# WebProxy myProxy= new WebProxy("proxyserver",8080);myProxy.BypassProxyOnLocal = true;String url = "http://www.yahoo.com";HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);request.Proxy = myProxy; VB.NET Dim myProxy As WebProxy = New WebProxy("proxyserver", 8080)myProxy.BypassProxyOnLocal = TrueDim url As String = "http://www.yahoo.com"Dim request As HttpWebRequest = _CType(HttpWebRequest.Create(url), _   HttpWebRequest)request.Proxy = myProxy Note that the above code requires the  System.Net   namespace. 7.4 Avoiding the networking pitfalls205Chapter 7 Firewall tunneling If a ﬁrewall is in place that blocks all ports, then you could make changes tothe ﬁrewall to allow access on your requested port. Firewalls are generallyaccessed either through a Web interface ( http://192.168.1.1  or similar) orvia a serial connection. You will need to have the manual and passwordsclose at hand. Some routers offer port forwarding to bypass ﬁrewalls. This iswhere the data directed at the router’s IP address on a speciﬁed port is for-warded to a speciﬁed internal IP address. The process is transparent to bothends of the connection.Finally, if you have no access to the ﬁrewall, or you want to provide auser-friendly solution, you can bounce data from a proxy. This is where themachine behind the ﬁrewall opens a steady TCP and connects to a proxymachine, which is outside of the ﬁrewall, and the proxy allows the client toconnect to it. Data from the client to the proxy is forwarded via the previ-ously opened connection. This is the technique used by Instant Messengerapplications. A coded example of this solution is provided at the end ofthis chapter. 7.4Avoiding the networking pitfalls Prevention is always better than cure. If you are releasing a product into thewild, it is almost certain that some user will have such an unusual networkconﬁguration that your software won’t work. To them, their network isn’tunusual, and in fact a hundred other users out there have the same prob-lem, but they didn’t bother to tell you that your software doesn’t work.  Port conﬂict If your software can’t start on its default port, it should move to anotherport, or at least prompt the user to enter a new port. If you don’t providethis function, you will encounter one of two problems: (1) users will inevi-tably run software that uses the same port as yours and that they don’t wantto stop using, or (2) ﬁrewalls may already be set up to allow trafﬁc throughsome ports; even if your customer doesn’t use a ﬁrewall, their ISP might.The client who is waiting to connect to your software will need to knowthat it has moved port. You could simply display a message box and ask theuser to type in the new port, or you could use a DNS request (Chapter 12)to tell users which ports the server is listening on and connect to each inturn. Generally, this approach is overkill.  206 7.4 Avoiding the networking pitfalls Tip:  It is possible to force sockets to listen on an occupied port, by settingthe reuse-address option thus:  Socket.SetSocketOption(SocketOption-Level.Socket, SocketOptionName.ReuseAddress,1) . This approach is not recommended as it may cause undeﬁned behavior. Dynamic IP addresses Another problem that is regularly encountered is dynamic IP addresses.This is where the IP address of the computer changes every time it goesonline. Left unchecked, many applications will grab the local IP addresswhen the application starts and assume that is will remain static for the life-time of the application. When users have dial-up connections, they couldobtain ﬁve different IP addresses in the space of an hour under normalusage (signing on and off the Internet). This situation poses a problem forserver applications because there is no way a client can know where itshould connect. This can be solved either on a case-by-case basis or by host-ing an IP tracking mechanism. Software such as “no-IP” can be used to map a dynamic IP address to aDNS name. The process of using this software is relatively straightforward,but it may be unfeasible to request software users to use this product tosolve the dynamic IP address issue. The alternative is to have the computerperiodically post its IP address to a server, whereupon the server will storethe IP address, along with a timestamp and a human-readable identiﬁer.Clients can look this up and connect to the dynamic IP address. The time-stamp ensures that ofﬂine computers will be deleted from the listing.When posting an IP address, care must be taken to ensure that the IP isvalid for the Internet. A LAN IP such as 192.168.0.1 is no good to a clienton the other side of the world. 7.4.1Firewall tunneling If you sell ﬁrewalls for a living, look away now because this section describeshow to tunnel ﬁles (or any other data) through a ﬁrewall, in either direc-tion, rendering the whole purpose of a ﬁrewall defunct. If you are develop-ing a peer-to-peer application for the open market, however, thisinformation opens up a whole new customer base. To best illustrate the concept of ﬁrewall tunneling, let’s look at an anal-ogy: Imagine two prisoners, one in Alcatraz and another in the Bastille.They can both make one phone call, but obviously, neither is allowed to 7.5 Conclusion207Chapter 7 receive calls. The prisoner in Alcatraz knows an escape route from theBastille, which he wants to tell his partner in crime. How does he send themessage? The prisoner in Alcatraz phones his friend’s home answeringmachine and leaves a message of where the escape route is located. The pris-oner in the Bastille then makes his call to his own answering machine,where he hears the message and uses the information to escape. The same technique is used to tunnel though ﬁrewalls. One user sendsdata to a publicly accessible server with a header indicating from whom thedata came and who the intended recipient is. The recipient is constantlypolling this server, querying it for any new messages. Once the data hasbeen posted up to the server, the recipient can then download it andinstruct the server to remove its copy. The system could be implemented roughly by simply using an emailaccount. Both computers would poll it using POP3 and post new messagesusing SMTP. Otherwise, Microsoft Message Queue (MSMQ) server (seeChapter 15) could be used for the same purpose.  Peer-to-peer architecture Peer-to-peer (P2P) is a way of structuring distributed applications such thatthe individual nodes have symmetric roles. Rather than being divided intoclients and servers, each with distinct roles (such as Web clients versus Webservers), in P2P applications a node may act as both a client and a server.P2P systems are generally deployable in an ad hoc fashion, without requir-ing centralized management or control. They can be highly autonomousand can lend themselves to anonymity.In order to function correctly, each node on a P2P network must knowthe location of at least one other node. In some implementations, a nodecould contact an indexing server, which would return a list of other nodeson the P2P network. The beneﬁt of P2P networks is that they are fault tol-erant (i.e., there is no single point of failure), and the network can continueto operate smoothly even if several nodes are missing. Furthermore, thecombined processing power and storage available across a multitude ofnodes can greatly exceed what is practical to combine into one centralserver computer. Famous P2P software includes Napster and Kazaa.  7.5Conclusion This chapter should contain enough information to enable anyone todevelop a simple LAN. More importantly, it illustrates network peculiarities 208 7.5 Conclusion of which a developer must be aware when developing distributed applica-tions for enterprise environments. With this information, it should be possible to develop an approach thatwill render the low-level network implementation details (such as privateand dynamic IP addresses) transparent to higher-level processes.The next chapter deals with data encryption and security. It explainshow the industry-standard encryption mechanisms work and how they canbe proclaimed to be “unbreakable.”  209 8 Protecting Data: Encryption 8.1Introduction Without encryption, it is easy for anyone with access to a computerbetween you and the receiver to view transmitted data while it is in transit.In fact, this book includes a chapter that describes how to monitor networktrafﬁc at the packet level. This network trafﬁc could include conﬁdential orprivileged information that you transmit from your computer. Security is paramount in ﬁnancial transactions and many other types ofinformation exchange with an associated dollar value. It is vitall that privi-leged information remain in the hands of its rightful owners and not strayinto the hands of hackers, or worse, the public domain.This chapter is divided into three sections. The ﬁrst section describeshow encrypted data is cracked and how to recognize weak encryption,which effectively makes your data less secure than plain text. The secondsection describes asymmetric encryption, which is most applicable forsecuring data in transit. The chapter concludes with a discussion on sym-metric encryption, which is ideal for use in conjunction with other types ofencryption for added performance and security. 8.2Cryptanalysis In order to appreciate fully what cryptography is, it is necessary to under-stand the difference between good and bad encryption. When encryptiontechniques are used incorrectly, they are worse than having no encryption atall because system users will mistakenly trust the encryption, when it is notsecure at all. This section should plainly demonstrate how to recognizeweak encryption and how simply it can be broken. 210 8.2 Cryptanalysis Any encryption algorithm that substitutes one character for another canbe broken without knowing the key or even the mechanism by which thetext was encrypted.  The process is known as  frequency analysis .The most common character used in English text is the space character(ASCII code 32). After that comes the letter “e,” then “t,” right down to“z,” the least common.The complete list is: (space)etaoinshrdlcumwfgypbvkjxqz In ciphers, where each letter is substituted by another letter, the fre-quency of its occurrence is similar to that of plain English. For instance, a piece of text was taken randomly out of a text ﬁle andencrypted. The resultant cipher text was: v`z/bnv/a`{/c`na/}ja{/cjn|j/cjak/`}/`{gj}xf|j/{}na|ij}/{gj/`{gj}/bjkfzb/{`/na`{gj}/z|j}/jwlj(cid:0){/n|/(cid:0)n}{/`i/{gj/(cid:0)j}bnaja{/{}na|ij}/n|/(cid:0)}`yfkjk/nm`yj/`i/{gj/|`i{xn}j/(cid:0)}`kzl{ The most common character is “/,” so we can assume that it is the spacecharacter. After that, “j” can be assumed to be “e,” and so on down to “z.”The result seems more like a human language, but only a few English wordscan be seen (e.g., “not,” “the,” “to”). fou cif not moin aent meise mend oa otheagwse tainsrea the othea cedwuc to inothea usea ebpelt is liat or the leacinent tainsrea is laoywded ivoye or the sortgiae laodupt Looking through the text, a few words would make sense if one letterwere changed. Because character substitution ciphers must have one-to-onemapping between characters, if one letter is changed, then the letter it ischanged to must also be substituted.We can therefore make three assumptions:1.othea   →→→→  other: a = r, r = ?2.o?   →→→→  on, of: Assume “not” is correct, r = f, f = ?3.?ou   →→→→  you: f = y, “y” doesn’t appear in cipher text 8.2 Cryptanalysis211Chapter 8 This process can be iterated several times. Each step makes the ciphertext more legible. you ciy not moin rent meise mend or othergwse trinsfer the other cedwuc to inother user ebpelt is lirt of the lercinent trinsfer is lroywded ivoye of the softgire lrodupt 1.trinsfer   →→→→  transfer: i = a2.softgare   →→→→  software: g = w, w = ?3.otherw?se   →→→→  otherwise: w = I you cay not moan rent mease mend or otherwise transfer the other cediuc to another user ebpelt as lart of the lercanent transfer as lroyided avoye of the software lrodupt 1.cediuc   →→→→  medium: c = m2.?ermanent   →→→→  permanent: l = p, p =?3.mease   →→→→  lease: m = l you may not loan rent lease lend or otherwise transfer the other medium to another user eb?ept as part of the permanent transfer as proyided avoye of the software produ?t 1.produ?t   →→→→  product: p = c2.ebcept   →→→→  except: b = x3.proyided   →→→→  provided: y = v4.avove   →→→→  above: v = bVoilà! The message has been decrypted. you may not loan rent lease lend or otherwise transfer the other medium to another user except as part of the permanent transfer as provided above of the software product Frequency analysis software can be programmed to run without anyhuman intervention and could easily recognize and decrypt ﬁles or networkdata that was encrypted with any of the ciphers mentioned to date. If the 212 8.4 Asymmetric encryption message had not been in English, or was audio data rather than text, thisapproach would not have worked.  8.3Terminology Cryptography carries with it a vast amount of jargon, some of which isunavoidable when discussing the subject. (cid:1) Plain text  is digital information that is unencrypted. (cid:1) Cipher text  is digital information that has been encrypted. (cid:1) A  key  is a piece of digital data that is used by a computer program toconvert plain text, to cipher text or vice versa.  (cid:1) A  cryptographic algorithm , or  cipher , is a prescribed algorithm for con-verting plain text into cipher text and back again, using a key. (cid:1) Strength  is the measure of the difﬁculty a hacker would have convert-ing cipher text to plain text without having access to the key. 8.4Asymmetric encryption If you imagine a padlock, it consists of a bolt, a key, and a locking mecha-nism. Each padlock is unique. They all have different keys and differentlocking mechanisms. The way these padlocks are made in the factory, it isimpossible to guess the shape of the key by simply looking at the lockingmechanism. It is possible to close the bolt on the padlock without having akey. This makes it much more secure than the previous encryption methodsdescribed, which would be more akin to a combination lock, where thecombination needs to be set when inserting the bolt into the lock.Now imagine three people: a tourist, a travel agent, and a thief. Thetourist wants to send $1,000 to the travel agent, but if the thief gets to thekey before the travel agent, he will steal the money. If the tourist were to putthe money in a box and then lock it, the travel agent would not have a wayto reopen the box if she did not have the key. If the key were to be sent, thethief would surely steal the key and the money before anyone knew whathad happened.The solution is that the tourist asks the travel agent to send him an openpadlock and keep the key. The tourist then puts the money in the box, locksit, and sends it back. The travel agent still has the key, so she can open the 8.5 Using RSA as asymmetric encryption213Chapter 8 box and bank the money. The thief may have seen the padlock, and mayeven have been able to examine the locking mechanism, but he could notopen it.In this case, the padlock key is called the  private key , and the lockingmechanism is the  public key . In computing, the padlocks become one-waymathematical equations, and the keys become numbers.An example of a one-way mathematical equation is as follows:A prime number is a number that is divisible only by itself and 1(e.g., 13). Given a number  z , which is a product of two prime num-bers  x  and  y , determine the values of  x  and  y , where neither  x  nor  y  isequal to 1.For example, what two numbers multiply to give 22,321? To solve this problem by hand, you could multiply every prime numberbetween 1 and 149 (square root of 22,321). Other techniques to factorlarge primes exist, but this would take a computer merely seconds to do;however, if the number to be factored was in the order of billions, it nolonger remains feasible for desktop PCs to solve.The Rivest-Shamir-Adleman (RSA) is quite slow in comparison to mostof the shared key (symmetrical) encryption technologies available. In a sys-tem using a combination of public key and shared key, overall encryptionspeed can be increased.If a message is encrypted with the Triple Data Encryption Standard(3DES), then the key is encrypted with RSA. The same level of security isoffered, but with a much faster execution.  8.5Using RSA as asymmetric encryption RSA (Rivest Shamir Adleman, named after its inventors) is implementedin the  RSACryptoServiceProvider  class. It generates public and privatekeys 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 . Addtwo more buttons:  btnEncrypt  and  btnDecrypt . To further assist code 214 8.5 Using RSA as asymmetric encryption development, we will encapsulate the core cryptographic functions in aclass. Therefore, add a new class to your project named  clsCryptography .First, the  Cryptography  class has to implement both encryption anddecryption. The cryptographic framework works from byte arrays prima-rily, so the functions will accept and return byte arrays. As mentioned ear-lier, RSA is asymmetric, so it uses two keys, which happen to be stored inXML (string) format. Open  clsCryptography  and enter the following code: C# namespace rsa{    public class clsCryptography  {    private RSACryptoServiceProvider RSA;    public string PublicKey;    public string PrivateKey;    public byte[] Encrypt(byte[] Data, string PublicKeyIn)    {      RSA.FromXmlString(PublicKeyIn);      return RSA.Encrypt(Data, false);        }    public byte[] Decrypt(byte[] Data, string PrivateKeyIn)    {      RSA.FromXmlString(PrivateKeyIn);      return RSA.Decrypt(Data, false);    }  }} VB.NET Namespace rsa Public Class clsCryptography  Private RSA As RSACryptoServiceProvider  Public PublicKey As String  Public PrivateKey As String  Public function Encrypt(Data as byte(),PublicKeyIn as _    string) as Byte()    RSA.FromXmlString(PublicKeyIn) 8.5 Using RSA as asymmetric encryption215Chapter 8     Return RSA.Encrypt(Data,False)  End function  Public Function Decrypt(Data as byte(),PrivateKeyIn as_  string) as Byte()    RSA.FromXmlString(PrivateKeyIn)    Return RSA.Decrypt(Data,False)  End Function End ClassEnd Namespace RSA cryptography is of little value if we have no keys to work from.These keys should be generated when the class is created, so we insert thiscode as the constructor of  clsCryptography : C# public clsCryptography(){  CspParameters cspParams = new CspParameters();  cspParams.Flags = CspProviderFlags.UseMachineKeyStore;  RSA = new RSACryptoServiceProvider(cspParams);    PublicKey = RSA.ToXmlString(false);  PrivateKey = RSA.ToXmlString(true);} VB.NET Public Sub New()  Dim cspParams As CspParameters = New CspParameters()   cspParams.Flags = CspProviderFlags.UseMachineKeyStore  RSA = New RSACryptoServiceProvider(cspParams)    PublicKey = RSA.ToXmlString(False)  PrivateKey = RSA.ToXmlString(True)End Sub The Boolean parameter sent to  ToXmlString  indicates whether the pri-vate key should be included in the XML output. The following namespaces must be added to the  clsCryptography  class: 216 8.5 Using RSA as asymmetric encryption C# using System;using System.Security.Cryptography; VB.NET imports Systemimports System.Security.Cryptography Open the application, go to the point in the code directly after the con-structor of the form, and enter some private variables: C# public class Form1 : System.Windows.Forms.Form{  private rsa.clsCryptography clsRSA = new  rsa.clsCryptography();      private byte[] Decrypted;  private byte[] Encrypted;  ... VB.NET Public Class Form1  Inherits System.Windows.Forms.Form  Private clsRSA As clsCryptography =  New clsCryptography()   Private Decrypted() As Byte  Private Encrypted() As Byte To display the generated keys on-screen, we append the XML to the sta-tus textbox at startup: C# private void Form1_Load(object sender, System.EventArgs e){           tbStatus.Text += "Private key is:\r\n" + clsRSA.PrivateKey + "\r\n";   tbStatus.Text += "Public key is:\r\n" + clsRSA.PublicKey + "\r\n";        } 8.5 Using RSA as asymmetric encryption217Chapter 8 VB.NET Private  Sub Form1_Load(ByVal sender As Object, ByVal e _As System.EventArgs)    tbStatus.Text += "Private key is:"    tbStatus.Text += clsRSA.PrivateKey + vbcrlf   tbStatus.Text += "Public key is:" + vbcrlf    tbStatus.Text += clsRSA.PublicKey + vbcrlfEnd SubTo encrypt the text, we convert it to a byte array and pass it to theclsCryptography class; the process is similar with decryption. Click on thetwo buttons in turn and add the following code:C#private void btnEncrypt_Click(object sender, System.EventArgs e){  byte[] PlainText =   System.Text.Encoding.ASCII.GetBytes(tbWorking.Text);  Encrypted = clsRSA.Encrypt(PlainText, clsRSA.PublicKey);  tbWorking.Text =  System.Text.Encoding.ASCII.GetString(Encrypted);               }private void btnDecrypt_Click(object sender, System.EventArgs e){          Decrypted = clsRSA.Decrypt(Encrypted,  clsRSA.PrivateKey);  tbWorking.Text =  System.Text.Encoding.ASCII.GetString(Decrypted);}VB.NETPrivate  Sub btnEncrypt_Click(ByVal sender As Object, _ ByVal e As System.EventArgs)  Dim PlainText() As Byte = _  System.Text.Encoding.Encoding.ASCII.GetBytes _       (tbWorking.Text)   Encrypted = clsRSA.Encrypt(PlainText, _        clsRSA.PublicKey)  tbWorking.Text = _2188.6Symmetric encryption   System.Text.Encoding.ASCII.GetString(Encrypted)End Sub Private  Sub btnDecrypt_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)          Decrypted = clsRSA.Decrypt(Encrypted, clsRSA.PrivateKey)  tbWorking.Text = _    System.Text.Encoding.ASCII.GetString(Decrypted)End SubNo additional namespaces are required. To test the application, run it from Visual Studio .NET. Type somethinginto the box provided and press Encrypt (Figure 8.1). The text shouldchange into an unrecognizable series of characters. Pressing Decrypt willrevert this back to plain text again.8.6Symmetric encryptionSymmetric encryption is when the same key is used for encryption anddecryption. It is commonly used in conjunction with asymmetric encryp-tion for performance purposes. When used on its own, it is important thatthe key never travel on an insecure channel and that is be delivered by handto the receiver on physical media, such as a disk or smart card. It is not suit-able for network use by itself; however, asymmetric encryption can providea means to deliver these keys on a secure channel and, therefore, makessymmetric encryption viable for networked applications. Symmetric encryption is, however, suitable for securing software anddatabases because the administrator can hold this key on a disk in a securelocation. Without the key, symmetric algorithms are actually more difﬁcultto break than RSA for the same key size. 8.6.1Using 3DES as symmetric encryptionA famous author, Simon Singh, once offered $15,000 to crack a short pas-sage of text encrypted with 3DES. One year later, a Swedish team man-aged to crack the message and claimed the prize. Unbeknown to SimonSingh at the time, the message had actually been singleDES and thus sub-stantially less secure. 3DES remains one of the world’s unbroken crypto-graphic algorithms.8.6Symmetric encryption219Chapter 8Create an application in Visual Studio .NET as usual, and draw a text-box, tbFile. Include three buttons named btnEncrypt, btnDecrypt, andbtnBrowse. You will also require an Open File Dialog control named open-FileDialog.Directly following the class deﬁnition, add a public DESCryptoService-Provider object as follows:C#public class Form1 : System.Windows.Forms.Form{  private DESCryptoServiceProvider des;VB.NETPublic Class Form1  Inherits System.Windows.Forms.Form  Private des As DESCryptoServiceProviderThis public object will contain the symmetric keys required to encryptand decrypt ﬁles. In this application, the keys are not saved to disk; they areonly stored within this object. Click on the Browse button and enter the following code:C#  private void btnBrowse_Click(object sender,Figure 8.1Asymmetricencryptionapplication.2208.6Symmetric encryption  System.EventArgs e)  {    openFileDialog.ShowDialog();    tbFile.Text  = openFileDialog.FileName;  }VB.NET    Private  Sub btnBrowse_Click(ByVal sender As Object, _    ByVal e As System.EventArgs)    openFileDialog.ShowDialog()    tbFile.Text  = openFileDialog.FileName  End SubThis code is pretty self-explanatory. It opens the standard File Opendialog window and displays the ﬁlename of the selected ﬁle in the tbFiletextbox. Click on the Encrypt button and enter the following code:C#  private void btnEncrypt_Click(object sender,  System.EventArgs e)  {    string encFile = tbFile.Text + ".enc";    FileStream fs = new FileStream(encFile, FileMode.Create,      FileAccess.Write);    StreamReader sr = new StreamReader(tbFile.Text);      string strinput = (sr).ReadToEnd();       sr.Close();            byte[] bytearrayinput  =             Encoding.Default.GetBytes(strinput);            des = new DESCryptoServiceProvider();            ICryptoTransform desencrypt =               des.CreateEncryptor();            CryptoStream cryptostream =             new CryptoStream(fs, desencrypt,            CryptoStreamMode.Write);            cryptostream.Write(bytearrayinput, 0,            bytearrayinput.Length);            cryptostream.Close();8.6Symmetric encryption221Chapter 8      fs.Close();      MessageBox.Show("encrypted");  }VB.NETPrivate Sub btnEncrypt_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) _Handles btnEncrypt.Click        Dim encFile As String = tbFile.Text + ".enc"        Dim fs As FileStream = New FileStream(encFile, _             FileMode.Create,FileAccess.Write)        Dim sr As StreamReader = New _             StreamReader(tbFile.Text)        Dim strinput As String = (sr).ReadToEnd()        sr.Close()        Dim bytearrayinput() As Byte = _             Encoding.Default.GetBytes(strinput)        des = New DESCryptoServiceProvider        Dim desencrypt As ICryptoTransform = _             des.CreateEncryptor()        Dim CryptoStream As CryptoStream = _             New CryptoStream(fs, desencrypt, _             CryptoStreamMode.Write)        cryptostream.Write(bytearrayinput, 0, _             bytearrayinput.Length)        cryptostream.Close()        fs.Close()        MessageBox.Show("encrypted")End SubThe encryption procedure consists of several steps. The ﬁrst step iswhere an output ﬁle is prepared. The output ﬁle has the same name as theinput ﬁle, except that the extension .enc is appended to the end of the ﬁle-name. The input ﬁle is then read in from memory by passing the ﬁlenameas a parameter to the constructor of a StreamReader object and calling theReadToEnd method to pull in the ﬁle contents to a string. This string is thenconverted to a byte array. The next step in the encryption process is the application of DES. Herethe public DES variable is instantiated. At this point, a unique symmetrickey is generated within the DESCryptoServiceProvider class. The encryp-tion mechanism works as a stream. As with most value-added streams, an2228.6Symmetric encryptionexisting stream is passed to the constructor of the new stream. In this case,the output ﬁle stream is the underlying stream used by the cryptographicstream. This stream then processes and writes out the byte array read infrom the input ﬁle using the Write method. The stream is then closed, anda message is shown on the screen.Now double-click on the Decrypt button, and enter the following code:C#private void btnDecrypt_Click(object sender, System.EventArgs e)  {    FileStream fsread = new FileStream(tbFile.Text,    FileMode.Open, FileAccess.Read);    ICryptoTransform desdecrypt = des.CreateDecryptor();    CryptoStream cryptostreamDecr = new CryptoStream(fsread,    desdecrypt, CryptoStreamMode.Read);    string decryptedFile = new StreamReader(        cryptostreamDecr).ReadToEnd();    FileInfo fi = new FileInfo(tbFile.Text);    string origionalFile = tbFile.Text.Substring(0,       tbFile.Text.Length - fi.Extension.Length);    StreamWriter fileWriter = new       StreamWriter(origionalFile);    fileWriter.Write(decryptedFile);    fileWriter.Close();    MessageBox.Show("decrypted");  }VB.NETPrivate Sub btnDecrypt_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) Handles _btnDecrypt.ClickDim fsread As FileStream = _   New FileStream(tbFile.Text, _   FileMode.Open, FileAccess.Read)   Dim desdecrypt As ICryptoTransform = _      des.CreateDecryptor()   Dim cryptostreamDecr As CryptoStream = _      New CryptoStream(fsread, _      desdecrypt, CryptoStreamMode.Read)   Dim decryptedFile As String = New _      StreamReader(cryptostreamDecr).ReadToEnd()8.6Symmetric encryption223Chapter 8  Dim fi As FileInfo = New FileInfo(tbFile.Text)  Dim origionalFile As String = _     tbFile.Text.Substring(0,tbFile.Text.Length _          - fi.Extension.Length)  Dim fileWriter As StreamWriter = New _     StreamWriter(origionalFile)  fileWriter.Write(decryptedFile)  fileWriter.Close()  MessageBox.Show("decrypted")End SubThe decryption process is a little easier because our symmetric key isalready generated. Three streams are used to decrypt the ﬁle on disk. Theﬁrst stream is a FileStream that reads the cipher text from the ﬁle on disk.The crypto stream is created from our public des variable, which wouldhave been previously instantiated in the encryption process. TheFileStream is passed as a parameter to the constructor of the crypto stream,which decrypts the data from the stream. To extract the data quickly fromthe crypto stream, a StreamReader is used, which uses the ReadToEndmethod to pull the decrypted data into a string. Finally, using a bit of string manipulation, the .enc extension isremoved from the ﬁlename, and a StreamWriter dumps the string contain-ing the decrypted data to disk. This stream is then closed, and a message isdisplayed on-screen.As usual, the following namespaces are required:C#using System.IO;using System.Text;using System.Security.Cryptography;VB.NETImports System.IOImports System.TextImports System.Security.CryptographyTo test this application, run it from Visual Studio .NET. Press Browseand locate a ﬁle on your hard disk. Press the Encrypt button, and press OKwhen the message box appears. You will notice that a new ﬁle has been cre-ated with the extension .enc. If you open this ﬁle in Notepad, it will appear2248.7Piracy protectionto be garbage. If you wish, you can delete or move the original ﬁle. Press theBrowse button again, and select the .enc ﬁle (Figure 8.2). When the mes-sage box appears, you will notice that the original ﬁle has been re-created. 8.7Piracy protectionSoftware is expensive to create, but costs virtually nothing to duplicate. Peo-ple generally have few qualms about sharing a CD ﬁlled with copyrightedmaterial with anyone who they believe will ﬁnd it useful. To the softwareproducer, this can be considered a lost sale. The most common form of software piracy is a CD-R with the licensecode scribbled across the front. The only real way to guarantee that thesame license code cannot be used on multiple machines is to track thesecodes from a central server.A common way to generate license codes is to choose a large randomnumber (a), and increment it with a multiple of a smaller random number(b). This number would generally be encrypted so that it is not easily mem-orable. A key that the user enters (c) can be deemed to be valid if (c - a) mod b = 0 Your software can broadcast this key on the local network or a centralserver to ensure uniqueness of the key. It is difﬁcult for an attacker to deter-mine a second valid key from c if a and b are sufﬁciently large.An other way to protect software is if your software generates a large ran-dom number (n) at the time of purchase. This number can be encrypted byyour private key to produce a second number (m) and returned to yoursoftware. If  m, decrypted with the public key, is n, then the key is valid.Because n is random, m is not valid for any other copy of the software.Figure 8.2Symmetricencryptionapplication.8.8Conclusion225Chapter 8Hackers can also use programs to cycle automatically through millions ofkey combinations by simulating a user typing into your “enter license key”window. For this reason, you should have your software close after 3 failedattempts to enter the license key and delete itself after 100 failed attempts.Beyond license fraud, there are people who make a hobby out of disas-sembling executable ﬁles and disabling piracy protection. There is no sureﬁreway to defeat this type of attack, but it can be made difﬁcult by duplicatingthe piracy protection code several times throughout the application.8.8ConclusionThis chapter has introduced the concept of data encryption in .NET withboth asymmetric and symmetric forms. Also covered was the basic theorybehind cryptographic systems and cryptanalysis.It cannot be stressed enough that you are more likely to get a faster, sim-pler, stronger, and sometimes even more interoperable method when usingthe standard encryption mechanisms used in .NET as compared to home-grown encryption algorithms. The next chapter deals with authentication, the science of knowing withwhom you are dealing. This page intentionally left blank  227 9 Controlling User Access: Authentication and Authorization 9.1Introduction Until now, we have assumed that hackers use network-snifﬁng software tointercept conﬁdential data; however, there is as much danger in forged orspoofed data. Chapter 5 on SMTP/POP3 demonstrates how the sendercan specify the originating email address arbitrarily, making it easy tosend 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 anemail purporting to be from a professor saying, “All lectures have beencanceled. You can all go home now, and we’ve decided to give you all anA+ on your exams.”This chapter deals with the tricky issue of conﬁrming that a client iswho he says he is and that no fraudulent activity is taking place. Authenti-cation systems must be able to validate supplied credentials securely againsttrusted sources and also to ensure that the message has not been tamperedwith in transit. This chapter is structured in four distinct sections. The ﬁrst section dealswith Microsoft authentication systems, such as NTLM and .NET Passport.This is followed by a discussion on techniques to detect data tampering. Thechapter continues with an explanation of secure sockets layer (SSL), one ofthe most common security mechanisms for data delivered via Web sites. Thechapter concludes with coverage of some other related authentication tech-nologies, such as .NET permissions and legacy authentication schemes. 9.2Authentication techniques To guarantee the identity of a client, you need to trust one piece of infor-mation that is unique to that client and that cannot easily be determined or 228 9.2 Authentication techniques faked (e.g., IP address, Windows username/password, or some other cre-dential). Authentication systems prevent the masquerading of credentials,but they cannot protect against a careless user compromising the security ofa Windows password.Several different types of authentications are applicable to different sce-narios. If you are developing a solution for an ISP, then the chances are theISP can be sure which client base has what IP address and, thus, can use IPaddresses as credentials. When developing a Windows-only intranet appli-cation, you can trust Windows logins. Internet service developers may use acombination of the IIS authentication options or a custom username andpassword system.The most basic form of authentication is IP address validation, whereaccess to information is granted only if the IP address of the client is withina given range. This scheme is used by ISPs to limit access to technical sup-port to current customers. They can do this because their customers willhave IP addresses in the range that was assigned to the ISP. IP spooﬁngwould defeat form of authentication, but this is not an easy undertaking.Only a select few determined hackers are capable of carrying it off. 9.2.1IIS authentication Although this book focuses on stand-alone software, using IIS as a serveris always an option not to be dismissed lightly. This approach doesremove some of the ﬂexibility from the system, and it becomes necessaryto use the encryption and authentication mechanisms that Microsoft pro-vides, rather than proprietary protocols. IIS5 provides ﬁve kinds ofauthentication: anonymous, basic, NT challenge/response (NTLM, stan-dard for Windows 9x and NT), Integrated Windows (Kerberos, standardfor Windows 2000 and XP), and digest. The latter two options are notavailable on IIS4. Each kind of authentication offers varying degrees ofinteroperability and security. The most basic form of IIS authentication, if it has a right to be calledauthentication, is anonymous. This is where the clients do not have to sup-ply any credentials and are automatically granted IUSR (guest) privileges.This allows them to read and write ﬁles, but not to generate any graphicalinterface or access certain API functions. One step above this is basic authentication. This forces the client to sup-ply credentials in base64 (basically, clear text). This system is completelyinteroperable between browsers, but offers very little security; however,when combined with SSL, this is a secure solution. 9.2 Authentication techniques229Chapter 9 Moving toward the Microsoft world, we have NT challenge/response, orNTLM. This is quite secure and cannot be broken without signiﬁcanteffort, but it can be hacked by a determined individual. NTLM is sup-ported on IIS4 and all versions of Internet Explorer. The credentials sup-plied by the client will have to match those of a local account on the server.Digest authentication was introduced in IIS5. There has not beenwidely publicized case of any hacker breaking digest encryption. It is com-patible with most versions of Internet Explorer. Again, the credentials sup-plied by the client will have to match those of a local account on the server.Kerberos provides one of the highest levels of security for authenticationavailable over the Internet. It requires access to a domain controller andworks only on IIS5 and recent versions of Internet Explorer.To access authentication options on IIS, click Start  →→→→ ControlPanel  →→→→ Administrative Tools  →→→→ Internet Information Services. Right-click onthe server in question, and click Properties. Select the Directory Securitytab and press Edit (Figure 9.1).The screen in Figure 9.1 shows the authentication options for IIS. Inthis case, the lowest form of security is selected as the default. Options Figure 9.1 IIS authenticationdialog. 230 9.3 Microsoft .NET Passport authentication exist to upgrade this to basic authentication or NTLM. The option fordigest authentication is not enabled here because this particular server hasno access to a domain controller. Apart from the security versus interoperability trade-off, there is also asecurity versus performance trade-off. On a benchmark computer (Pentium3, 450 MHz, 128 Mb RAM), each of the preceding authentication systemswas tested for performance in a high-load environment. When accepting anonymous connections, the computer handled 860requests per second. With basic authentication, the computer handled 780requests per second, proving to be the fastest authentication mechanism,albeit with little security. NTLM incurred an additional overhead, reducingthe overall speed to 99 requests per second. Digest authentication clockedin at 96 requests per second. With Kerberos authentication, the computercould handle only 55 requests per second. Finally, with full-blown SSL, theserver dropped as low as a mere 2 requests per second.  9.3Microsoft .NET Passport authentication Passport authentication is where users can be identiﬁed by their Hotmailemail addresses. Other passport-supporting email accounts do exist, butHotmail is the most prevalent. This form of authentication is not meant tosecure international fund transfers, but it certainly sufﬁces for personalcommunications. The advantage of passport over in-house-developed sys-tems is that many people already have a Hotmail email address, and thus donot have to reregister their details. Passport authentication is used primarily for Web sites, but can also beapplied to applications, MSN Messenger being a good example. The onlinehelp for .NET Passport is centered on Web site development, but it is possi-ble to implement a proxy service built as a programmatically accessible Website that your application could connect to. This could then be used toobtain personal details from a user-supplied passport. Passports are available in two ﬂavors: preproduction and production.Preproduction passports are free, but only a limited amount of personalinformation can be extracted from a passport. Production passports are notfree, and Microsoft will inspect your site or application before you aregranted a production passport. You do, however, get the beneﬁt of beingable to read full personal details from visitors’ passports. Furthermore, apreproduction passport does not have the functionality to perform a sign-out operation.  9.3 Microsoft .NET Passport authentication231Chapter 9 The ﬁrst step in implementing .NET Passport–enabled software is toobtain what is known as a site ID. This is simply a number, which is givento you when you register your details with Microsoft .NET Services Man-ager. On  www.netservicesmanager.com , click Applications  →→→→ Create Applica-tion, and then ﬁll in all of the necessary ﬁelds. Once you have a site ID, you can download the Passport SDK from www.microsoft.com/net/services/passport/developer.asp . This SDK should beinstalled on the server on which you intend to deploy the Web site, or theproxy server that is to provide passport services to the .NET-enabled stand-alone applications. The ﬁnal step is to download a private key that is to be installed on thedeployment server. This can be downloaded under Manage Applications, in.NET Services Manager. The key comes in the form of an executable,which must be run from the command prompt as follows: Partner###_#.exe /addkeyPartner###_#.exe /makecurrent /t 0 Where ####_# differs for different installations and site IDs. At thispoint, you may then run the passport administration utility (Figure 9.2). Figure 9.2 .NET PassportManagerAdministrationdialog. 232 9.4 Hashing information Enter your site ID in the space provided. Then press the CommitChanges button. To test the system, start and stop IIS using Computer Management, orthe IIS snap-in, then press Refresh Network Map, and Commit Changesagain. You should see the following Web site appear:  http:/localhost/passport-test/ , as shown in Figure 9.3.Pressing the Sign-In button will bring you to a cobranded login page forPassport. On successful login, the browser will display the URL that wasspeciﬁed during the site ID signup procedure  9.4Hashing information Hashing is a one-way algorithm in which data can be converted to a hashvalue, but a hash value cannot be converted back to meaningful data. It isused in conjunction with encryption to ensure that messages are not tam-pered with in transit. Modern hashing systems include Message Digest(MD5) and Secure Hash Algorithm (SHA-1).When a hash value is produced from a block of plain text, it should becomputationally difﬁcult to generate a different block of text that wouldyield the same hash value. A standard property of hashing algorithms is thata small change in the input text creates a large change in the hash value.Hash algorithms always produce output values with the same length,regardless of the amount of input text. In practice, a hash value is generated for a given message, and then themessage and the hash code are encrypted together. When the message isdecrypted, a hash must match that of the message; otherwise, it may havebeen tampered with. Even though it would be impossible for a hacker to Figure 9.3 .NET Passport testpage. 9.4 Hashing information233Chapter 9 read this encrypted message in transit, it would be possible for him toalter the contents of the transmission, which could result in misinter-preted communications.Another useful application of hashing is the secure storage of usernamesand passwords. If an application stores username and password pairs in adatabase, it is easy for a professional hacker to access this database and readthem off. If the password is hashed, the hacker cannot tell what the originalpassword was. When the legitimate user enters a password into your appli-cation, the entered password will be hashed, and if it matches the value inthe database, then the user is granted access.This may pose a problem if the user forgets a password because theapplication cannot determine the original password from the hash. A sys-tem should be in place to replace passwords from an administrator’saccount. More importantly, if the hacker can guess the hashing algorithmused, he could generate a hashed password, replace the existing one, andgain access. For this reason, where data integrity can be compromised, thehashing procedure should be combined with another form of encryptionsuch as 3DES.Hashing can also be used to prevent unauthorized data mining of onlineservices. If you provide an Internet-based service that is accessed via a cus-tom-made client (e.g., a DLL that provides currency conversion based onlive exchange rates, or whatever), and you want only paying customers toaccess the service, the last thing you want is a competitor to use a packet-snifﬁng tool to determine what data you are sending to the server and createa product that uses your service without paying you. The obvious solutionis to use asymmetric encryption; however, let us imagine that performanceis the overriding factor, and asymmetric encryption would cause an unac-ceptable processing overhead.A keyed hash (or a hash of the payload with an appended secret string ofcharacters) of the data included in the header creates only a small overhead,but it makes the header impossible to re-create without knowing the hash key.This affords no security against your competitors’ reading what is being sentback and forth to your server, but it prevents them from generating their ownclient; however, you should take care that the client cannot be disassembledto view this key easily. A tool such as Dotfuscator (www.preemptive.com)canbe used to obfuscate the code and help hide this key from prying eyes.A real-world example of this system in use is the Google toolbar. Thisutility can display Google’s page rank for any given Web page. Google doesnot want people to be able to data-mine these values using automatic pro- 234 9.4 Hashing information cesses, so the request that the toolbar component makes to the Googleserver contains a keyed hash code for the Web site in question. It is difﬁcultto predict this hash code, and requests made without this code return anerror. Full-blown asymmetric encryption was not used in this case becauseit would have created unacceptable overhead for the servers to return datathat is basically available to anyone.  9.4.1Hashing algorithms .NET provides support for two hashing algorithms: Secure Hash Algo-rithm, or SHA, and Message Digest, or MD5 in the classes  SHA1Managed and  MD5CryptoServiceProvider , respectively. SHA is speciﬁed by the secure hash standard (SHS). The hash is gener-ated from 64-byte blocks, which are transformed by a combination of one-way operations and a function of previous block transforms. The speciﬁca-tion for SHA is widely available and can be implemented easily in any otherlanguage, so it is suitable for use on solutions with clients written in otherlanguages or on other platforms. The speciﬁcation is available in RFC 3174( ftp://ftp.rfc-editor.org/in-notes/rfc3174.txt ).Hashing algorithms do not involve the same high-level mathematics asRSA or elliptic curve encryption. This is not to say that it is advisable totry to develop your own hashing algorithm. Breeds of algorithms that aresimilar in function to hashing are cyclic redundancy check (CRC) func-tions. CRC functions provide a ﬁxed-length checksum for any giveninput. Although these may be one-way functions and provide generallyhigher throughput than hashing algorithms, they do not afford the samelevel of security.There are four different variations of the SHA available for use in .NET: SHA1Managed  (20-byte hash),  SHA256Managed  (32-byte hash),  SHA384Managed (48-byte hash), and  SHA512Managed  (64-byte hash). The longer the hash, themore difﬁcult it is for a hacker to create a new message with the same hash,although a longer hash may contain more information about the originalmessage.  In either case, SHA1 should be sufﬁcient. 9.4.2Using SHA  Create a new Windows application in Visual Studio .NET as usual, anddraw two textboxes on the form named  tbPlaintext  and  tbHashed . A but-ton named  btnHash  is also needed. Click on the button and enter the fol-lowing code: 9.4 Hashing information235Chapter 9 C# private void btnHash_Click(object sender, System.EventArgs e){  byte[]  entered =  Encoding.ASCII.GetBytes(tbPlaintext.Text);  byte [] hash = new SHA1Managed().ComputeHash(entered);  tbHashed.Text = Encoding.ASCII.GetString(hash);} VB.NET Private  Sub btnHash_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  Dim entered() As Byte = _  Encoding.ASCII.GetBytes(tbPlainText.Text)   Dim hash() As Byte = New _  SHA1Managed().ComputeHash(entered)   tbHashed.Text = Encoding.ASCII.GetString(hash)End Sub This code converts the text entered in  tbPlainText  into a byte array,and then passes this byte array to the  ComputeHash  method of the SHA1Managed  class. The hash code is generated by an instance of this SHA1Mananged  class. By substituting  SHA1Managed  with  SHA512Managed  oreven  MD5cryptoServiceProvider , the hashing will take place using thatalgorithm instead of SHA1.You will also require the relevant namespaces: C# using System.Text;using System.Security.Cryptography; VB.NET Imports System.TextImports System.Security.Cryptography To test this, run it from Visual Studio .NET, type some text into thetextbox provided, and press the button. A ﬁxed-length hash will appear inthe second textbox as shown in Figure 9.4. A small change in the plain textwill cause a large change in the hash value, which will always remain thesame length. 236 9.6 Certiﬁcates 9.5SSL The most common form of security used over the Internet is secure socketslayer, or SSL. SSL is a secure stream protocol, which uses both symmetricand asymmetric encryption, combined with digital certiﬁcates to provideauthentication. Digital certiﬁcates can be bought from a certiﬁcate author-ity (CA) such as Thawte or Verisign. In order to buy a certiﬁcate, you needto prove your identity beyond doubt, which may involve providing a letterfrom your bank manager or the articles of association for your company.The certiﬁcate contains details of your server’s DNS name and your orga-nization, and it is encrypted by the CA’s private key. The public key forevery CA is installed in every browser, so anyone on the Internet can besure that your company, and no one else, operates the machine that servesthe page they are looking at. Furthermore, all data sent between client andserver is encrypted with RSA. SSL is deﬁned in RFC 2660. The most common use for SSL is securingWeb pages, but it can be equally applied to email, FTP, or news. HTTPover SSL (HTTPS) operates on port 443; SMTP over SSL (SSMTP) oper-ates on port 465; and NNTP over SSL (SNNTP) operates on port 563. 9.6Certiﬁcates SSL provides end-to-end encryption and authentication. Whenever abrowser views a secure Web site, a padlock appears in the status bar. Click-ing this icon will authenticate the server as belonging to a particular com-pany, in a speciﬁc location. This is achieved by using server certiﬁcates. Figure 9.4 Secure hashingapplication. 9.6 Certiﬁcates237Chapter 9 A certiﬁcate has to be issued by a CA in order to be globally accepted. Itis possible to create self-signed certiﬁcates, but these would generally bedeemed trustworthy only within your organization. A digital certiﬁcatesigned by XYZ Corporation would be trusted by employees of XYZ, butprobably wouldn’t be trusted by the general public. The most common form of digital certiﬁcate is known as X.509. This isan international standard maintained by the IETF Public Key Infrastruc-ture (PKIX) working group. X.509 comes in three versions: v1, v2, and v3.Version 3 is the most commonly used form. The certiﬁcate comprises vari-ous ﬁelds that identify the holder, the issuer, and the certiﬁcate itself: (cid:1) Serial number:  The unique serial number on every certiﬁcate createdby an issuer  (cid:1) Signature:  Identiﬁes the makeup of the certiﬁcate, represented by anobject identiﬁer (OID). (cid:1) Validity period:  The date at which the certiﬁcate becomes and ceasesto be valid (cid:1) Subject:  The owner of the private key (cid:1) Public key:  The key that will decrypt the certiﬁcate hash  (cid:1) Signed hash:  The hash of the certiﬁcate encrypted with the private keyof the CAThe subject has several predeﬁned ﬁelds (Table 9.1), some of which arestandard, but there are no strict guidelines as to what can or cannot beincluded in the subject line. The certiﬁcate is not encrypted, but its contents are held in eitherBase64 or Distinguished Encoding Rules (DER). This is to facilitate trans-mission over plain-text email and to make it more difﬁcult to sniff certiﬁ-cates from the network. Some common myths about certiﬁcates should be mentioned in thiscontext. Contrary to popular belief, certiﬁcates are not only used for Webpage authentication; they can also be used in email (S/MIME) and general-purpose data (IPSec). Another common fallacy is that the private keyshould be kept in the HTTP root of the server it authenticates. This is akinto leaving the house keys under the doormat. The private key should neverbe transmitted over the Internet because if it is lost, it will need to be reis-sued. The issuer generally does not retain private keys for customers. 238 9.7 Server certiﬁcates  9.7Server certiﬁcates  If you ever enter your credit card details into a Web site, the ﬁrst thing youshould look for is the padlock icon in the bottom right-hand corner of thebrowser. This icon not only means that the communications with theremote site are secure, but also that you can click on this icon and assureyourself that the company with which you are dealing is the owner of theWeb site you are viewing.Server certiﬁcates for real-world Web sites need to be obtained from aCA. For development purposes, however, it is possible to make self-signedcertiﬁcates. A useful utility for creating self-signed certiﬁcates is IBM Key-Man ( www.alphaworks.ibm.com/tech/keyman ). You could also use Keytool,which is part of the Java SDK from Sun, but this utility doesn’t have a GUIand is more awkward to use. The steps to enable HTTPS using a self-signed certiﬁcate and IBM Key-Man on IIS are as follows:1.Click Control Panel  →→→→ Administrative Tools  →→→→ Internet InformationServices.2.Expand the tree, and right-click Default Web Site, then clickProperties.3.Select the Directory Security tab, then click Server Certiﬁ-cate  →→→→ Next  →→→→ Create a new certiﬁcate. Table 9.1 Standard subject markers for digital certiﬁcates. Subject MarkerMeaning CCountrySPState/provinceSStateLLocalityOOrganizationOUOrganizational unitCNCommon nameEEmail 9.8 Client certiﬁcates239Chapter 9 4.Select Prepare a request now, then ﬁll in your details on eachpage, pressing the Next button when complete. The default loca-tion for the certiﬁcate request ﬁle is  c:\certreq.txt .5.Install IBM KeyMan, and run it from Start  →→→→ Programs  →→→→ IBMKeyMan  →→→→ KeyMan.6.Select Create New, then PKCS #12 Token, then the tick icon.7.Select Actions  →→→→ Generate Key, then click the tick icon to acceptthe default RSA 1024bit security. 8.Select Actions  →→→→ Create Certiﬁcate  →→→→ Self-Signed certiﬁcate, thenﬁll in your details in the space provided. Press the tick icon twiceto proceed.9.Select Actions  →→→→ Create Certiﬁcate  →→→→ Sign a PCKS #10 request,then enter c:\certreq.txt into the box provided and press thetick icon.10.Select a location to save the certiﬁcate. You should use the .cerextension for your ﬁle. 11.Going back to the directory security settings for IIS, select ServerCertiﬁcate, press Next, then click Process the pending request.12.Enter the path of the .cer ﬁle produced by KeyMan. Then pressNext and then Finish.13.You can now test HTTPS on your local server, by entering https://localhost in your browser. You will receive a warning saying that“The security certiﬁcate was issued by a company you have notchosen to trust.” This is because it was signed by yourself, not aCA. Pressing Yes on this warning will allow you to proceed.9.8Client certiﬁcatesWhereas server certiﬁcates authenticate a Web site to a browser, a client cer-tiﬁcate authenticates a browser to a server. Client certiﬁcates are only usedfor maximum-security Web sites, such as online business banking. Clientcertiﬁcates are available free of charge from Thawte. They are used to sendand receive encrypted emails and to authenticate your email address torecipients. You will need to have a passport or social security number toreceive a client certiﬁcate. A basic client certiﬁcate only authenticates the email address, not theperson who sent the email. To get your name on the certiﬁcate, you need to2409.8Client certiﬁcateshave a bank manager or attorney vouch for your identity. The rest of thissection assumes that you have, at this point, received a client certiﬁcatefrom Thawte. To view the client certiﬁcates installed on your system, open InternetExplorer. Click on Tools→→→→Internet Options→→→→Content→→→→Certiﬁcates (Fig-ure 9.5).Clicking on View→→→→Details→→→→Subject on this screen will show whichemail address this certiﬁcate authenticates. Pressing Export will produce anX.509 (.cer) ﬁle, which is used in the next example program. 9.8.1Microsoft Certiﬁcate ServicesAs mentioned earlier, you cannot download a software package that willcreate globally acceptable X.509 certiﬁcates on the ﬂy because the certiﬁcateissuer needs to be trusted in order for the certiﬁcate to be meaningful. Cer-tiﬁcate issuers are to legally required enforce policies and have their privatekey fully insured against theft. Figure 9.5Internet ExplorerCertiﬁcates dialog.9.8Client certiﬁcates241Chapter 9Organizations may require internal security (e.g., in a university, theservers that hold student grade information would need to be authenti-cated, to ensure that a student is not using a “poisoned” DNS server toimpersonate one of the servers). In this scenario, it might be expensive tobuy certiﬁcates for every server, and there is no need for people from out-side the campus to access the servers, let alone trust them. This is whereMicrosoft Certiﬁcate Services (MSCS) is used. MSCS runs on Windows 2000 and can generate X.509 certiﬁcates inPKCS #7 format from PKCS #10 certiﬁcate requests. MSCS can run aseither a root CA or subordinate CA and can optionally hold certiﬁcates inthe active directory. When used in conjunction with the active directory,MSCS will use this as its certiﬁcate revocation list (CRL).A CRL is a publicly accessible list of serial numbers of certiﬁcates thathave been compromised or have been shown to have been  fraudulentlyacquired. Verisign holds its CRL at http://crl.versign.com. 9.8.2Reading certiﬁcatesCertiﬁcates can be read using the X509Certificate class (Table 9.2) in.NET. Table 9.2Signiﬁcant methods and properties of X.509 certiﬁcates .Method or PropertyDescriptionGetCertHashStringReturns the hash value for the certiﬁcate as a hexadecimal stringGetEffectiveDateStringReturns the effective date of this certiﬁcateGetExpirationDateStringReturns the expiration date of this certiﬁcateGetFormatReturns the name of the format of this certiﬁcateGetIssuerNameReturns the name of the certiﬁcation authority that issued the certiﬁcateGetKeyAlgorithmReturns the key algorithm information for this certiﬁcateGetKeyAlgorithmParametersReturns the key algorithm parameters for this certiﬁcateGetNameReturns the name of the principal to which the certiﬁcate was issued2429.8Client certiﬁcatesTo write a short .NET application to read certiﬁcate ﬁles, create a newproject in Visual Studio .NET. Draw two textboxes named tbCertFile andtbDetails. Add two buttons, btnBrowse and btnExamine. You will alsorequire a File Open Dialog control named openFileDialog. Click on the Browse button and add the following code:C#private void btnBrowse_Click(object sender, System.EventArgs e){  openFileDialog.ShowDialog();  tbCertFile.Text = openFileDialog.FileName;}VB.NETPrivate Sub btnBrowse_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnBrowse.Click   openFileDialog.ShowDialog()   tbCertFile.Text = openFileDialog.FileNameEnd SubOnce we have the name of the certiﬁcate ﬁle, we can use anX.509certificate object to decrypt the ﬁle and extract some pertinentinformation. Now click on the Examine button and enter the following code:  C#private void btnExamine_Click(object sender, System.EventArgs e){ X509Certificate x509 =    X509Certificate.CreateFromCertFile(tbCertFile.Text);GetPublicKeyStringReturns the public key for the certiﬁcateGetRawCertDataStringReturns the raw data for the entire certiﬁcateGetSerialNumberStringReturns the serial number of the certiﬁcateTable 9.2Signiﬁcant methods and properties of X.509 certiﬁcates (continued).Method or PropertyDescription9.8Client certiﬁcates243Chapter 9 tbDetails.Text = x509.GetName(); tbDetails.Text += x509.GetIssuerName();}VB.NETPrivate Sub btnExamine_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) _Handles btnExamine.Click  Dim x509 As X509Certificate  x509 = X509Certificate.CreateFromCertFile(tbCertFile.Text)  tbDetails.Text = x509.GetName()  tbDetails.Text += x509.GetIssuerName()End SubYou will also need to include the relevant namespace:C#using System.Security.Cryptography.X509Certificates;VB.NETImports System.Security.Cryptography.X509CertificatesFigure 9.6Digital certiﬁcatereader application.2449.9Permissions in .NETTo test the application, run it from Visual Studio .NET. Click Browse,and locate your .cer ﬁle on disk, which you have previously exported fromInternet Explorer. Press Examine, and you should see information aboutthe issuer and the certiﬁcate owner, as is shown in Figure 9.6.9.9Permissions in .NETAny programmer familiar with Java will know about the sandbox imposedon applets. This protects client computers from accidentally executing Javacode that could potentially damage that computer. The restrictions includeﬁle reading and writing and connecting to a computer other than the onethat the applet was downloaded from. .NET offers the same sandbox architecture, which provides users with afacility to execute untrustworthy code without risking damage to theircomputers. There are several levels of sandbox, from trusted local computerto potentially dangerous code downloaded from an unknown site on theInternet.Although there is no widespread usage of .NET applets running insideWeb pages, there will be in the future. At present, the most signiﬁcantimpact the .NET sandbox will have on code is when a program is executeddirectly from a network share. This type of application deployment couldbe used on a corporate intranet, where a small application is executed froma central server at every login to record employees’ working practices andthe like.Code running from network shares is restricted in several ways. It can-not write arbitrarily to the local hard disk, but it can use an unlimitedamount of isolated storage space on the local computer or the networkshare from which it was executed. Because unmanaged code cannot be gov-erned by .NET, any assembly operating within a sandbox cannot make acall to unmanaged code. This includes any use of legacy COM controls orWindows API functions. Restrictions also apply to reading environmentvariables, performing reﬂection, and accessing the event log. To view or edit the run-time security policy in .NET, you can access thisfrom Control Panel→→→→Administrative Tools→→→→Microsoft .NET FrameworkConﬁguration. Then click Runtime Security Policy (Figure 9.7).The System.Security.Permissions namespace offers facilities to checkpermissions programmatically and impose further restrictions on the code.There seem to be very few circumstances in which it would be necessary toimpose further restrictions on an intranet application.9.9Permissions in .NET245Chapter 9An interesting feature of code access security in .NET is the isolatedstorage feature. This is one idea that was not adapted from Java, unlike somany other features of .NET. This feature enables applications deployedover an intranet or other semitrusted source to read and write a limitedamount of data to the host computers.  If the application could read andwrite arbitrarily, the privilege could be exploited maliciously to read yourpersonal emails, but isolated storage is a clever solution to this problem.Isolated storage, as the name suggests, is where a small amount of harddisk space (10 Kb) is allocated to any particular application originatingfrom a trusted Internet site. The folder where this data is placed is well awayfrom the system folders and anything else that may contain user data. Eachapplication is allocated its own folder and space such that untrusted appli-cations cannot read each other’s data. The amount of isolated storage allo-cated to any particular application is conﬁgurable. This can prevent rogueapplications from hogging too much disk space. Intranet-originating appli-cations are allocated unlimited isolated storage.To use isolated storage from within a .NET application, obtain an Iso-latedStorageFile object and then create a stream to it. This stream canthen be used in the same way as a FileStream. Figure 9.7 .NET permissionconﬁgurationutility.2469.10Financial network securityC#IsolatedStorageFile IsolatedStore;IsolatedStorageFileStream IsolatedStream;IsolatedStore =IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly,    null,null);IsolatedStream = new IsolatedStorageFileStream("data.txt",    FileMode.CreateNew, IsolatedStore);VB.NETDim IsolatedStore as IsolatedStorageFileDim IsolatedStream as IsolatedStorageFileStreamIsolatedStore = IsolatedStorageFile.GetStore _   (IsolatedStorageScope.Assembly, _    Nothing,Nothing)IsolatedStream = New IsolatedStorageFileStream _   ("data.txt", FileMode.CreateNew, IsolatedStore)Access to isolated storage in the case described above would be allocatedon a per-assembly basis. Isolated storage can also be allocated on a per-userbasis, per–domain name basis (for Internet code), or a combination of theabove. 9.10Financial network securityIf a hacker were to break into an e-commerce site successfully and capturesomeone’s credit card number, some unfortunate person would get stungﬁnancially; however, if the same thing happened on an interbank network,a country’s economy could be ruined overnight. Banks and ﬁnancial institu-tions use a diverse array of cryptography and authentication systems, whichare not accessible to the general public.The threat to security so far has been pictured as a lone hacker trying tosteal credit cards; however, a rogue nation or terrorist organization coulduse a network of supercomputers to bring down a large national bank inorder to cripple a country’s economy. Most banks use private leased lines between their branches so that theconﬁdential information does not come into contact with the public phonenetwork. ATMs usually employ VPN links to the bank. ATMs are limited9.10Financial network security247Chapter 9to a maximum value of transactions they can perform, so it would beimpossible to use one rogue VPN connection to drain a bank of its capital. When a bank needs to communicate with a second ﬁnancial institutionoverseas to perform, it must use the public phone network. Where commu-nications between two banks happen on a daily basis, a private virtual cir-cuit (PVC) is set up between the two banks. This reduces the amount offoreign data on the line, but neither bank actually owns the telecom con-nection. The communication will be very strongly encrypted in one of twomain formats: ISO 8730 or SWIFT.9.10.1X.25Many ﬁnancial protocols run over X.25 packet layer protocol rather thanIP. This offers no inherent security above the fact that it isn’t IP. X.25 wasdeveloped by the CCITT in 1978 and is in widespread use on banking net-works. Like the OSI model, it uses encapsulation, where low-level detailssuch as packet framing are not of concern at the implementation level. Itsupports many of the features of TCP/IP, such as connection orientationand data integrity provided by high-level data link control/Link access pro-cedure balanced (HDLC/LAPB). Supported speeds are from 300 bps to2.04 Mbps, on packets up to 1,024 bytes. Routing on X.25 is extensive, with support for both shared virtual circuitsand PVCs. Up to 200 virtual circuits can be supported on one X.25 line. Anetwork has to be designed to support X.25 data. In situations where X.25must travel over an IP network, LAPB can be replaced by TCP/IP. Cisco IOSsoftware or TCP X.25 gateways have the capability to do this, as described inRFC 1613.9.10.2ISO 8730 Although less common than SWIFT, this format is used frequently forinterbank transfers. It uses symmetric keys with ISO 8732 / ANSI X9.17key distribution. The key distribution center (KDC) would be run by oneor the other of the banks, or a trusted third party. An ISO 8730 message can be hashed in one of two ways: a hash can betaken of (1) the entire message, or (2) only of the details that are crucial tothe purpose of the message. In any case, every message must include thedate on which the MAC was created. Out-of-date messages can therefore bediscarded. This date value must be hashed regardless of the mode of opera-tion. Hashed ﬁelds throughout the message are clearly delimited thus:2489.10Financial network securityQD<date>DQ: The date the MAC was createdQK<key>QK: The authentication key used by the recipient QX<message ID>XQ: A unique number for that day and keyQT<transaction detail>TQ: Details of the transaction amount,currency, identiﬁcation of the parties, and the dateMQ<hash>MQ: The hash itself, being eight bytes long, separated bya space 9.10.3SWIFTThe Society for Worldwide Interbank Financial Telecommunications(SWIFT) network caters to 7,000 ﬁnancial institutions in almost 200countries around the world. It is based in Belgium, Holland, and theUnited States. To access the SWIFT network, dedicated terminals arerequired, each with SWIFT-accredited software. Communications can be made using either X.25 or Secure IP Network(SIPN). Connections to the SWIFT point of presence (POP) are madewith leased lines or dedicated ISDN links. An API is available from SWIFTto communicate on this network, but accreditation must be sought beforeany transactions are made using any in-house software.SWIFT is not solely concerned with electronic fund transfers. The pre-deﬁned communications on SWIFT are customer transfers, bank-to-bankinstructions, foreign exchange and derivatives, documentary collections,securities, syndicated loans, precious metals, travelers checks, documentarycredits, statements, advice, and general messages. When a transaction involves two currencies, control of the debit andcredit is designated to the bank at which the transaction currency is localtender. When only one currency is involved, a third-party clearinghouse orother ﬁnancial institution carries out the control of the debit and credit.9.10.4Corporate transactions When a bank has a large corporation as a client, it will expect to processmany highly sensitive transactions with them on a daily basis. Some of thesetransactions will be on a par with interbank transfers and, thus, must beafforded the same level of security.The Comité Français d’Organisation et de Normalisation Bancaires(CFONB) designed a secure ﬁle-transfer mechanism named ETEBAC 5.9.11Conclusion249Chapter 9This mechanism was designed speciﬁcally for client–bank transactions andis widely used in France and elsewhere. A common system for corporate transactions in the United Kingdom isthe Bankers Automated Clearing Service (BACS). This is used when a com-pany performs an electronic fund transfer (EFT) to pay an employee’s salaryor wishes to process a direct debit. The BACS can process anywhere up to 60million transactions per day, for more than 40,000 customers. It is accessedremotely via the BACSTEL service during ofﬁce hours. BACSTEL runs overX.25, but an IP version of BACSTEL is set to replace this standard. 9.11ConclusionThis chapter has looked at the mechanisms for guaranteeing the identity ofnetwork clients over the Web and on Microsoft networks. The structureand use of digital certiﬁcates in a distributed environment were discussed.Extending the topic to real-world scenarios, we looked at how banks useauthentication to transfer billions of dollars safely across phone lines. Sample code was provided to demonstrate how to process a credit cardpayment securely over an SSL connection. This type of facility is common-place in most e-commerce solutions, point-of-sale systems, and many othersoftware products. The next chapter introduces the concept of application scalability (i.e.,how software performs under heavy usage and when designed to run reli-ably for long periods). This page intentionally left blank  251 10 Programming for Scalability 10.1Introduction Providing software that lets people do their jobs is  usability ; providing soft-ware that lets 10,000 people do their jobs is  scalability . The term  scalability encompasses many facets of software. It means stability, reliability, and efﬁ-cient use of one or more computer resources. The goal of a scalable systemis that it must be available for use at all times and remain highly responsiveregardless of how many people use the system.Scalability, with respect to software architectures, has also come to meanextensibility and modularity. This simply means that when a software sys-tem needs to scale upward in complexity, it does not need to be overhauledwith each addition. In the following pages, you will learn about bothaspects of scalability.The ﬁrst half of this chapter deals with scalable architecture design. Thisis most largely applicable when a distributed service requires more than oneserver and the system-performance-to-hardware-cost ratio is of paramountimportance. This is followed by some hands-on code examples of how toprovide added scalability to your application, such as load balancing andefﬁcient thread management.  10.2Case study: The Google search engine Google.com  is certainly the Internet’s largest search engine. It serves 200 mil-lion requests per day and runs from more than 15,000 servers distributedworldwide. It is arguably one of the most scalable Internet services ever pro-vided to the general public. Each server that Google uses is no more powerful than the average desk-top PC. Granted, each server crashes every so often, and they are prone to 252 10.2 Case study: The Google search engine hardware failure, but a complex software failover system is employed byGoogle to account for server crashes seamlessly. This means that even if ahundred servers crashed at the same time, the service would still be availableand in working order. The rationale behind using a large number of bog-standard PCs ratherthan a few state-of-the-art servers is simple: cost per performance. It is pos-sible to buy servers with 8 CPUs, 64-Gb memory, and 8 Tb of disk space,but these cost roughly three times the price of a rack of 88 dual-processormachines with 2-Gb memory and 80-Gb disk space. The high-end serverwould serve a single client four times faster than the rack of slower comput-ers, but the rack could serve 22 times as many of concurrent users as thehigh-end server. That’s scalability.It is not the case, however, to say that one server handles one user’srequest. If this were the case, each computer would have to trawl throughthousands of terabytes of data looking for a search term. It would takeweeks to return a single query. Instead, the servers are divided into six dif-ferent groups—Web servers, document servers, index servers, spell checkservers, advertisement servers, and Googlebot servers—each performing itsown task.Google uses a sophisticated DNS system to select the most appropriateWeb server for its visitors. This DNS system can automatically redirect visi-tors to the geographically closest data center. This is why, for instance, ifyou type  www.google.com  in Switzerland, you will be directed to  www.goo-gle.ch , which is located in Zurich. But if you type  www.google.com  in Cali-fornia, you will be directed to their data center in Santa Clara. The DNSsystem also accounts for server load and may redirect to different centers inthe event of high congestion. When the request arrives at the data center, it goes through a hardwareload balancer that selects one from a cluster of available Web servers to han-dle the request. These Web servers’ sole function is to prepare and serve theHTML to the client; they do not perform the actual search. The search taskis delegated to a cluster of index servers, which lie behind the Web servers.An index server cluster comprises hundreds of computers, each holdinga subset (or shard) of a multiterabyte database. Many computers may holdidentical subsets of the same database in case of a hardware failure on one ofthe index servers. The index itself is a list of correlated words and termswith a list of document IDs and a relevancy rating for each match. A docu-ment ID is a reference to a Web page or other Google-readable media (e.g.,PDF, DOC). The order of results returned by the index depends on the 10.3 Replication and redundancy253Chapter 10 combined relevancy rating of the search terms and the page rank of the doc-ument ID. The page rank is a gauge of site popularity measured as a sum ofthe popularity of the sites linking to it. Other factors also affect page rank,such as the number of links leaving the site, the structure of internal links,and so forth. Google’s document servers contain cached copies of virtually the entireWorld Wide Web on their hard drives. Each data center would have its owndocument server cluster, and each document server cluster would need tohold at least two copies of the Web, in order to provide redundancy in caseof server failure. But document servers are not merely data warehouses.They also perform retrieval of the page title and keyword-in-context snip-pet from the document ID provided by the index servers. As the search is running, the peripheral systems also add their content tothe page as the search is in progress. This includes the spell check and theadvertisements. Once all elements of the page are together, the page isshipped off to the visitor, all in less than a second. Google also employs another breed of software, a spider named Google-bot. This piece of software, running on thousands of PCs simultaneously,trawls the Web continuously, completing a full round-trip in approximatelyone month. Googlebot requests pages in an ordered fashion, following linksto a set depth, storing the content in the document servers and updatingthe index servers with updated document IDs, relevancy ratings, and pagerank values. Another spider named Fastbot crawls the Web on a more regu-lar basis, sometimes in less than a week. It only visits sites with a high pagerank and those that are frequently updated.The Google architecture is one of the best in the world and is the pinna-cle of scalability; however, for .NET developers, there is a slight twist in thetail. Google can afford to buy 15,000 servers by cutting down on licensingcosts. This means that they use Linux, not Windows. Unfortunately, Linuxisn’t exactly home turf for .NET, but there is an open-source project calledMONO, which aims to provide a C# compiler for Linux (see  www.go-mono.com ). 10.3Replication and redundancy Keeping a backup system ready for instant deployment is  redundancy ; keep-ing the backup system identical to the live system is  replication . When deal-ing with a high-availability Internet-based service, it is important to keepmore than one copy of critical systems. Thus, in the event of software or 254 10.4 Scalable network applications hardware failure, an identical copy of the software can take the place of thefailed module. Backup systems do not need to be kept on separate machines. You canuse redundant hard drives using a redundant array of inexpensive disks(RAID) array. This is where the ﬁle system is stored on several physical harddisks. If one disk fails, then the other disks take over, with no loss of data.Many computers can read from a RAID array at once but only one com-puter can write at the same time (known as “shared nothing”). Of course,it’s not just hard disks that fail. If a computer fails, another must take overin the same way.Providing redundancy among computers is the task of a  load balancer,  apiece of hardware or software that delegates client requests among multipleservers. In order to provide redundancy, the load balancer must be able torecognize a crashed computer or one that is unable to respond in a timelyfashion. A full discussion of load balancers is included later in this chapter. Replication provides the means by which a backup system can remainidentical to the live system. If replication did not occur, data on the backupsystem could become so out-of-date that it would be worthless if set live.Replication is built into Microsoft SQL, accessible under the replicationfolder in Enterprise Manager. SQL replication works by relaying update,insert, and delete statements from one server to another. Changes madewhile the other server is down are queued until the server goes live again.  10.4Scalable network applications Server-side applications are often required to operate with full efﬁciencyunder extreme load.  Efﬁciency , in this sense, relates to both the throughputof the server and the number of clients it can handle. In some cases, it iscommon to deny new clients to conserve resources for existing clients. The key to providing scalable network applications is to keep threadingas efﬁcient as possible. In many examples in this book, a new thread is cre-ated for each new client that connects to the server. This approach,although simple, is not ideal. The underlying management of a singlethread consumes far more memory and processor time than a socket. In benchmarking tests, a simple echo server, running on a Pentium IV1.7 GHz with 768-Mb memory, was connected to three clients: a PentiumII 233 MHz with 128-Mb memory, a Pentium II 350 MHz with 128-Mbmemory, and an Itanium 733 MHz with 1-Gb memory. This semitypicalarrangement demonstrated that using the approach outlined above, the 10.5 Future prooﬁng255Chapter 10 server could only serve 1,008 connections before it reached an internalthread creation limit. The maximum throughput was 2 Mbps. When a fur-ther 12,000 connections were attempted and rejected, the throughputkeeled off to a mere 404 Kbps.The server, although having adequate memory and CPU time resourcesto handle the additional clients, was unable to because it could not createany further threads as thread creations and destructions were taking up allof the CPU resources. To better manage thread creation, a techniqueknown as  thread pooling  (demonstrated later in this chapter) can beemployed. When thread pooling was applied to the echo server example,the server performed somewhat better. With 12,000 client connections, theserver handled each one without fail. The throughput was 1.8 Mbps, vastlyoutperforming the software in the previous example, which obtained only0.4 Mbps at the same CPU load. As a further 49,000 clients connected,however, the server began to drop 0.6% of the connections. At the sametime, the CPU usage reached 95% of its peak capacity. At this load, thecombined throughput was 3.8 Mbps.Thread pooling unarguably provides a scalability bonus, but it is notacceptable to consume 95% of server resources just doing socket I/O, espe-cially when other applications must also use the computer. In order to beefup the server, the threading model should be abandoned completely, infavor of I/O completion ports (see Chapter 3). This methodology usesasynchronous callbacks that are managed at the operating system level.  By modifying the above example to use I/O completion ports ratherthan thread pools, the server once again handled 12,000 clients withoutfail; however, this time the throughput was an impressive 5 Mbps. Whenthe load was pushed to 50,000 clients, the server handled these connectionsvirtually ﬂawlessly and maintained a healthy throughput of 4.3 Mbps. TheCPU usage at this load was 65%, which could have permitted other appli-cations to run on the same server without conﬂicts. In the thread-pool and completion-port models, the memory usage at50,000 connections was more than 240 Mb, including non-paged-poolusage at more than 145 Mb. If the server had less than this available inphysical memory, the result would have been substantially worse.  10.5Future prooﬁng Scalability can also apply to the ability of an application to evolve gracefullyto meet future demands without major overhaul. When software is ﬁrst 256 10.6 Thread pooling designed, the primary goal is to hit all of the customer’s requirements or tomeet the perceived needs of a typical end-user. After rollout of the product,it may address these requirements perfectly. Once the market demandssome major change to the application, the program has to scale to meet thenew demands without massive recoding.This connotation of scalability is not the focus of the chapter, but someof the following tips may help create a future-proof application: (cid:1) Use classes instead of basic types for variables that represent elementswithin your software that may grow in complexity. This ensures thatfunctions accept these variables because parameters will not need tobe changed as dramatically in the future. (cid:1) Keep culture-speciﬁc strings in a resource ﬁle; if the software is everlocalized for a different language, this will reduce the change impact. (cid:1) Keep abreast of modern technologies. It may soon be a requirementof network applications to be IPv6 compliant. (cid:1) Provide a means to update your software automatically post deploy-ment.The key to architectural scalability is to make everything conﬁgurableand to assume nothing of the deployment environment.  10.6Thread pooling Every computer has a limit to the number of threads it can process at onetime. Depending on the resources consumed by each thread, this numbercould be quite low. When given the choice either to guarantee your soft-ware to handle a set number of clients or to “max out” the computer’sresources and risk a system crash, choose the ﬁrst option: thread pooling.Threads can improve the responsiveness of applications, where eachthread consumes less than 100% processor time. Multitasking operatingsystems share the available CPU resources among the running threads byquickly switching between them to give the impression that they are allrunning in parallel. This switching, which may occur up to 60 times persecond, incurs some small switching cost, which can become prohibitive ifthe number of threads becomes too large. Threads that are blocked waitingfor some event do not, however, consume CPU resources while they wait, 10.6 Thread pooling257Chapter 10 but they still consume some kernel memory resources. The optimum num-ber of threads for any given application is system dependent. A thread poolis useful at ﬁnding this optimum number of threads to use.To give some perspective on the effect of unpooled threading, examinethe code below: C# public static void IncrementThread(){  while(true)  {    myIncrementor++;    long ticks = DateTime.Now.Ticks – startTime.Ticks;    lock (this)    {      lblIPS.Text = "Increments per second:" +       (myIncrementor / ticks) * 10000000;    }  }} VB.NET Public Shared Sub IncrementThread() Dim ticks as long Do  MyIncrementor = MyIncrementor+1  Ticks = DateTime.Now.Ticks – startTime.Ticks  SyncLock(me)   lblIPS.Text = "Increments per second:" + _      (myIncrementor / ticks) * 10000000  End synclock Loop End Sub This code adds one to a public variable named  MyIncrementor . It thentakes an accurate reading of system time, before updating the screen toshow the level of increments per second. The  SyncLock  or  Lock  statementis used to ensure that no two threads attempt to update the screen at thesame time because this causes unpredictable results. The results shown on-screen should not be used as a measure of how quickly the computer can 258 10.6 Thread pooling perform subtraction because most of the processor time is actually spentshowing the results!When this thread was instantiated on its own, it operated at a speed of235 increments per second; however, when this thread was instantiated1,000 times and ran concurrently, the threads consumed more than 60 Mbof memory stack frame, which on some older computers would go directlyto a paging ﬁle on disk, creating a systemwide loss of performance. In agroup of 1,000 threads, the overall performance was a mere 98 incrementsper second, meaning that a single thread could take more than 10 secondsto iterate through one  while  loop. The test machine was a 333 MHz Pen-tium III with 128 Mb of RAM. With a thread pool, the optimal number of threads on this particularcomputer was found to be 25, which gave an overall operating speed of402 increments per second, with a slightly modiﬁed  Incrementer-Thread()  routine.  10.6.1Implementing a thread pool Thread pools are used constantly in servers, where a reliable service must beprovided regardless of load. This sample application is a simply a benchmark-ing utility, but with experimentation it could be adapted for any purpose.Create a new project in Visual Studio .NET and drop in two labels: lblThreads  and  lblIPS . The thread pool will be populated with threads assoon as the form loads. The exact time at which the form starts is stored ina public variable named  startTime . Every thread then adds one to a publicvariable named  myIncrementor , which helps gauge overall performance.Both of these are included in the code directly after the class declaration: C# public class Form1 : System.Windows.Forms.Form{  public  double myIncrementor;  public  DateTime startTime;  ... VB.NET Public Class Form1  Inherits System.Windows.Forms.Form  Public myIncrementor As Double  Public startTime As DateTime  ... 10.6 Thread pooling259Chapter 10 To populate the thread pool, a check is made to see how many threadsshould run together concurrently. That number of threads is then added tothe thread pool. There is no problem in adding more than the recom-mended number of threads to the pool because the surplus threads will notexecute until another thread has ﬁnished. In this case, the threads run in aninﬁnite loop; therefore, no surplus threads would ever execute. Double-click on the form and add the following code: C# private void Form1_Load(object sender, System.EventArgs e){  int workerThreads=0;  int IOThreads=0;  ThreadPool.GetMaxThreads(out workerThreads,out IOThreads);  lblThreads.Text = "Threads: " + workerThreads;  for (int threads=0;threads<workerThreads;threads++)  {    ThreadPool.QueueUserWorkItem(new    WaitCallback(Increment),this);  }   startTime = DateTime.Now;} VB.NET Private  Sub Form1_Load(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim workerThreads As Integer = 0  Dim IOThreads As Integer = 0  ThreadPool.GetMaxThreads(workerThreads, IOThreads)  lblThreads.Text = "Threads: " & workerThreads  Dim threads As Integer = 0  For threads = 1 To workerThreads  ThreadPool.QueueUserWorkItem(New WaitCallback _   (AddressOf Increment), Me)  Next  startTime = DateTime.NowEnd Sub 260 10.6 Thread pooling This code ﬁrst obtains the default number of threads that can run con-currently on the local machine using the  GetMaxThreads  method. It thendisplays this value on-screen before creating and running these threads.There can only be one thread pool in an application, so only staticmethods are called on the thread pool. The most important method is QueueUserWorkItem . The ﬁrst parameter of this method is the function(delegate) to be called, and the second parameter (which is optional) is theobject that is to be passed to the new thread. The  Increment  function isthen implemented thus: C# public void Increment(){ while(true) {  myIncrementor++;  long ticks = DateTime.Now.Ticks - startTime.Ticks;  lock (this)  {           lblIPS.Text = "Increments per second:"+   (myIncrementor/ticks) * 10000000;  } }} VB.NET Public Sub Increment()  Dim ticks As Long  Do    myIncrementor = myIncrementor + 1    ticks = DateTime.Now.Ticks - startTime.Ticks    SyncLock (Me)      lblIPS.Text = "Increments per second:" & _      (myIncrementor / ticks) * 10000000    End SyncLock  LoopEnd Sub 10.7 Avoiding deadlocks261Chapter 10 The  lock  (or  syncLock ) is required for application stability. If twothreads repeatedly access the same user interface element at the same time,the application’s UI becomes unresponsive.Finally, the threading namespace is required: C# using System.Threading; VB.NET imports System.Threading To test the application, run it from Visual Studio .NET and wait for aminute or two for the increments-per-second value to settle on a number(Figure 10.1). You can experiment with this application and see how perfor-mance increases and decreases under certain conditions, such as runningseveral applications or running with low memory.  10.7Avoiding deadlocks Deadlocks are the computing equivalent of a Catch-22 situation. Imaginean application that retrieves data from a Web site and stores it in a database.Users can use this application to query from either the database or the Website. These three tasks would be implemented as separate threads, and forwhatever reason, no two threads can access the Web site or the database atthe same time.The ﬁrst thread would be: (cid:1) Wait for access to the Web site. (cid:1) Restrict other threads’ access to the Web site. (cid:1) Wait for access to the database.  Figure 10.1 Thread pool sampleapplication. 262 10.8 Load balancing (cid:1) Restrict other threads’ access to the database. (cid:1) Draw down the data, and write it to the database. (cid:1) Relinquish the restriction on the database and Web site.The second thread would be: (cid:1) Wait for access to the database.  (cid:1) Restrict other threads’ access to the database. (cid:1) Read from the database. (cid:1)Execute thread three, and wait for its completion.(cid:1)Relinquish the restriction on the database.The third thread would be:(cid:1)Wait for access to the Web site. (cid:1)Restrict other threads’ access to the Web site.(cid:1)Read from the Web site.(cid:1)Relinquish the restriction on the Web site.Any thread running on its own will complete without any errors; how-ever, if thread 2 is at the point of reading from the database, while thread 1is waiting for access to the database, the threads will hang. Thread 3 willnever complete because thread 1 will never get access to the database untilthread 2 is satisﬁed that thread 3 is complete. A deadlock could have been avoided by relinquishing the databaserestriction before executing thread 3, or in several different ways, but theproblem with deadlocks is spotting them and redesigning the threadingstructure to avoid the bug.10.8Load balancingLoad balancing is a means of dividing workload among multiple servers byforwarding only a percentage of requests to each server. The simplest wayof doing this is DNS round-robin, which is where a DNS server containsmultiple entries for the same IP address. So when a client requests a DNS,it will receive one of a number of IP addresses to connect to. This10.8Load balancing263Chapter 10approach has one major drawback in that if one of your servers crashes,50% of your clients will receive no data. The same effect can be achievedon the client side, where the application will connect to an alternative IPaddress if one server fails to return data. Of course, this would be a night-mare scenario if you deploye a thousand kiosks, only to ﬁnd a week laterthat your service provider had gone bust and you were issued new IPaddresses. If you work by DNS names, you will have to wait 24 hours forthe propagation to take place.Computers can change their IP addresses by themselves, by simplyreturning a different response when they receive an ARP request. There isno programmatic control over the ARP table in Windows computers, butyou can use specially designed load-balancing software, such as MicrosoftNetwork Load Balancing Service (NLBS), which ships with the Windows2000 advanced server. This allows many computers to operate from thesame IP address. By way of checking the status of services such as IIS oneach computer in a cluster, every other computer can elect to exclude thatcomputer from the cluster until it ﬁxes itself, or a technician does so. Thecomputers do not actually use the same IP address; in truth, the IPaddresses are interchanged to create the same effect. NLBS is suitable for small clusters of four or ﬁve servers, but for high-end server farms from between 10 and 8,000 computers, the ideal solutionis a hardware virtual server, such as Cisco’s Local Director. This machine sitsbetween the router and the server farm. All requests to it are fed directly toone of the 8,000 computers sitting behind it, provided that that server is lis-tening on port 80.None of the above solutions—DNS round-robin, Cisco Local Director,or Microsoft NLBS—can provide the ﬂexibility of custom load balancing.NLBS, for instance, routes requests only on the basis of a percentage of theclient requests they will receive. So if you have multiple servers with differ-ent hardware conﬁgurations, it’s your responsibility to estimate each sys-tem’s performance compared to the others. Therefore, if you wanted toroute a percentage of requests based on actual server CPU usage, youcouldn’t achieve this with NLBS alone.There are two ways of providing custom load balancing, either throughhardware or software. A hardware solution can be achieved with a littleimagination and a router. Most routers are conﬁgurable via a Web interfaceor serial connection. Therefore, a computer can conﬁgure its own routereither through an RS232 connection (brieﬂy described in Chapter 4) or byusing HTTP. Each computer can periodically connect to the router and setup port forwarding so that incoming requests come to it rather than the26410.8Load balancingother machine. The hardware characteristics of the router may determinehow quickly port forwarding can be switched between computers and howrequests are handled during settings changes. This method may requiresome experimentation, but it could be a cheap solution to load balancing,or at least to graceful failover. Custom software load balancers are applicable in systems where the timeto process each client request is substantially greater than the time to movethe data across the network. For these systems, it is worth considering usinga second server to share the processing load. You could program the clientsto connect to switch intermittently between servers, but this may notalways be possible if the client software is already deployed. A software loadbalancer would inevitably incur an overhead, which in some cases could bemore than the time saved by relieving server load. Therefore, this solutionmay not be ideal in all situations. This implementation of a software load balancer behaves a little like aproxy server. It accepts requests from the Internet and relays them to aserver of its choosing. The relayed requests must have their HOST headerchanged to reﬂect the new target. Otherwise, the server may reject therequest. The load balancer can relay requests based on any criteria, such asserver CPU load, memory usage, or any other factor. It could also be usedto control failover, where if one server fails, the load balancer could auto-matically redirect trafﬁc to the remaining operational servers. In this case, asimple round-robin approach is used. The example program balances load among three mirrored HTTP serv-ers: uk.php.net, ca.php.net, and ca2.php.net. Requests from users are directedinitially to the load-balancing server and are then channeled to one of theseservers, with the response returned to the user. Note that this approach doesnot take advantage of any geographic proximity the user may have to theWeb servers because all trafﬁc is channeled through the load balancer. To create this application, start a new project in Microsoft Visual Studio.NET. Draw a textbox on the form, named tbStatus. It should be set withmultiline to true. Add two public variables at the top of the Form class as shown. The portvariable is used to hold the TCP port on which the load balancer will listen.The site variable is used to hold a number indicating the next availableWeb server. C#public class Form1 : System.Windows.Forms.Form10.8Load balancing265Chapter 10{  public int port;  public int site;VB.NETPublic Class Form1 Inherits System.Windows.Forms.Form    Public port As Integer    Public Shadows site As Integer  When the application starts, it will immediately run a thread that willwait indeﬁnitely for external TCP connections. This code is placed into theform’s Load event: C#private void Form1_Load(object sender, System.EventArgs e){     Thread thread = new Thread(new  ThreadStart(ListenerThread));  thread.Start();}VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim thread As Thread = New Thread(New ThreadStart( _    AddressOf ListenerThread)) thread.Start()End SubThe ListenerThread works by listening on port 8889 and waiting onconnections. When it receives a connection, it instantiates a new instance ofthe WebProxy class and starts its run method in a new thread. It sets theclass’s clientSocket and UserInterface properties so that the WebProxyinstance can reference the form and the socket containing the clientrequest. C#public void ListenerThread(){26610.8Load balancing  port = 8889;  TcpListener tcplistener = new TcpListener(port);  reportMessage("Listening on port " + port);        tcplistener.Start();        while(true)  {    WebProxy webproxy = new WebProxy();    webproxy.UserInterface = this;    webproxy.clientSocket = tcplistener.AcceptSocket();    reportMessage("New client");    Thread thread = new       Thread(new ThreadStart(webproxy.run));    thread.Start();   }}VB.NETPublic 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()  LoopEnd SubA utility function that is used throughout the application is reportMes-sage. Its function is to display messages in the textbox and scroll the textboxautomatically, so that the user can see the newest messages as they arrive. C#public void reportMessage(string msg){  lock(this)10.8Load balancing267Chapter 10  {    tbStatus.Text += msg + "\r\n";    tbStatus.SelectionStart = tbStatus.Text.Length;    tbStatus.ScrollToCaret();  }}VB.NETPublic Sub reportMessage(ByVal msg As String)  SyncLock Me    tbStatus.Text += msg + vbCrLf    tbStatus.SelectionStart = tbStatus.Text.Length    tbStatus.ScrollToCaret()  End SyncLockEnd SubThe core algorithm of the load balancer is held in the getMirror func-tion. This method simply returns a URL based on the site variable. Morecomplex load-balancing techniques could be implemented within this func-tion if required. C#public string getMirror(){  string Mirror = "";  switch(site)  {    case 0:     Mirror="uk.php.net";     site++;      break;    case 1:     Mirror="ca.php.net";     site++;     break;    case 2:     Mirror="ca2.php.net";     site=0;     break;  }  return Mirror;}26810.8Load balancingVB.NETPublic Function getMirror() As String  Dim Mirror As String = ""  Select Case site  Case 0    Mirror = "uk.php.net"    site = site + 1  Case 1    Mirror = "ca.php.net"    site = site + 1  Case 2    Mirror = "ca2.php.net"    site = 0  End Select  Return MirrorEnd FunctionThe next step is to develop the WebProxy class. This class contains twopublic variables and two functions. Create the class thus:C#public class WebProxy{  public Socket clientSocket;  public Form1 UserInterface; }VB.NETPublic Class WebProxy    Public clientSocket As Socket    Public UserInterface As Form1End ClassThe entry point to the class is the run method. This method reads 1,024(or fewer) bytes from the HTTP request. It is assumed that the HTTPrequest is less than 1 Kb in size, in ASCII format, and that it can bereceived in one Receive operation. The next step is to remove the HOSTHTTP header and replace it with a HOST header pointing to the serverreturned by getMirror. Having done this, it passes control to relayTCP tocomplete the task of transferring data from user to Web server. 10.8Load balancing269Chapter 10C#public void run(){  string sURL = UserInterface.getMirror();  byte[] readIn = new byte[1024];  int bytes = clientSocket.Receive(readIn);        string clientmessage = Encoding.ASCII.GetString(readIn);  clientmessage = clientmessage.Substring(0,bytes);  int posHost = clientmessage.IndexOf("Host:");  int posEndOfLine = clientmessage.IndexOf("\r\n",posHost);  clientmessage =     clientmessage.Remove(posHost,posEndOfLine-posHost);  clientmessage =     clientmessage.Insert(posHost,"Host: "+ sURL);  readIn = Encoding.ASCII.GetBytes(clientmessage);  if(bytes == 0) return;  UserInterface.reportMessage("Connection from:" +     clientSocket.RemoteEndPoint + "\r\n");  UserInterface.reportMessage    ("Connecting to Site:" +  sURL + "\r\n");  relayTCP(sURL,80,clientmessage);  clientSocket.Close();}VB.NETPublic Sub run()  Dim sURL As String = UserInterface.getMirror()  Dim readIn() As Byte = New Byte(1024) {}  Dim bytes As Integer = clientSocket.Receive(readIn)  Dim clientmessage As String = _  Encoding.ASCII.GetString(readIn)  clientmessage = clientmessage.Substring(0, bytes)  Dim posHost As Integer = clientmessage.IndexOf("Host:")  Dim posEndOfLine As Integer = clientmessage.IndexOf _     (vbCrLf, posHost)  clientmessage = clientmessage.Remove(posHost, _      posEndOfLine - posHost)  clientmessage = clientmessage.Insert(posHost, _     "Host: " + sURL)  readIn = Encoding.ASCII.GetBytes(clientmessage)  If bytes = 0 Then Return27010.8Load balancing  UserInterface.reportMessage("Connection from:" + _     clientSocket.RemoteEndPoint.ToString())  UserInterface.reportMessage("Connecting to Site:" + sURL)  relayTCP(sURL, 80, clientmessage)  clientSocket.Close()End SubThe data transfer takes place on relayTCP. It opens a TCP connectionto the Web server on port 80 and then sends it the modiﬁed HTTP headersent from the user. Immediately after the data is sent, it goes into a loop,reading 256-byte chunks of data from the Web server and sending it backto the client. If at any point it encounters an error, or the data ﬂow comesto an end, the loop is broken and the function returns.C#public void relayTCP(string host,int port,string cmd){    byte[] szData;  byte[] RecvBytes = new byte[Byte.MaxValue];    Int32 bytes;            TcpClient TcpClientSocket = new TcpClient(host,port);  NetworkStream NetStrm = TcpClientSocket.GetStream();  szData =  System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray());  NetStrm.Write(szData,0,szData.Length);  while(true)        {    try    {         bytes = NetStrm.Read(RecvBytes, 0,RecvBytes.Length);             clientSocket.Send(RecvBytes,bytes,SocketFlags.None);      if (bytes<=0) break;    }    catch    {      UserInterface.reportMessage("Failed connect");      break;    }          }      }10.8Load balancing271Chapter 10VB.NETPublic Sub relayTCP(ByVal host As String, ByVal port _     As Integer, ByVal cmd As String)  Dim szData() As Byte  Dim RecvBytes() As Byte = New Byte(Byte.MaxValue) {}  Dim bytes As Int32  Dim TcpClientSocket As TcpClient = New TcpClient(host, port)  Dim NetStrm As NetworkStream = TcpClientSocket.GetStream()  szData = _     System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray())  NetStrm.Write(szData, 0, szData.Length)  While True    Try      bytes = NetStrm.Read(RecvBytes, 0, RecvBytes.Length)      clientSocket.Send(RecvBytes, bytes, SocketFlags.None)      If bytes <= 0 Then Exit While    Catch      UserInterface.reportMessage("Failed connect")      Exit While    End Try  End WhileEnd SubAs usual, some standard namespaces are added to the head of the code:C#using System.Net;using System.Net.Sockets;using System.Text;using System.IO;using System.Threading;VB.NETImports System.NetImports System.Net.SocketsImports System.TextImports System.IOImports System.ThreadingTo test the application, run it from Visual Studio .NET, and then open abrowser on http://localhost:8889; you will see that the Web site is loaded27210.9Conclusionfrom all three servers. In this case, data transfer consumes most of the site’sloading time, so there would be little performance gain, but it should serveas an example (Figure 10.2).  10.9ConclusionScalability problems generally only start appearing once a product hasrolled out into full-scale production. At this stage in the life cycle, makingmodiﬁcations to the software becomes a logistical nightmare. Any changesto the software will necessarily have to be backwards compatible with olderversions of the product. Many software packages now include an autoupdater, which accommo-dates postdeployment updates; however, the best solution is to address scal-Figure 10.2HTTP load-balancingapplication.10.9Conclusion273Chapter 10ability issues at the design phase, rather than ending up with a dozen versionsof your product and the server downtime caused by implementing updates.The next chapter deals with network performance, including techniquessuch as compression and multicast.This page intentionally left blank  275 11 Optimizing Bandwidth Utilization 11.1Introduction You can’t always expect your customer to have the same bandwidth as yourofﬁce LAN. Huge numbers of people still use modem connections, andsome use mobile GPRS devices with even lower connection speeds. These customers will only buy your software if it works at a speed that isat least usable and does not frustrate them. Online services with slow load-ing times will infuriate casual Web users and drive away potential custom-ers. Conversely, people will pay more for better performance. To give anexample, VNC ( www.realvnc.com ) is free, under general public license(GPL), whereas client licenses for Microsoft Terminal Services (MTS) arecertainly not free. Both pieces of software allow you to control anothercomputer remotely, but many people still opt for MTS. Why? Performance.MTS provides more ﬂuid control over the remote computer than VNCover the same bandwidth.This chapter is largely devoted to two different performance-enhancingtechniques. The ﬁrst section of the chapter covers a technology known as multicast , the ability to send one piece of data to more than one recipientsimultaneously. The second section deals with data compression anddecompression. This is the ability to convert a block of data into a smallerblock of data and then return this to either an exact or near copy of theoriginal data.  11.2Tricks and tips to increase performance Performance increases can often be made by simple changes to how data ismoved between client and server. In some cases, these techniques may not 276 11.2 Tricks and tips to increase performance be applicable; however when used correctly, each of the following methodswill help keep your data moving quickly.  11.2.1Caching Caching can increase network performance by storing frequently accessedstatic data in a location that provides faster data return than the normalaccess time for the static data. It is important that all three of the followingcriteria are met: (cid:1) The data must be frequently accessed . There is no point in storing largedatasets in memory or on disk when only one client will ever requestit, once.  (cid:1) The data must not change as often as it is requested . The data shouldremain static for long periods, or else clients will receive outdateddata. (cid:1) The access time for cached data must be substantially faster than theaccess time to receive the data directly . It would defeat the purpose if aclient were denied access to the data from its source and instead wasredirected to a caching server that had to reprocess the data.Data can be cached at any point between the client and server.  Server-side caches  can protect against out-of-date data, but they are slower than cli-ent-side caches.  Client caches  are very fast because the data is read from disk,not the network, but they are prone to out-of-date data.  Proxy caches  are acombination of the two. They can refresh their cache regularly when idleand can serve data faster because they will be on a local connection to theclient. Old data on a proxy can be frustrating for a user because it is awk-ward to ﬂush the cache of a proxy server manually.Server caching can be extremely useful when data on the server needs tobe processed before it can be sent to clients. A prime example of this is thatwhen an ASP.NET page is uploaded to a server, it must be compiled beforegenerating content that is sent to the client. It is extremely wasteful to havethe server recompile the page every time it is requested, so the compiledversion is held in a server-side cache. When a site consists of mainly static content, it is possible to cache acompressed version of each of the pages to be delivered because mostbrowsers can dynamically decompress content in the right format. There- 11.2 Tricks and tips to increase performance277Chapter 11 fore, instead of sending the original version of each page, a compressed ver-sion could be sent. When the content is dynamic, it is possible to utilize on-the-ﬂy compression from server-accelerator products such as Xcache andPipeboost. Caching introduces the problem of change monitoring, so that thecached data reﬂects the live data as accurately as possible. Where the data isin the form of ﬁles on disk, one of the simplest mechanisms is to comparethe “date modiﬁed” ﬁeld against the cached data. Above that, hashing couldbe used to monitor changes within datasets or other content.Within the environment of a single Web site or application, caching canbe controlled and predicted quite easily, except when the content to beserved could come from arbitrary sources. This situation might arise in ageneric caching proxy server, where content could come from anywhere onthe Internet. In this case, the proxy must make an educated assessmentabout whether pages should be cached locally or not. The proxy would need to hold an internal table, which could record allrequests made to it from clients. The proxy would need to store the fullHTTP request because many sites behave differently depending on whatcookies and so forth are sent by the client. Along with the requests, theproxy would need to be able to count the number of identical requests andhow recently they were made. The proxy should also keep checksums (orhashes) of the data returned from the server relative to each request. Withthis information, the proxy can determine if the content is too dynamic tocache. With that said, even the most static and frequently accessed siteschange sometimes. The proxy could, during lull periods, check some of thecurrently cached Web sites against the live versions and update the cacheaccordingly.  11.2.2Keep-alive connections Even though most Web pages contain many different images that all comefrom the same server, some older (HTTP 1.0) clients create new HTTPconnections for each of the images. This is wasteful because the ﬁrst HTTPconnection is sufﬁcient to send all of the images. Luckily, most browsersand servers are capable of handling HTTP 1.1 persistent connections. A cli-ent can request that a server keep a TCP connection open by specifying Connection: Keep-Alive  in the HTTP header.  Netscape pioneered a technology that could send many disparate formsof data through the same HTTP connection. This system was called “serverpush” and could provide for simple video streaming in the days before Win- 278 11.2 Tricks and tips to increase performance dows media. Server push was never adopted by Microsoft, and unfortu-nately it is not supported by Internet Explorer, but it is still available inNetscape Navigator.When a TCP connection opens and closes, several handshake packetsare sent back and forth between the client and server, which can waste up toone second per connection for modem users. If you are developing a propri-etary protocol that involves multiple sequential requests and responsesbetween client and server, you should always aim to keep the TCP connec-tion open for as long as possible, rather than repeatedly opening and closingit with every request. The whole handshake latency issue can be avoided completely by usinga non-connection-oriented protocol such as UDP. As mentioned in Chap-ter 3, however, data integrity is endangered when transmitted over UDP.Some protocols such as real-time streaming protocol (RTSP, deﬁned inRFC 2326) use a combination of TCP and UDP to achieve a compromisebetween speed and reliability. 11.2.3Progressive downloads When most of a ﬁle is downloaded, the client should be able to begin to usethe data. The obvious applications are audio and video, where users canbegin to see and hear the video clip before it is fully downloaded. The sametechnique is applicable in many scenarios. For instance, if product listingsare being displayed as they are retrieved, a user could interrupt the processonce the desired product is shown and proceed with the purchase. Image formats such as JPEG and GIF come in a progressive version,which renders them as full-size images very soon after the ﬁrst few hundredbytes are received. Subsequent bytes form a more distinct and higher-qual-ity image. This technique is known as  interlacing . Its equivalent in an onlinecatalog application would be where product names and prices downloadﬁrst, followed by the images of the various products. 11.2.4Tweaking settings Windows is optimized by default for use on Ethernets, so where a produc-tion application is being rolled out to a client base using modems, ISDN,or DSL, some system tweaking can be done to help Windows manage theconnection more efﬁciently and, ultimately, to increase overall network per-formance. Because these settings are systemwide, however, these changes 11.2 Tricks and tips to increase performance279Chapter 11 should only be applied when the end-customer has given your software per-mission to do so.The TCP/IP settings are held in the registry at  HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters Under this location, various parameters can be seen, such as default nameservers and gateways, which would otherwise be inaccessible programmati-cally. Not all of these parameters would already be present in the registry bydefault, but they could be added when required.The ﬁrst system tweak is the TCP window size, which can be set at thefollowing registry location:  HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\ GlobalMaxTcpWindowSize The TCP window speciﬁes the number of bytes that a sending computercan transmit without receiving an ACK. The recommended value is256,960. Other values to try are 372,300, 186,880, 93,440, 64,240, and32,120. The valid range is from the maximum segment size (MSS) to 2 30 .For best results, the size has to be a multiple of MSS lower than 65,535times a scale factor that’s a power of 2. The MSS is generally roughly equalto the maximum transmission unit (MTU), as described later. This tweakreduces protocol overhead by eliminating part of the safety net and trim-ming some of the time involved in the turnaround of an ACK. TcpWindowSize  can also exist under  \Parameters\Interface\ . If thesetting is added at this location, it overrides the global setting. When thewindow size is less than 64K, the  Tcp1323Opts  setting should be applied asdetailed below: HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Tcp1323Opts “Tcp1323” refers to RFC 1323, a proposal to add timestamps to pack-ets to aid out-of-order deliveries. Removing timestamps shaves off 12 bytesper TCP/IP packet, but reduces reliability over bad connections. It alsoaffects TCP window scaling, as mentioned above. Zero is the recommendedoption for higher performance. Set the size to one to include window-scal- 280 11.2 Tricks and tips to increase performance ing features and three to apply the timestamp. This setting is particularlyrisky and should not be tampered with without great care.The issue of packets with a time-to-live (TTL) value is discussed againin the multicast section in this chapter, where it is of particular importance.The setting can be applied on a systemwide level at this registry location: HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DefaultTTL The TTL of a packet is a measure of how many routers a packet will travelthrough before being discarded. An excessively high TTL (e.g., 255) willcause delays, especially over bad links. A low TTL will cause some packetsto be discarded before they reach their destination. The recommendedvalue is 64. The MTU is the maximum size of any packet sent over the wire. If it isset too high, lost packets will take longer to retransmit and may get frag-mented. If the MTU is set too low, data becomes swamped with overheadand takes longer to send. Ethernet connections use a default of 1,500 bytesper packet; ADSL uses 1,492 bytes per packet; and FDDI uses 8,000 bytesper packet. The MTU value can be left as the default or can be negotiatedat startup. The registry key in question is HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\EnablePMTUDiscovery The recommended value is one.This will make the computer negotiate withthe NIC miniport driver for the best value for MTU on initial transmission.This may cause a slow startup effect, but it will ultimately be beneﬁcial ifthere should be little packet loss and the data being transferred is large. Ideally, every piece of datagram being sent should be the size of theMTU. If it is any larger than the MTU, the datagram will fragment, whichtakes computing time and increases the risk of datagram loss. This setting ishighly recommended for modem users:  HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\EnablePMTUBHDetect The recommended setting is zero. Setting this parameter to one ( True )enables “black hole” routers to be detected; however, it also increases the 11.2 Tricks and tips to increase performance281Chapter 11 maximum number of retransmissions for a given TCP data segment. Ablack hole router is one that fails to deliver packets and does not report thefailure to the sender with an ICMP message. If black hole routers are not anissue on the network, they can be ignored. HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\SackOpts The recommended setting is one. This enables Selective Acknowledgement(SACK) to take place, which can improve performance where window sizesare low.  HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxDupAcks The recommended value is two. The parameter determines the numberof duplicate acknowledgments that must be received for the same sequencenumber of sent data before “fast retransmit” is triggered to resend the seg-ment that has been dropped in transit. This setting is of particular impor-tance on links where a high potential for packet loss exists. Moving outside the low-level TCP nuts and bolts, a setting can improvethe performance of outgoing HTTP connections. These settings can speedup activities such as Web browsing:  HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\"MaxConnectionsPerServer"=dword:00000020"MaxConnectionsPer1_0Server"=dword:00000020HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\"MaxConnectionsPerServer"=dword:00000020"MaxConnectionsPer1_0Server"=dword:00000020 This setting actually increases the number of concurrent outgoing con-nections that can be made from the same client to the one server. This is a(small) violation of the HTTP standard and can put undue strain on someWeb servers, but the bottom line is, if it makes your application run faster,who cares? 282 11.3 Multicast UDP 11.3Multicast UDP Multicasting  is where a message can travel to more than one destination atthe same time. This can provide signiﬁcant increases in efﬁciency wherethere is more than one recipient of the data being sent. It is ideally suited tonetworks where all clients and servers are on the same LAN, and it isroutable on the Internet, but is only supported by some service providers.The ﬁrst audio multicast took place in 1992, followed one year later bythe ﬁrst video multicast. Nowadays, multicast UDP is used in productssuch as Symantec Ghost to provide remote software installations on multi-ple hosts simultaneously. It is also used to broadcast video footage of popu-lar events over the Internet.  11.3.1Multicast basics From a programmer’s perspective, the difference between point-to-pointUDP and multicast UDP is minimal. In .NET, we use the  UDPClient object and call the  JoinMulticastGroup()  method, passing to it a multicastIP address. We can then send and receive packets using the same methodsas we would with a standard UDP connection.A multicast IP address is one that lies in the range 224.0.0.0 to239.255.255.255. Unfortunately, you can’t pick any multicast IP addressarbitrarily because there are some restrictions. The IANA controls multicastIP addresses, so you should consult RFC 3171 and the IANA Web site for adeﬁnitive list. Never use a multicast IP address that is already assigned to awell-known purpose, such as the following: (cid:1) 224.0.0.0 to 224.0.0.255:  The Local Network Control Block is non-routable and cannot travel over the Internet. These addresses havewell-known purposes (e.g., DHCP is on address 224.0.0.12). (cid:1) 224.0.1.0 to 224.0.1.255:  The Internetwork Control Block isroutable, but these addresses have special uses. Network time proto-col (NTP) is on address 224.0.1.1, and WINS is on address224.0.1.24. (cid:1) 239.0.0.0 to 239.255.255.255:  The scope-relative addresses are notroutable, but they have no special purpose and can be used freely forexperimental purposes. 11.3 Multicast UDP283Chapter 11 It is possible to request a globally unique multicast IP address from theIANA. Initially, you should use an experimental multicast address such as234.5.6.11 or obtain a leased multicast address from multicast addressdynamic client allocation protocol (MADCAP), as deﬁned in RFC 2730.If other people are using the same multicast address as you, you mayreceive stray packets that could corrupt the data you are trying to transmit.If you are broadcasting exclusively to a LAN, use a scope-relative address. When broadcasting on a WAN (but not the Internet), you can limit theTTL of the packet to less than 63. TTL prevents a packet from beingrouted indeﬁnitely. Every hop decreases the TTL by one. When the TTLreaches zero, the packet is discarded. This can conﬁne a packet to a geo-graphic area and also prevents multicast avalanches, which occur whenpackets are replicated exponentially and end up clogging routers all overthe Internet.   11.3.2Multicast routing Multicast UDP may be the ﬁrst non-P2P protocol to be accessible pro-grammatically, but there is nothing new in protocols that broadcast ratherthan going from A to B. Routing protocols such as RIP and OSPF do nothave set endpoints; rather, they percolate through networks in all directionsat once. In fact, it would be a paradox if a routing protocol needed to berouted from point to point. The technique is not limited to routing proto-cols (e.g., BOOTP [bootstrap] and ARP are other examples of nondirec-tional protocols).The biggest limitation of network broadcasts is that they generally onlywork within the same LAN and cannot be routed across the Internet. Multi-cast UDP goes partway toward solving this problem. It is true that not every-one can send or receive multicasts to or from the Internet. Multicast datadoes have a tendency to ﬂood networks, so not all service providers want tobe bombarded with unsolicited data. To enable service providers who doaccept multicast to communicate, the multicast backbone (MBONE) wasdeveloped. This links multicast-compatible providers together via point-to-point channels in non-multicast-compatible networks. It currently spansmore than 24 countries, mostly in academic networks.Multicast implies that data travels in all directions at once (ﬂoods), butin practice, it is not the UDP packets that ﬂood, but multicast routing pro-tocol packets that do this job for them. There are three multicast routingprotocols: distance vector multicast routing (DVMRP), multicast openshortest path ﬁrst (MOSPF), and protocol independent multicast (PIM). 284 11.3 Multicast UDP A subscriber to a multicast will issue an Internet group management proto-col (IGMP) packet to register its interest in receiving messages. This proto-col is also used to leave groups.There is no equivalent multicast TCP because of the constant one-to-one handshaking that is required. This causes some difﬁculties for applica-tion developers because data sent by UDP can be corrupted as a result ofpacket loss, duplication, and reordering. This problem can be counteractedby inserting headers in the data containing a sequence number, which theclient can reorganize or request a once-off TCP/IP transfer of the missingpacket from the server.Similarly, it is difﬁcult to implement public/private key security via mul-ticast because every client would have a different public key. The IETF isscheduled to publish a standard security mechanism over multicast(MSEC) to address this issue. 11.3.3Implementing multicast Before you can implement a multicast-enabled application, you shouldensure that your Internet connection supports multicast trafﬁc and is con-nected to the MBONE network.This example consists of two applications: a sender and a receiver. Westart with the implementation of the sender. Open a new project in VisualStudio .NET and add three textboxes:  tbMulticastGroup ,  tbPort , and tbMessage . You will also require a button named  btnSend .Click on the Send button, and add the following code: C# private void btnSend_Click(object sender, System.EventArgs e){  send(tbMulticastGroup.Text , int.Parse(tbPort.Text),  tbMessage.Text );} VB.NET Private  Sub btnSend_Click(ByVal sender As Object, _      ByVal e As System.EventArgs)  send(tbMulticastGroup.Text,Integer.Parse(tbPort.Text), _    tbMessage.Text)End Sub 11.3 Multicast UDP285Chapter 11 Multicast operation can be performed at both the socket level and  Udp-Client  level. To illustrate both techniques, the sender (client) will be imple-mented using sockets, whereas the receiver will be implemented using the UdpClient  object. Before sending or receiving from a multicast group, it isnecessary to join the group. This is done in the example below using thesocket option  AddMembership . In the same way as if the socket was operating in point-to-point (uni-cast) mode, the remote endpoint must be speciﬁed with both a port and anIP address. The IP address in this case must be valid and within the multi-cast range (224.0.0.0 to 239.255.255.255). The TTL speciﬁes how far thepacket can travel; in this case, it is set to the maximum, 255.The next step is to implement the  Send  function as follows: C# public void send(string mcastGroup, int port, string message) {      IPAddress ip=IPAddress.Parse(mcastGroup);          Socket s=new Socket(AddressFamily.InterNetwork,     SocketType.Dgram, ProtocolType.Udp);          s.SetSocketOption(SocketOptionLevel.IP,     SocketOptionName.AddMembership, new MulticastOption(ip));  s.SetSocketOption(SocketOptionLevel.IP,     SocketOptionName.MulticastTimeToLive, 255);        byte[] b;  b = Encoding.ASCII.GetBytes(message);  IPEndPoint ipep=new IPEndPoint(  IPAddress.Parse(mcastGroup), port);          s.Connect(ipep);          s.Send(b,b.Length,SocketFlags.None);          s.Close();      } VB.NET Public  Sub send(ByVal mcastGroup As String, _  ByVal port As Integer, ByVal message As String)      Dim ip As IPAddress = IPAddress.Parse(mcastGroup)  Dim s As Socket = New Socket(AddressFamily.InterNetwork, _  SocketType.Dgram, ProtocolType.Udp)  s.SetSocketOption(SocketOptionLevel.IP, _ 286 11.3 Multicast UDP   SocketOptionName.AddMembership, New MulticastOption(ip))  s.SetSocketOption(SocketOptionLevel.IP, _  SocketOptionName.MulticastTimeToLive, 255)  Dim b As Byte()  b = Encoding.ASCII.GetBytes(Message)  Dim ipep As IPEndPoint = New _  IPEndPoint(IPAddress.Parse(mcastGroup), port)  s.Connect(ipep)  s.Send(b, b.Length, SocketFlags.None)  s.Close()End Sub This code uses sockets rather than streams to send multicast data. Sev-eral parameters need to be applied to the newly created code in order for itto operate effectively in multicast mode. First, the protocol type is set toUDP with  ProtocolType.Udp  because this is the underlying protocol for allmulticast broadcasts.A socket option is then set such that the socket will request to join thespeciﬁed group. The option  SocketOptionName.AddMembership  indicatesthat the socket is attaching to a multicast group. The ﬁnal parameter is theTTL; in this case, the TTL is 255, which effectively means that thepacket(s) can travel anywhere in the world. The message, which is in string format, is converted to a byte array. Theendpoint is set to the multicast address on the port speciﬁed. The socketthen connects to the endpoint, sends the byte array, and then disconnects. To complete the program, add the required namespaces at the top of thecode: C# using System.Text;using System.Net;using System.Net.Sockets; VB.NET Imports System.TextImports System.NetImports System.Net.Sockets 11.3 Multicast UDP287Chapter 11 The next step is to code the multicast receiver. Open a new project inVisual Studio .NET and draw a textbox named  tbMessages  with  multi-line set to true on the form.C#private void Form1_Load(object sender, System.EventArgs e){  Thread thdReceiver = new Thread(new  ThreadStart(receiverThread));  thdReceiver.Start();}VB.NET Private  Sub Form1_Load(ByVal sender As Object, _    ByVal e As System.EventArgs)  Dim thdReceiver As Thread   thdReceiver = New Thread(New ThreadStart _    (AddressOf receiverThread))   thdReceiver.Start()End SubThe receiving thread will remain in an inﬁnite loop awaiting new data.It is therefore run in a separate thread named recieverThread(). In this case, the multicast functionality is implemented using theUdpClient object. Membership to the group is obtained by calling theJoinMulticastGroup. Again the TTL and port details must be speciﬁed. Enter the following code to ﬁnish this application: C#public void receiverThread(){  UdpClient client = new UdpClient(5000);  IPAddress group = IPAddress.Parse("224.5.4.6");  int timeToLive = 255;  int port = 5000;  client.JoinMulticastGroup(group, timeToLive);  IPEndPoint remoteEP = new IPEndPoint(group, port);  while (true)  {    IPEndPoint ep = null;28811.3Multicast UDP    byte[] buffer = client.Receive(ref ep);    string message = Encoding.ASCII.GetString(buffer);        this.tbMessages.Text += message + "\n";  }      }VB.NETPublic  Sub receiverThread()  Dim client As UdpClient =  New UdpClient(5000)   Dim group As IPAddress =  IPAddress.Parse("224.5.4.6")   Dim timeToLive As Integer =  255   Dim port As Integer =  5000   client.JoinMulticastGroup(group, timeToLive)  Dim remoteEP As IPEndPoint =  New IPEndPoint(group,port)   Do    Dim ep As IPEndPoint =  Nothing     Dim buffer() As Byte =  client.Receive( ep)     Dim message as String = _    System.Text.Encoding.ASCII.GetString(buffer)     Me.tbMessages.Text += message + vbcrlf  LoopEnd SubThis code uses a higher level of abstraction than the sender and imple-ments a multicast receiver using UdpClient objects rather than bare sockets.In much the same way as you would receive standard UDP packets, theUdpClient is set to listen on a speciﬁed port (in this case, 5000) by passingthe port number to the constructor. Where it differs is when JoinMulti-castGroup is called. This method is passed an IPAddress object that holdsthe multicast IP address and the TTL value for any packets sent. The pro-gram goes into an inﬁnite loop at this point, receiving arrays of bytes fromwhomever happens also to be transmitting on that multicast IP address.These byte arrays are then converted into strings and displayed on-screen. To ﬁnish this code, add the required namespaces as follows:C#using System.Threading;using System.Net;using System.Net.Sockets;using System.Text;11.4Data compression289Chapter 11VB.NETImports System.ThreadingImports System.NetImports System.Net.SocketsImports System.TextTo test this application, run both the sender and receiver from VisualStudio .NET. Set the group address on the sender to 224.5.6.7 and the portto 5000, type in a short message, and press send. You will see the textappearing in the receiver application (Figure 11.1). It should be possible toopen multiple instances of the receiver application and have them all receivethe same text simultaneously. 11.4Data compressionThe most effective way to send data between computers faster is to sendless data. This does not mean that you send the recipient less information,just that it is packaged in a more compact way. The process of compressingdata so that the decompressed data is identical to the original is known aslossless compression and is used in ZIP compression. The process of com-pressing data in a way that is not identical, but is not perceived as differentFigure 11.1Multicast UDPclient and server.29011.5Lossless compressionfrom the original, is known as lossy compression and is used in JPEG andMp3 compression.11.5Lossless compressionLossless compression is used when the integrity of data is paramount. In thesame way that it saves space to round the company’s annual returns to thenearest million, there may be a risk that someone could run off with$499,999 without affecting the books. There are two ways of compressing data without losing integrity:entropy encoding and source encoding. Entropy encoding is where the statis-tical similarity between bytes or byte sequences is recorded, rather than thebytes themselves. Source encoding is where the rate of change between bytesor byte sequences is recorded and not the bytes themselves. Entropy encod-ing is used in the ZIP format, whereas source encoding is used in adaptivedelta pulse code modulation (ADPCM), an audio compression technique.The most basic form of entropy encoding is run length encoding (RLE),where a byte sequence consisting entirely of the same byte is converted intoa number followed by the byte. Therefore, the sequence (in hex) 00 00 0000 00 could be shortened to 05 00. This approach achieves compressiononly on ﬁles with very high entropy, but it was used effectively in the ratheroutdated PCX format. A more effective component of ZIP compression is Huffman compres-sion, where the most common bytes are encoded into short bit sequences.The less common bytes are encoded into bit sequences longer than a byte,but because they are less common, the overall effect is a shorter ﬁle. A table of bit-code-to-byte conversions is known as a codebook, whichcan be either static or dynamic. Because the codebook adds to the totallength of the transmitted ﬁle, it is advantageous to have a short codebook orno codebook with a static codebook. There is no need to transmit the code-book with the data because the receiver will already have it.Static codebooks have been around for years, in fact, since well beforethe time of computers. The ﬁrst data compression scheme was Morse code.The designers of Morse code may not have had entropy reduction in mind,but they did happen to choose the shortest codes for the most common let-ters. E and T are encoded as a single dot and dash, whereas Z is encoded asa four “bit” sequence. Morse code is not applicable for computer data com-pression because it uses a pause as a delimiter, which cannot be representedin binary.11.5Lossless compression291Chapter 11Dynamic codebooks are built up during compression, which is wherethe most common characters are ascertained and then assigned bitsequences. The codebook is then used to compress the data bytes intoshorter bit sequences, which are joined together and padded to form a bytestream that should be smaller than the original data.Codebooks cannot be built up arbitrarily. They must reﬂect the fre-quency of each character in the data and be easily delimitable. The simplestscheme is to assign a two-bit sequence to the most common character (i.e.,01). Each byte that follows this character frequencywise is represented byeither an additional 1 or 00.In English text, the most common character is a space, followed by eand then “t.” Therefore, a space can be represented as 01, “e” as 011, and “t”as 0100. Using this method, the sequence “e   et” (6 bytes) can be repre-sented as 01101010 10110100 (2 bytes). The process of building up aHuffman codebook (or “tree”) is not processor intensive, and it is possibleto implement in real time to provide higher effective bandwidth to clients.11.5.1Implementing ZIP compressionIt is not necessary to reinvent the wheel when it comes to ZIP compression.Many third-party controls are available for download on the Internet. Someof these are under GPL and, thus, can be redistributed in binary (closed-source) form, once the license terms, as speciﬁed on the publisher’s Website, are adhered to. A good implementation of ZIP in .NET is the #ZipLibfrom www.icsharpcode.net. The following example demonstrates how tocompress a ﬁle using #ZipLib, so it is worthwhile to download it from theirWeb site. Where using third-party code is not an option, the ofﬁcial reference forthe ZIP format is located in RFC 1950 through RFC 1952.Create a new project in Visual Studio .NET, click Projects→→→→Add Refer-ences→→→→Browse, and then select SharpZipLib.dll from the folder where#ZipLib was installed. Draw two textboxes named tbInput and tbOutputon the form with two corresponding buttons, btnBrowseInput and btn-BrowseOutput. The two browse buttons should have corresponding FileOpen and File Save Dialog controls, OpenFileDialog and SaveFileDialog,respectively. Finally, a button named btnCompress is also required.The ﬁrst step is to tie the File Open and File Save dialog boxes to thebuttons, to make it easier for users to select the relevant ﬁles. Click on theBrowse button opposite the Input textbox and enter the following code:29211.5Lossless compressionC#private void btnBrowseInput_Click(object sender, System.EventArgs e){  openFileDialog.ShowDialog();  tbInput.Text = openFileDialog.FileName;}VB.NETPrivate  Sub btnBrowseInput_Click(ByVal sender As Object, _    ByVal e As System.EventArgs)  openFileDialog.ShowDialog()  tbInput.Text = openFileDialog.FileNameEnd SubClick on the Browse button opposite the Output textbox and enter thefollowing code:C#private void btnBrowseOutput_Click(object sender, System.EventArgs e){  saveFileDialog.ShowDialog();  tbOutput.Text = saveFileDialog.FileName;}VB.NETPrivate  Sub btnBrowseOutput_Click(ByVal sender As Object, _    ByVal e As System.EventArgs)  saveFileDialog.ShowDialog()  tbOutput.Text = saveFileDialog.FileNameEnd SubThe workhorse of the application is contained behind the Compressbutton. ZIP ﬁles can contain more than one source ﬁle and retain CRC anddate information with each ﬁle to help maintain integrity. The ZipOutput-Stream is appended to using ZipEntry objects. Each entry contains theoriginal ﬁle data, along with a CRC for that ﬁle and a date. 11.5Lossless compression293Chapter 11Note: Checksums (or CRCs) are similar to hash values, although they areused for integrity checks rather than security against data tampering.The SetLevel method is used to deﬁne the strength of compression, wherezero is no compression and nine is maximum compression. There is a smallperformance difference between the compression levels, but in most cases,it should be set to maximum. Click on the Compress button and enter the following code:C#private void btnCompress_Click(object sender, System.EventArgs e){            Crc32 crc = new Crc32();  ZipOutputStream ZipStream =     new ZipOutputStream(File.Create(tbOutput.Text));  ZipStream.SetLevel(9);       string file = tbInput.Text;  FileStream fs = File.OpenRead(file);  byte[] buffer = new byte[fs.Length];  fs.Read(buffer, 0, buffer.Length);  ZipEntry entry = new ZipEntry(file);  entry.DateTime = DateTime.Now;  entry.Size = fs.Length;  fs.Close();  crc.Reset();  crc.Update(buffer);  entry.Crc  = crc.Value;  ZipStream.PutNextEntry(entry);        ZipStream.Write(buffer, 0, buffer.Length);      ZipStream.Finish();  ZipStream.Close();}VB.NETPrivate  Sub btnCompress_Click(ByVal sender As Object, _ ByVal e As System.EventArgs)                Dim crc As Crc32 = New Crc32        Dim ZipStream As ZipOutputStream = _29411.5Lossless compression           New ZipOutputStream( _           System.IO.File.Create(tbOutput.Text))        ZipStream.SetLevel(9)        Dim file As String = tbInput.Text        Dim fs As FileStream = System.IO.File.OpenRead(file)        Dim buffer() As Byte = New Byte(fs.Length) {}        fs.Read(buffer, 0, buffer.Length)        Dim enTry As ZipEnTry = New ZipEnTry(file)        enTry.DateTime = DateTime.Now        enTry.Size = fs.Length + 1        fs.Close()        crc.Reset()        crc.Update(buffer)        enTry.Crc = crc.Value        ZipStream.PutNextEnTry(enTry)        ZipStream.Write(buffer, 0, buffer.Length)        ZipStream.Finish()        ZipStream.Close()End SubZIP ﬁles consist of multiple entries, one entry for each ﬁle. Each entryhas an associated CRC value, which is analogous to a hash value in provingintegrity checks for ﬁles that could have been corrupted in transit. Creatinga ZIP ﬁle takes three steps: (1) creating a zip stream, (2) deﬁning the variousentries, and (3) calculating the CRC values for each entry.The zip stream is created with a constructor that is passed the ﬁnal desti-nation of the .zip ﬁle. The compression level is also set at this point: level 1is fast, but offers little compression, whereas level 9 is slower, but offers bet-ter compression ratios.The second step is to create an entry for the ﬁle that is to be compressed.Here a new ZipEntry object is instantiated. The constructor of this object ispassed the ﬁlename and the path for the ﬁle to be compressed. The ﬁle’sdate and length are included in the entry. This entry is appended to thestream using the PutNextEntry method. Every entry must have a corresponding CRC value. This value is calcu-lated by ﬁrst reading in the contents of the ﬁle and then passing the result-ant byte array to the Update method of a Crc32 object. The CRC value isstored in the crc property of the entry.  11.5Lossless compression295Chapter 11The ZIP ﬁle is written to disk, one entry at a time, by passing the con-tents of the uncompressed ﬁle to the Write method of the zip stream.Finally, the stream is ﬂushed with the Finish command and then closed. Add the following assemblies at the top of the code: C#using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.GZip;using System.IO;VB.NETImports ICSharpCode.SharpZipLib.ChecksumsImports ICSharpCode.SharpZipLib.ZipImports ICSharpCode.SharpZipLib.GZipImports System.IOTo test the application, run it from Visual Studio .NET. Press theBrowse button beside the Input textbox and select a text ﬁle from yourcomputer. Press the second Browse button and enter a ﬁlename with theextension .zip. Press Compress, and then locate the newly created ZIP ﬁle.You should notice a reduction in ﬁle size (Figure 11.2).Figure 11.2ZIP datacompressionapplication.29611.6Lossy compressionExamples of decompressing ZIP ﬁles and more advanced uses of thiscontrol may be found at the www.icSharpcode.net Web site. Interested read-ers should refer to this site for more information. In Chapter 4, a certainHTTP header may have particular relevance to developers working onWeb-based applications that have browsers as clients. Speciﬁcally:Accept-Encoding: gzip, deflateAs the name suggests, browsers can accept compressed data as well asplain text. Therefore, it is possible to improve the performance of a Webserver by compressing its output, either on the ﬂy or in cache compression. The gzip and deflate compression algorithms are contained within the#ZipLib control, so furnishing this format to clients is easy. All that isrequired in addition to the compression aspect is that Content-Encoding isadded to the header in the HTTP reply. An open-source implementationof this was developed by Ben Lowery and can be found at: www.blowery.org/stories/2002/12/12/httpcompressionmodule.html. 11.6Lossy compressionIn cases where data integrity is not as important, but good compression isimperative, lossy compression is a good option. This is particularly perti-nent to audio and visual data, where users will put up with a little mufﬂingor blurring, as long as they see or hear what they want without having towait too long.11.6.1Audio compressionAn audio ﬁle has very little byte-to-byte entropy, and compression schemessuch as ZIP or Huffman will have little effect on the ﬁle size; however, ifyou open an audio ﬁle in a wave editor, such as Goldwave (www.gold-wave.com), you will notice a deﬁnite pattern when you look closely at thedata in Figure 11.3.The screenshot is of a recording of a girl’s voice. It contains only a frac-tion of a second (0.026 sec) of audio, but contains more than 2 Kb of data.To achieve CD-quality audio, a computer must output data at 44,100 (× 2)bytes per second.Audio is made up of waves. Each sample in a wave is usually very similarto the preceding sample. The rate of change constantly increases and11.6Lossy compression297Chapter 11decreases in harmonic fashion. Therefore, instead of recoding the value ofeach sample, if the change between samples is recorded, then the amount ofdata is reduced.In delta pulse code modulation (DPCM), an increase in sample value isrepresented by the bit 1 and a decrease is represented by the bit 0. Duringdecompression, the sample value is incremented or decremented by 1,depending on the value of the current bit in the bitstream. This causes twodetrimental effects: slope overload and granular noise. Slope overload iswhere the input signal changes substantially from sample to sample, result-ing in a mufﬂing effect in the decompressed signal. Granular noise is wherethe input signal does not change at all, in which case the output soundoscillates around the true value, which causes either a hiss or a high-pitchedshrill in the audio.To counteract the mufﬂing effect, adaptive DPCM, or ADPCM, can beused. This is where, during the decompression process, a number that dou-bles with each contiguous sample increases the sample value. This processmore closely mimics the harmonic action of the sine wave, but can producea phase undershoot, which is a rasping, sharp noise. You may never have heard of ADPCM (although it is used heavily intelecommunications and especially on international telephone lines), butMp3 has become almost a household name. There is a good reason for this,in that Mp3 provides excellent compression ratios and acceptable soundquality and can be decompressed in real time by any PC and many portabledigital music devices. Mp3 achieves this quality by recognizing how humansperceive sounds at an acoustic level. Our ears are designed to hear harmonicsounds, and standard lossy compression algorithms cause unnatural attenua-tions that are not pleasant to listen to. By ﬁltering at the harmonic level,rather than at the byte level, a much more natural sound is produced.Figure 11.3Typical speechwaveform.29811.6Lossy compressionRecognizing a pattern of cyclic values in a stream of data, which may becombined with thousands of other cyclic patterns, is not an easy task for acomputer; however, a rather gifted mathematician developed a formula toproduce a mathematical representation of the harmonics contained in ablock of data. In Figure 11.3, a pattern of waves can be seen in the audio; these aremade primarily from a 300 Hz with a 2400 Hz harmonic. To extract thisinformation from what appears to the computer as a block of ones andzeros, you need to use a mathematical formula known as the DiscreteCosine Transform (DCT):Cu is equal to 0.7071 (the reciprocal of root 2); when u is zero, Cu isone, for all u not equal to zero. When the above formula is applied to an array of eight numbers (i.e.,f(1) to f(7)), the resultant array in S is a representation of the data in termsof frequencies. It is possible to represent any sequence of eight integers interms of the values of the peaks and troughs of a wave composed of up toeight harmonics. When compressing audio data, most of the higher har-monics are zero or near zero and can be canceled out; thus the array in Scan be compressed using traditional lossless encoding more efﬁciently.The most famous audio compression format that uses DCT is the ubiq-uitous Mp3. This technology is not an easy implementation, and its exactformat is a closely guarded secret. You can use third-party DLLs and appli-cations such as Lame, BladeEnc, and L3enc to perform the compression.Alternately, you can license the technology from Fraunhoffer.11.6.2Image compressionImage compression is remarkably similar to audio compression, except thatit works in two dimensions rather than one. There may not be the sameobvious wave pattern in images, but in digital photographs the natural dith-ering in shades of color compresses very well when DCT/Huffman com-pression is applied. During the JPEG compression process, the image is split into macrob-locks, or 8×8 blocks of pixels. Each macroblock is then compressed usinga two-dimensional DCT to isolate and reduce the number of color har-S8u()Cu2-------=fx()2x1+()πu16---------------------------cosx0=7∑⋅11.6Lossy compression299Chapter 11monics within each area of the picture. The idea of waves existing withinan image may seem alien, but they exist everywhere in natural textures.The two-dimensional DCT can be expressed mathematically as follows:Cu is equal to 0.7071 (the reciprocal of root 2); when u is zero, Cu isone for all u not equal to zero. The same applies to Cv.This formula produces a two-dimensional array, which can be com-pressed by rounding the near-zero values of the array to zero, then usingRLE compression followed by Huffman compression.Luckily, you will probably never have to implement JPEG compressionfrom scratch. .NET has native support for JPEG, along with plenty of otherimage formats, including PNG, TIFF, and GIF. The following sample pro-gram shows you how to compress a bitmap image into a JPEG.Start a new project in Visual Studio .NET. Draw a picture box, namedpictureBox onto the form. Draw two textboxes named tbInput andtbOutput on the form, with two corresponding buttons, btnBrowseInputand btnBrowseOutput. The two browse buttons should have correspondingFile Open and File Save Dialog controls, named openFileDialog andsaveFileDialog, respectively. Finally, a button named btnCompress is alsorequired.The ﬁrst step is to tie the File Open and File Save dialog boxes to thebuttons to make it easier for users to select the relevant ﬁles. The open ﬁleprocedure will also load the new image into the picture box. Click on the Browse button opposite the Input textbox and enter thefollowing code:C#private void btnBrowseInput_Click(object sender, System.EventArgs e){  openFileDialog.ShowDialog();  tbInput.Text = openFileDialog.FileName;  pictureBox.Image= Image.FromFile(openFileDialog.FileName);  }Svu(,)Cv2-------Cu2-------fyx,()2x1+()πu16---------------------------2y1+()πv16--------------------------coscosx0=7∑y0=7∑=30011.6Lossy compressionVB.NETPrivate  Sub btnBrowseInput_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  openFileDialog.ShowDialog()  tbInput.Text = openFileDialog.FileName  pictureBox.Image= Image.FromFile(openFileDialog.FileName)End SubClick on the Browse button opposite the Output textbox and enter thefollowing code:C#private void btnBrowseOutput_Click(object sender, System.EventArgs e){  saveFileDialog.ShowDialog();  tbOutput.Text = saveFileDialog.FileName;}VB.NETPrivate  Sub btnBrowseOutput_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  saveFileDialog.ShowDialog()  tbOutput.Text = saveFileDialog.FileNameEnd SubTo save a JPEG from a loaded image, you may simply call the Savemethod. The method requires the image format and a stream as inputparameters: C#private void btnCompress_Click(object sender, System.EventArgs e){  FileStream fs = new  FileStream(tbOutput.Text,FileMode.CreateNew);  PictureBox.Image.Save(fs,  System.Drawing.Imaging.ImageFormat.Jpeg);        fs.Close();  }11.6Lossy compression301Chapter 11VB.NETPrivate  Sub btnCompress_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  Dim fs As FileStream = New FileStream(tbOutput.Text, _  FileMode.CreateNew)  PictureBox.Image.Save(fs, _  System.Drawing.Imaging.ImageFormat.Jpeg)  fs.Close()End SubTo test this application, run it from Visual Studio .NET, press theBrowse button next to the Input textbox, and choose a bitmap from yourcomputer. Click the Browse button next to the Output textbox, and select alocation to save the JPEG. Press Compress, and then locate the new savedJPEG on your computer; you should notice that it will have a smaller ﬁlesize (Figure 11.4).Figure 11.4JPEG Compressionapplication.30211.6Lossy compressionYou may notice that it is possible to take a JPEG ﬁle as an input to thisprogram. Although the application will allow you to do this, the end resultwill be a JPEG ﬁle of even lower quality than the original. 11.6.3Video compressionWith a healthy percentage of consumers using broadband technology intheir homes, it will soon be possible to deliver video on demand to the aver-age user. Without compression, the bandwidths required would be phe-nomenal. Various standards have been developed to compress video datainto narrower channels, the most successful of which is Motion PicturesExpert Group (MPEG). MPEG encoders are not cheap, but they do pro-vide the best compression of any other format. A cheap alternative to MPEG is the audio-video interleaved (AVI) for-mat. This is a technology built in to the Windows API. It has nowhere nearthe same compressing capabilities, but it saves time in developing a propri-etary format. A good resource for creating AVI ﬁles programmatically iswww.shrinkwrapvb.com. The code examples are in Visual Basic 6.0, but theycan be ported to VB.NET from within Visual Studio .NET. If a proprietary format is the only option, then examining the operationof MPEG or MJPEG may help. Video compression is similar to audio com-pression, except there are three data channels for imaging and one channelfor audio. Every pixel is made from a combination of three colors: red,green, and blue (RGB format).  One important compression technique for motion pictures is subsam-pling. Subsampling is a technique employed by MPEG and JPEG, at thestart of the encoding process. In this process, the ﬁrst step is to convert theRGB format into the YUV format. The YUV format deﬁnes each color interms of luminance and chrominance. Chrominance deﬁnes the color, fromred to blue. Luminance deﬁnes the saturation, or greyness. Because lumi-nance changes more often than chrominance, less color data can be sent.The rationalization for this is that a red car may turn dark red when travel-ing under a shadow, but it would rarely turn blue spontaneously.When this phenomenon is applied to motion picture compression,chrominance levels are updated every frame, whereas the saturation levelsare updated only every few frames. In the H.261 standard, the ratio ofchrominance to luminance sampling is 4:1. The most novel part of MPEG encoding is the motion-estimation algo-rithm. This is where the image is split into macroblocks, or 8×8 blocks of11.7Conclusion303Chapter 11pixels. These blocks are compared for similarity with blocks in previous orfuture frames. Because most images do not change signiﬁcantly betweenframes, this is an effective compression technique, albeit processor intensiveon the encoder. Therefore, MPEG compression can rarely be done on-the-ﬂy and must be pre-encoded before serving. MPEG decompression is onthe order of 10 times faster than compression and can be performed as datais being received. The ﬁnal stage in MPEG compression is where each macroblock is com-pressed using JPEG image compression.11.7ConclusionThis chapter has dealt with the problem of furnishing clients with moredata in less time. In many cases, this involves a trade-off in data integrity,timeliness, or quality; however, this trade-off is often entirely justiﬁed. Agood percentage of people can tell the difference between CD-quality andMp3 audio, but when given a choice to download a 20-Mb wave ﬁle or theequivalent 2-Mb Mp3, very few people will value the quality differenceenough to wait 10 times longer to hear the music. A trade-off is not always a necessary side effect of techniques to sendmore data faster across the phone networks. Lossless data compression guar-antees the integrity of data, yet can compress high-entropy data, such asplain text or XML, to a mere fraction of its original size. As long as theserver and client have the processing power to compress and decompressthe data at a rate faster than the amount of time it would take to send thedata over the wire, then compression is an excellent means of acceleratingyour applications’ communications. The next chapter deals with network protocols that are not ordinarilyused to move data between computers, but that act as auxiliary protocols tohelp applications become more responsive and scalable. These protocols canbe used to determine if computers are connected to the network or if thereare alternative computers with which to communicate. An introduction toWindows Management Instrumentation (WMI) will demonstrate how toadminister computers remotely over a network. This page intentionally left blank  305 12 Ping, DNS, and WHOIS: Monitoring your Network 12.1Introduction Network protocols are not just used to move data from one point toanother. Some protocols have speciﬁc purposes that help keep Internet traf-ﬁc ﬂowing and make using the network easier. These utility protocols may not be required for every network applica-tion; however, because these are niche technologies, many developers maynot know how to implement such features. By leveraging these technolo-gies, it may be possible to add unique features to your products, which mayprovide that competitive advantage.This chapter is divided into roughly equal sections describing threeeveryday utility protocols: DNS, WHOIS, and Ping. The chapter con-cludes with a discussion of an interesting utility protocol developed byMicrosoft, named WMI. 12.2DNS DNS operates on UDP port 53 and is described in RFC 1010, RFC 1304,RFC 1035, and RFC 1183. As described in Chapter 1, the most commonuse for DNS is to convert domain names into IP addresses because peopleﬁnd it difﬁcult to remember strings of numbers more than nine digits long.DNS was developed to provide a system that converts easily recognizabledomain names into IP addresses. No central computer stores a list of domain names against IP addresses.Instead, a worldwide network of DNS servers holds this information. EveryWeb site would typically be listed on two DNS servers; these machines aresaid to be authoritative in the domain. DNS servers routinely query eachother for updated information, and in this way the information slowly 306 12.2 DNS propagates through the Internet. Therefore, if you change hosting providerson a Web site, it will take up to 48 hours for the new DNS information topropagate through the Internet.You can use  DNS.GetHostByName  to convert a domain name ( string ) toan IP address ( IPHostEntry ). The reverse of this action, converting an IPaddress to a domain name, can be achieved using  DNS.GetHostByAddress .There is more to DNS than converting IP addresses to domain names andvice versa, however. In fact, most DNS handling is behind the scenes, andmost high-level network programming would rarely need to know the IPaddress of the servers or clients with which it was communicating.An interesting facet of DNS is its role in sending and receiving emails.As mentioned in Chapter 5, SMTP servers discover the destination POP3servers using DNS mail exchange (MX). This is where a specially formattedDNS query is sent to a (any) DNS server, which returns a list of mail serv-ers associated with the speciﬁed domain in order of preference.This technique can be used to provide email address validation, aboveand beyond the simple checks for the @ symbol followed by a period. Itcould also be used to simplify email software by skipping the need for end-users to enter SMTP server details. A ﬁnal advantage of this technique isthat it is much faster than relaying by SMTP, so it could improve the per-formance of email software. 12.2.1Implementing DNS MX Open a new project in Visual Studio .NET and draw three textboxesnamed  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: C# private void btnFind_Click(object sender, System.EventArgs e){  byte[] DNSQuery;  byte[] DNSReply;      UdpClient dnsClient = new UdpClient(tbDNSServer.Text , 53);   DNSQuery = makeQuery(DateTime.Now.Millisecond *  60,tbDomain.Text);    dnsClient.Send(DNSQuery,DNSQuery.GetLength(0));  IPEndPoint endpoint = null;        DNSReply = dnsClient.Receive(ref endpoint); 12.2 DNS307Chapter 12   this.tbStatus.Text = makeResponse(DNSReply,tbDomain.Text);} VB.NET Private  Sub btnFind_Click(ByVal sender As Object,_ ByVal e As System.EventArgs)  Dim DNSQuery() As Byte  Dim DNSReply() As Byte  Dim dnsClient As UdpClient = New _    UdpClient(tbDNSServer.Text, 53)  DNSQuery = makeQuery(DateTime.Now.Millisecond * 60, _    tbDomain.Text)  dnsClient.Send(DNSQuery, DNSQuery.GetLength(0))  Dim endpoint As IPEndPoint = Nothing  DNSReply = dnsClient.Receive(endpoint)  Me.tbStatus.Text = makeResponse(DNSReply, tbDomain.Text)End Sub This opens a UDP connection on port 43 to the DNS server and sendsan MX query to it. The response is then parsed and displayed by the  make-Response  function. To prepare the MX query, we need to write a new function. It involvesquite a bit of byte-by-byte writing, which we won’t discussed in too muchdetail here. Interested readers should consult the RFCs quoted at the startof this section.  C# public byte[] makeQuery(int id,string name) {        byte[] data = new byte[512];  byte[] Query;  data[0]   = (byte) (id >> 8);  data[1]  = (byte) (id & 0xFF );  data[2]  = (byte) 1; data[3] = (byte) 0;  data[4]  = (byte) 0; data[5] = (byte) 1;  data[6]  = (byte) 0; data[7] = (byte) 0;  data[8]  = (byte) 0; data[9] = (byte) 0;  data[10] = (byte) 0; data[11] = (byte) 0;  string[] tokens = name.Split(new char[] {'.'});  string label; 308 12.2 DNS   int position = 12;  for(int j=0; j<tokens.Length; j++)   {    label = tokens[j];    data[position++] = (byte) (label.Length & 0xFF);    byte[] b = System.Text.Encoding.ASCII.GetBytes(label);    for(int k=0; k < b.Length; k++)     {      data[position++] = b[k];    }  }  data[position++] = (byte) 0 ; data[position++] = (byte) 0;  data[position++] = (byte) 15; data[position++] = (byte) 0 ;  data[position++] = (byte) 1 ;  Query = new byte[position+1];  for (int i=0;i<=position;i++)  {    Query[i]= data[i];  }  return Query;} VB.NET Public Function makeQuery(id as Integer,name as _             String) as Byte()        Dim data() As Byte = New Byte(512) {}        Dim Query() As Byte        data(0) = CType((id >> 8), Byte)        data(1) = CType((id And &HFF), Byte)        data(2) = 1 : data(3) = 0        data(4) = 0 : data(5) = 1        data(6) = 0 : data(7) = 0        data(8) = 0 : data(9) = 0        data(10) = 0 : data(11) = 0        Dim tokens() As String = Name.Split(New Char() {"."})        Dim label As String        Dim position As Integer = 12        Dim j As Integer        For j = 0 To tokens.Length - 1            label = tokens(j)            data(position) = _ 12.2 DNS309Chapter 12             CType((label.Length And &HFF), Byte)            position = position + 1            Dim b() As Byte = _               System.Text.Encoding.ASCII.GetBytes(label)            Dim k As Integer            For k = 0 To b.Length - 1                data(position) = b(k)                position = position + 1            Next        Next        data(position) = 0        position = position + 1        data(position) = 0        position = position + 1        data(position) = 15        position = position + 1        data(position) = 0        position = position + 1        data(position) = 1        Query = New Byte(position + 1) {}        Dim i As Integer        For i = 0 To position            Query(i) = data(i)        Next        Return QueryEnd Function Domain names in DNS queries appear in a rather unusual format.Instead of periods separating each level (word) in the domain, a byte valuerepresenting the next part of the domain is used. This would mean that www.google.com  becomes  3www6google3com  (the numbers represent thebinary value and not the ASCII code for the number). For more informa-tion on this topic and the DNS format in general, please refer to the RFCslisted at the start of this chapter.The next step is to analyze the response, so type in the  makeResponse function as follows:  C# public string makeResponse(byte[] data,string name) {  int qCount = ((data[4] & 0xFF) << 8) | (data[5] & 0xFF); 310 12.2 DNS   int aCount = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);  int position=12;  for( int i=0;i<qCount; ++i)   {    name = "";    position = proc(position,data,ref name);    position += 4;  }  string Response ="";  for (int i = 0; i < aCount; ++i)   {    name = "";    position = proc(position,data,ref name);    position+=12;    name="";    position = proc(position,data,ref name);    Response += name + "\r\n";  }  return Response;} VB.NET Public Function makeResponse(ByVal data() As Byte, _      ByVal DomainName As String) As String  Dim qCount As Integer = ((data(4) And &HFF) << 8) Or _    (data(5) And &HFF)  Dim aCount As Integer = ((data(6) And &HFF) << 8) Or _    (data(7) And &HFF)  Dim position As Integer = 12  Dim i As Integer  For i = 0 To qCount - 1    DomainName = ""    position = proc(position, data, DomainName)    position += 4  Next  Dim Response As String = ""  For i = 0 To aCount - 1    DomainName = ""    position = proc(position, data, DomainName)    position += 12    DomainName = "" 12.2 DNS311Chapter 12     position = proc(position, data, DomainName)    Response += DomainName + vbCrLf  Next  Return ResponseEnd Function The preceding code extracts the MX servers from the DNS reply and dis-plays them on-screen. It uses the  proc  function to convert between the nativeDNS format for domain names and the standard dot notation format. The next step is to implement the  proc  function as follows: C# private int proc(int position,byte[] data,ref string name) {  int len = (data[position++] & 0xFF);  if(len == 0)   {    return position;  }  int offset;  do  {    if ((len & 0xC0) == 0xC0)     {      if (position >= data.GetLength(0))       {        return -1;      }      offset = ((len & 0x3F) << 8) | (data[position++] &        0xFF);      proc(offset,data,ref name);      return position;    }     else     {      if ((position + len) > data.GetLength(0))       {        return -1;      }      name += Encoding.ASCII.GetString(data, position, len);      position += len; 312 12.2 DNS     }    if (position > data.GetLength(0))     {      return -1;    }    len = data[position++] & 0xFF;    if (len != 0)     {      name += ".";    }  }  while (len != 0);  return position;} VB.NET Private Function proc(ByVal position As Integer, ByVal data() _  As Byte, ByRef DomainName As String) As Integer  Dim len As Integer = data(position) And &HFF  position = position + 1  If len = 0 Then    Return position  End If  Dim offset As Integer  Do    If (len And &HC0) = &HC0 Then      If position >= data.GetLength(0) Then        Return -1      End If      offset = ((len And &H3F) << 8) Or (data(position))      position = position + 1      proc(offset, data, DomainName)      Return position    Else      If (position + len) > data.GetLength(0) Then        Return -1      End If      DomainName+=Encoding.ASCII.GetString(data, _      position, len)      position += len    End If 12.2 DNS313Chapter 12     If position > data.GetLength(0) Then      Return -1    End If    len = data(position)    position = position + 1    If len <> 0 Then      DomainName += "."    End If  Loop While len <> 0  Return positionEnd Function The  proc  function converts between the DNS native format for domainnames and the standard notation. It stores the result in a private variablenamed  name  and advances the position pointer to the end of the domainname.Finally, add the required namespaces: C# using System.Net;using System.IO;using System.Text;using System.Net.Sockets; VB.NET Imports System.NetImports System.IOImports System.TextImports System.Net.Sockets To run this application, ﬁrst ﬁnd the IP address of a DNS server. You canuse 204.111.1.36 or your ISP’s DNS server (type  IPConfig /all  in DOS toﬁnd it). Type the IP address of the DNS server into the box provided and adomain name (without the “www” preﬁx) into the second textbox. Pressﬁnd, and you will see the associated MX server appear in the textbox.  Note:  You will note that when you query hotmail.com, the MX servers cyclebetween 1 and 4. This is the effect of round-robin load balancing being used to handle the large volumes of mail handled by hotmail (Figure 12.1). 314 12.3 Ping 12.3Ping Ping or, as it is more correctly known, Internet control message protocol(ICMP), is a protocol used to report broken network connections or otherrouter-level problems that end hosts might need to know. When a routercan’t get its packet to the next hop, it discards the packet and sends anICMP packet back to the sender. ICMP packets are not used to report lostrouting problems for other ICMP packets in order to prevent network cas-cade effects.Many developers are familiar with the  ping  utility, which can be used todetermine if a computer is switched on or not and how much delay there isover the connection to it. This protocol can be implemented in .NET toprovide applications with the ability to check quickly if a computer towhich it needs to connect is turned on. It is possible to send a ping by constructing it with a raw socket; anexample of this can be seen at  www.eggheadcafe.com/articles/20020209.asp. A simpler implementation is to use the ICMP DLL, which is standard to allWindows platforms. Create a new project in Visual Studio .NET. Add a new module to theproject, and enter the following code: Figure 12.1 DNS MX clientapplication. 12.3 Ping315Chapter 12 C# public class PING{  public  struct IP_OPTION_INFORMATION  {    public byte TTL, Tos,Flags,OptionSize;    [MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]    public string OptionsData;  }  public struct ICMP_ECHO_REPLY  {    public uint Address, Status, RoundTripTime;    public ushort DataSize,Reserved;    public IP_OPTION_INFORMATION Options;    }  [DllImport("icmp.dll",SetLastError=true)]  public static extern uint IcmpSendEcho (    uint IcmpHandle,    uint DestAddress,    string RequestData,    uint RequestSize,    ref IP_OPTION_INFORMATION RequestOptns,    ref ICMP_ECHO_REPLY ReplyBuffer,    uint ReplySize,    uint TimeOut);  [DllImport("icmp.dll",SetLastError=true)]  public static extern uint IcmpCreateFile ();  public static IP_OPTION_INFORMATION pIPo;  public static ICMP_ECHO_REPLY pIPe;} VB.NET Option Strict OffOption Explicit On Module PING    Structure IP_OPTION_INFORMATION        Dim TTL As Byte        Dim Tos As Byte        Dim Flags As Byte 316 12.3 Ping         Dim OptionsSize As Integer         <VBFixedString(128), _        System.Runtime.InteropServices.MarshalAs  _  (System.Runtime.InteropServices.UnmanagedType.ByValTStr, _        SizeConst:=128)> _        Public OptionsData As String     End Structure    Structure IP_ECHO_REPLY        Dim Address As Int32        Dim Status As Integer         Dim RoundTripTime As Integer         Dim DataSize As Short        Dim Reserved As Short        Dim data As Integer        Dim Options As IP_OPTION_INFORMATION     End Structure    Public pIPo As IP_OPTION_INFORMATION    Public pIPe As IP_ECHO_REPLY    Declare Function IcmpCreateFile Lib "icmp.dll" () As _    Integer    Declare Function IcmpSendEcho Lib "ICMP" (ByVal _    IcmpHandle As Integer, ByVal DestAddress As UInt32, _    ByVal RequestData As String, _    ByVal RequestSize As Short, _    ByRef RequestOptns As IP_OPTION_INFORMATION, _    ByRef ReplyBuffer As IP_ECHO_REPLY, _    ByVal ReplySize As Integer, _    ByVal timeout As Integer) As BooleanEnd Module With nearly all API code, it is rarely necessary to understand everyparameter sent to each function.  IcmpCreateFile  creates a handle to resources used when generatingping requests. Where a program may issue large numbers of ping requests,then  IcmpCloseHandle  should be used to reclaim memory.  12.3 Ping317Chapter 12 IcmpSendEcho  sends an ICMP echo request to a host as speciﬁed in the DestAddress  parameter. The format of the outgoing ping is set in the RequestOptns  parameter, and details of the reply (or lack thereof) arestored in the  ReplyBuffer .Go to the form and draw a textbox named  tbIP  and a button named btnPing . Click on the button and add the following code: C# private void btnPing_Click(object sender, System.EventArgs e){          uint LongIP;  string buffer;  UInt32 hIP;  uint timeout;  buffer = new StringBuilder().Append(' ',32).ToString();  LongIP = convertIPtoLong(tbIP.Text);  hIP = PING.IcmpCreateFile();  PING.pIPo.TTL = 255;  timeout = 2700;  PING.IcmpSendEcho(hIP, LongIP, buffer,              (uint)buffer.Length,              ref PING.pIPo, ref PING.pIPe,              (uint)Marshal.SizeOf(PING.pIPe) + 8,             timeout);  MessageBox.Show(describeResponse(PING.pIPe.Status));}   VB.NET Private Sub btnPing_Click(ByVal eventSender As _System.Object, ByVal eventArgs As System.EventArgs) _    Handles btnPing.Click Dim LongIP As UInt32 Dim buffer As String Dim hIP As Integer Dim timeout As Short buffer = Space(32) LongIP = convertIPtoLong((tbIP.Text)) hIP = IcmpCreateFile() pIPo.TTL = 255 timeout = 2700 318 12.3 Ping   IcmpSendEcho(hIP, LongIP, buffer, Len(buffer), pIPo, _     pIPe, Len(pIPe) + 8, timeout)  MsgBox(describeResponse(pIPe.Status))End Sub You may notice that the IP address is converted from a string to aUint32 (unsigned 32-bit integer) by the  ConvertIPtoLong  function. This isrequired because the  DestAddress  parameter of  IcmpSendEcho  uses a binaryrepresentation of IP addresses.So, add in the following function to implement  convertIPtoLong:C#public UInt32 convertIPtoLong(string ip){  string[]  digits;  digits = ip.Split(".".ToCharArray());  return Convert.ToUInt32(          Convert.ToUInt32(digits[3]) * Math.Pow(2,24) +          Convert.ToUInt32(digits[2]) * Math.Pow(2,16) +          Convert.ToUInt32(digits[1]) * Math.Pow(2,8) +          Convert.ToUInt32(digits[0]));}VB.NETPublic Function convertIPtoLong(ByRef ip As String) As UInt32        Dim digits() As String        digits = Split(ip, ".")        convertIPtoLong = Convert.ToUInt32(digits(3) * 2 ^ 24 _          + digits(2) * 2 ^ 16 + _          digits(1) * 2 ^ 8 + _          digits(0)) End FunctionThis function splits an IP address into its four constituent bytes, multi-plies each byte by a power of 2, and adds them together. In the case of theloop-back address 127.0.0.1, this is converted to 127 + 1 × 224, or16,777,343. You may also notice in the code above that a message box is displayedonce IcmpSendEcho returns. This message could therefore describe to theuser the result of the ping request. The function describeResponse per-12.3Ping319Chapter 12forms the task of converting the rather cryptic response codes into mean-ingful phrases.Enter the following code:C#public string describeResponse(uint code){  string Rcode = "";  switch(code)  {    case 0 : Rcode = "Success";break;    case 11001 : Rcode = "Buffer too Small";break;    case 11002 : Rcode = "Dest Network Not Reachable";break;    case 11003 : Rcode = "Dest Host Not Reachable";break;    case 11004 : Rcode = "Dest Protocol Not Reachable";break;    case 11005 : Rcode = "Dest Port Not Reachable";break;    case 11006 : Rcode = "No Resources Available";break;    case 11007 : Rcode = "Bad Option";break;    case 11008 : Rcode = "Hardware Error";break;    case 11009 : Rcode = "Packet too Big";break;    case 11010 : Rcode = "Rqst Timed Out";break;    case 11011 : Rcode = "Bad Request";break;    case 11012 : Rcode = "Bad Route";break;    case 11013 : Rcode = "TTL Exprd in Transit";break;    case 11014 : Rcode = "TTL Exprd Reassemb";break;    case 11015 : Rcode = "Parameter Problem";break;    case 11016 : Rcode = "Source Quench";break;    case 11017 : Rcode = "Option too Big";break;    case 11018 : Rcode = " Bad Destination";break;    case 11019 : Rcode = "Address Deleted";break;    case 11020 : Rcode = "Spec MTU Change";break;    case 11021 : Rcode = "MTU Change";break;    case 11022 : Rcode = "Unload";break;    case 11050 : Rcode = "General Failure";break;  }  return Rcode;}VB.NETPublic Function describeResponse(ByRef code As Integer) _    As String32012.3Ping Dim Rcode As String Select Case code  Case 0 : Rcode = "Success"  Case 11001 : Rcode = "Buffer too Small"  Case 11002 : Rcode = "Dest Network Not Reachable"  Case 11003 : Rcode = "Dest Host Not Reachable"  Case 11004 : Rcode = "Dest Protocol Not Reachable"  Case 11005 : Rcode = "Dest Port Not Reachable"  Case 11006 : Rcode = "No Resources Available"  Case 11007 : Rcode = "Bad Option"  Case 11008 : Rcode = "Hardware Error"  Case 11009 : Rcode = "Packet too Big"  Case 11010 : Rcode = "Rqst Timed Out"  Case 11011 : Rcode = "Bad Request"  Case 11012 : Rcode = "Bad Route"  Case 11013 : Rcode = "TTL Exprd in Transit"  Case 11014 : Rcode = "TTL Exprd Reassemb"  Case 11015 : Rcode = "Parameter Problem"  Case 11016 : Rcode = "Source Quench"  Case 11017 : Rcode = "Option too Big"  Case 11018 : Rcode = " Bad Destination"  Case 11019 : Rcode = "Address Deleted"  Case 11020 : Rcode = "Spec MTU Change"  Case 11021 : Rcode = "MTU Change"  Case 11022 : Rcode = "Unload"  Case 11050 : Rcode = "General Failure" End Select describeResponse = RcodeEnd FunctionMany of the response codes listed would be rare and would probablyindicate a programming error instead of a real network error. The mostcommon are Success and Dest host not available. C# programmers will also require the following namespaces in both theform and class ﬁle:C#using System.Text;using System.Runtime.InteropServices;12.4WHOIS321Chapter 12To test the application, run it from Visual Studio .NET, type the IP address(not domain name!) of a well-known Web server into the box provided, andpress Ping. It should respond with the message “Success” if the computer isaccessible or “Dest Host Not Reachable” if it is not, as in Figure 12.2.Ping can be used for more than simply checking whether a computer isswitched on or not; it can also be used to trace the route of packets over theInternet. This is achieved by sending a ping request with a TTL of 1, fol-lowed by a ping with a TTL of 2, and so on. At each hop, a router willreport a dead ping request and send a packet back to the original host,which will contain the IP address of the router. This technique is used bythe tracert utility. In .NET v2 (Whidbey), it is possible to retrieve statistics easily relatingto the number and type of pings received and sent by your computer. Pleaserefer to the IcmpV4Statistics class, as described in Chapter 13, for moreinformation on this topic.12.4WHOISWHOIS (“who is”) is a protocol that can be used to query the registrant ofa domain name. It runs on TCP port 43 and is described deﬁnitively inRFC 954. This information includes the name and company of the personwho bought the domain name, along with details of the DNS servers forthat domain and the operator(s) of those servers.Despite its usefulness, WHOIS is a poorly designed protocol. There aremany WHOIS servers worldwide, each of which contains a subset of all theInternet domain names. There is no way to determine from a domain nameFigure 12.2ICMP (ping) clientapplication.32212.4WHOISwhich WHOIS server contains registrant information for that name. Fur-thermore, the content of WHOIS replies is not properly standardized,which makes it particularly difﬁcult to parse replies properly.Note: Operators of WHOIS servers generally limit the number of queriesper day per IP address to 100 in order to prevent data mining. Most countries have their own WHOIS server that covers the top-leveldomain for that country (such as .co.uk or .ie). International top-leveldomains such as .com, .net, and .org are stored in subsets in large WHOISservers or allocated by central WHOIS servers on a continent-by-continentbasis. A few well-known WHOIS servers are whois.networksolutions.com,whois.crsnic.net, and whois.ripe.net.To perform a WHOIS query manually, run telnet from the commandprompt, and type the following:O whois.ripe.net 43Google.deThe result will be as follows (abbreviated for clarity):% This is the RIPE Whois server.% The objects are in RPSL format.% The object shown below is NOT in the RIPE database.% It has been obtained by querying a remote server:% (whois.denic.de) at port 43.                                                      %REFERRAL START                                                                             domain:      google.dedescr:       Google Inc.descr:       Valentinskamp 24descr:       20354 Hamburgdescr:       GERMANYnserver:     ns1.google.comnserver:     ns2.google.comnserver:     ns3.google.comnserver:     ns4.google.com                                                                 status:      connect12.4WHOIS323Chapter 12changed:     20021125 170514source:      DENIC                                                   [admin-c]                                                            Type:         PERSONName:         joel FokkeAddress:      Valentinskamp 24City:         HamburgPcode:        20354Country:      DEChanged:      20021023 150831Source:       DENIC                                                                              [tech-c][zone-c]Type:         ROLEName:         DENICoperationsAddress:      DENIC eGAddress:      Wiesenhuettenplatz 26City:         Frankfurt am MainPcode:        60329Country:      DEPhone:        +49 69 27235 272Fax:          +49 69 27235 234Email:        ops@denic.deChanged:      20020621 194343Source:       DENIC                                                                            %REFERRAL END Unfortunately, as mentioned earlier, the WHOIS reply is not standard-ized, so expect different ﬁelds from different WHOIS servers. Whois.Net-workSolutions.Com will return ﬁelds in this format (abbreviated reply forhotmail.com):Registrant:  Microsoft Corporation (HOTMAIL-DOM)    One Microsoft Way    Redmond, CA 98052    USDomain Name: HOTMAIL.COM32412.4WHOISAdministrative Contact: Gudmundson, Carolyn(PPUFRBYFWI)                        domains@microsoft.com                        One Microsoft Way                        Redmond, WA 98052                        US                        (425) 882-8080 fax: (425) 936-7329Technical Contact:      NOC, MSN  (RWJALTFZAI)                        msnhst@microsoft.comNote: For a bit of entertainment, look up the WHOIS entry forMicrosoft.com with whois.crsnic.net. You’ll ﬁnd some interesting entriesmade by some Linux fans!Performing a WHOIS query with .NET is easy. All that is required is toopen a TCP connection on port 43, send the domain name followed by thenew line character, and read back the response until the connection closes. Create a new project in Visual Studio .NET. Draw three textboxesnamed tbServer, tbQuery, and tbStatus, the latter having multiline setto true. A button named btnSend is also required.Click on the Send button, and add the following code:C#private void btnSend_Click(object sender, System.EventArgs e){  byte[] Query = Encoding.ASCII.GetBytes(  tbQuery.Text + "\n");  TcpClient clientSocket = new TcpClient(tbServer.Text,43);      NetworkStream networkStream =  clientSocket.GetStream();  networkStream.Write(Query,0,Query.GetLength(0));  StreamReader Response = new StreamReader(networkStream);  tbStatus.Text=Response.ReadToEnd();  networkStream.Close();  }12.4WHOIS325Chapter 12VB.NETPrivate  Sub btnSend_Click(ByVal sender As Object, _        ByVal e As System.EventArgs)  Dim Query() As Byte =  Encoding.ASCII.GetBytes _        (tbQuery.Text + vbcrlf)   Dim clientSocket As TcpClient =  New _        TcpClient(tbServer.Text,43)   Dim networkStream As NetworkStream = _        clientSocket.GetStream()   networkStream.Write(Query,0,Query.GetLength(0))    Dim Response As StreamReader =  New _        StreamReader(networkStream)     tbStatus.Text=Response.ReadToEnd()  networkStream.Close()End SubYou will also require a reference to some namespaces needed for thestring handling and networking:C#using System.Text;using System.Net;using System.Net.Sockets;using System.IO;VB.NETImports System.TextImports System.NetImports System.Net.SocketsImports System.IOTo test the application, run it from Visual Studio .NET. Enter the nameof a WHOIS server in the box provided, in this case whois.crsnic.net.Enter a domain name in the query box, omitting the “www” preﬁx. PressSend, and you should receive information about the registrant of thatdomain, similar to that shown in Figure 12.3.32612.4WHOIS12.4.1TelnetIn the days before GUIs, users of UNIX enjoyed the luxury of being able tocontrol their server remotely via a command-line interface. Text-only inter-faces may be passé, but many online services are still hosted on UNIX, andwhere conﬁguration changes need to be made to the server, telnet is still thedefacto standard for UNIX servers.The protocol itself is straightforward: a TCP connection is opened onport 23, and this connection is persisted until one end closes the connec-tion. Generally, any character typed on the keyboard is sent to the serverand any returned data is displayed on-screen as text.Telnet could be used as a back end to a remote conﬁguration console fora UNIX product, but beyond that, it would rarely be used programmati-cally. It is, however, often used to debug servers and investigate new TCP-based protocols because all telnet clients provide the option to connect onports other than 23.A telnet client is included with Windows. In Windows 95 and 98, thetelnet client has a GUI, but XP uses a DOS-based client. If you have a Webserver on your computer, you can check that telnet is operational by typingthe following code at the command prompt:telnet localhost 80GET /Figure 12.3WHOIS clientapplication.12.5Other members of the TCP/IP suite327Chapter 12If the server is online, an HTTP reply will be displayed on-screen simi-lar to Figure 12.4. Otherwise, a “Could not open connection to the host”message will be displayed. A secure version of telnet named SSH is now widely used to communi-cate with Linux and UNIX boxes.12.5Other members of the TCP/IP suiteMany protocols work behind the scenes in IP networks to provide the ser-vice. These would generally not be used programmatically, but they areworth being aware of.12.5.1ARPAddress resolution protocol (ARP) resolves IP addresses into their equivalentMAC addresses. Reverse ARP (RARP) performs the reverse of this function. To view the ARP entries stored on your system, try the following:DOSC:\>arp -a12.5.2RIPRouting information protocol (RIP) works by counting the number oftimes a packet moves toward its destination. Each new routing is called aFigure 12.4Telnet MS-DOSutility.32812.5Other members of the TCP/IP suitehop, and the maximum hop count is usually set to 16. RIP will discardpackets that are routed more than 16 times. 12.5.3OSPFOpen shortest path ﬁrst (OSPF) is a routing protocol that uses a link-statealgorithm. This type of algorithm looks at the available routes a data packetcan take to its destination and decides the fastest route. OSPF does nothave a maximum hop count.12.5.4BGP/EGPBorder gateway protocol (BGP) supersedes exterior gateway protocol(EGP) and is used to route packets outside of a network to other people’snetworks. It differs from OSPF, which is used in internal networks. Note: You should never have two BGP routers on the same network with-out support for OSPF or RIP.12.5.5SNMPSimple network management protocol (SNMP) enables network adminis-trators to connect and manage network devices. It is being supersededwith RMON, but is still widely used by network devices. It operates overUDP port 161 and is generally accessed using a managed information base(MIB) browser (downloadable from www.mg-soft.com). An MIB is a col-lection of resource variables, providing information about the status of thedevice. SNMP can issue traps (events) when something goes wrong with anetwork device.12.5.6PPPPoint-to-point protocol (PPP) can be used to transport IP, IPX, and Net-BEUI over serial links such as modem connections. PPP is commonly usedby ISPs to provide subscribers with modem or ISDN Internet access. PPPrequires a phone number and, usually, a DNS server address, with user-name and password. PPP supersedes Serial Line Internet Protocol (SLIP)because of its speed, simplicity, and ﬂexibility.12.6WMI329Chapter 1212.6WMIWMI, or Windows Management Instrumentation, is used within a Win-dows intranet to provide a facility to perform simple administrative tasksremotely. The main advantage this provides is that the WMI client is builtinto Windows, so there is no need to write or install a proprietary client, aslong as the Windows Management Instrumentation service is running onthe remote machine. One of the main uses of WMI is to extract technical informationabout remote Windows systems. Whether you want to tell how much freedisk space is on a remote computer or discover its CPU clock speed,WMI can do the job.WMI is structured somewhat like a database. The CIM (CommonInformation Model) repository holds multiple namespaces. These in turnhold many classes, which have properties which correspond to eitherdevices such as a CD-ROM drive or intangiable processes or data such asthe NT event log.To view the CIM namespaces installed on your system, run WBE-MTEST from the command line. Press Connect→→→→type Root→→→→Connect→→→→Enum Instances→→→→type __NAMESPACE→→→→ok. A few namespaces of inter-est are:(cid:1)root\directory\ldap: provides access to active directory services(cid:1)root\snmp: provides access to SNMP MIB data(cid:1)root\default: provides access to the windows registry(cid:1)root\WMI: provides access to Windows Device Model (WDM)devices.The root\cimv2 namespace is the largest of all the CIM namespaces, andforms the basis of the following examples. To view a list of all the classes con-tained within the root\cimv2 namespace, load WBEMTEST, press Con-nect→→→→Type root\cimv2→→→→Connect→→→→Enum Classes→→→→Check Recursive→→→→clickOk. The data contained in these classes can be queried using a languageknown as WQL (WMI Query Language), as the example in section 12.6.1demonstrates.33012.6WMI12.6.1Reading WMI dataWMI data may resemble a database conceptually, but the System.Manage-ment namespace, which encapsulates WMI, is dissimilar to the data accessnamespaces. In the same way as a database connection is required beforeSQL can be executed, a scope must be deﬁned before WQL can be used.WMI uses a ManagementScope that is passed the location of the remotecomputer in the format \\<host name>\root\namespace and a Connec-tionOptions object that contains the logon credentials (username andpassword).A ManagementObjectSearcher processes the WQL. This object returns aManagementObjectCollection when the Get() method is called. This col-lection is similar to a table, where every element represents a row in thetable. This row is represented as a ManagementBaseObject. Every row has avariable number of columns, which are represented by a collection of Prop-ertyData objects held within the Properties collection contained in eachManagementBaseObject object.Start a new project in Visual Studio .NET. Under Project→→→→Add Refer-ences, add a reference to System.Management. Draw four textboxes onto theform named tbHost, tbUsername, tbPassword, and tbExecute. You willalso require a list view named lvWMI and a button named btnExecute.Click on the Execute button and add the following code:C#private void btnExecute_Click(object sender, System.EventArgs e){     ConnectionOptions Options = new ConnectionOptions(); if(tbPassword.Text != "" && tbUsername.Text != "") {  Options.Username = tbHost.Text + "\\" + tbUsername.Text;  Options.Password = tbPassword.Text; }       ManagementScope Scope = new ManagementScope("\\\\" + tbHost.Text "\\root\\cimv2", Options); Scope.Connect(); ObjectQuery Query = new ObjectQuery(tbExecute.Text); ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query); ManagementObjectCollection ItemCollection;12.6WMI331Chapter 12 ItemCollection = Searcher.Get(); lvWMI.Clear(); lvWMI.Columns.Clear(); lvWMI.View = View.Details; foreach(ManagementBaseObject Item in ItemCollection) {  if (lvWMI.Columns.Count==0)  {   foreach (PropertyData prop in Item.Properties)   {    lvWMI.Columns.Add(prop.Name,                      lvWMI.Width/4,                       HorizontalAlignment.Left);   }  }  ListViewItem lvItem = new ListViewItem();  bool firstColumn = true;  foreach (PropertyData prop in Item.Properties)  {   if (firstColumn)   {    lvItem.SubItems[0].Text = prop.Value+"";    firstColumn=false;   }   else   {    lvItem.SubItems.Add(prop.Value+"");   }  }  lvWMI.Items.Add(lvItem); }}VB.NETPrivate  Sub btnExecute_Click(ByVal sender As Object, _        ByVal e As System.EventArgs)     Dim Options As ConnectionOptions If tbPassword.Text <> "" And tbUsername.Text <> "" Then        Options.Username = tbHost.Text + "\\" + _              tbUsername.Text        Options.Password = tbPassword.Text33212.6WMI End If Dim Scope As ManagementScope = New ManagementScope _        ("\\" + tbHost.Text + "\root\cimv2", Options) Scope.Connect() Dim Query As ObjectQuery = New ObjectQuery(tbExecute.Text) Dim Searcher As ManagementObjectSearcher = New _        ManagementObjectSearcher(Scope, Query) Dim ItemCollection As ManagementObjectCollection ItemCollection = Searcher.Get() lvWMI.Clear() lvWMI.Columns.Clear() lvWMI.View = View.Details Dim Item As ManagementBaseObject For Each Item In ItemCollection  Dim prop As PropertyData  If lvWMI.Columns.Count = 0 Then   For Each prop In Item.Properties    lvWMI.Columns.Add(prop.Name, _           lvWMI.Width / 4, _           HorizontalAlignment.Left)   Next  End If  Dim lvItem As ListViewItem = New ListViewItem  Dim firstColumn As Boolean = True  For Each prop In Item.Properties    If firstColumn = True Then      lvItem.SubItems(0).Text = Convert.ToString(prop.Value)      firstColumn = False    Else      lvItem.SubItems.Add(Convert.ToString(prop.Value))    End If  Next  lvWMI.Items.Add(lvItem) NextEnd SubYou will also require a reference to the relevant namespaces, so add thiscode to the top of the application:C#using System.Management;12.6WMI333Chapter 12VB.NETImports System.ManagementTo test the application, run it from Visual Studio .NET, and typelocalhost into the host box provided, entering a username and password ifone is required on your machine. Type a WQL query such as Select *from Win32_NetworkAdapterConfiguration and press Execute. The listview should ﬁll with information about your system (Figure 12.5).To run WMI queries against remote machines, you must have adminis-trator privileges on those computers.12.6.2Leveraging WMIYou are not restricted to reading data when using WMI; you can also per-form actions on remote computers using this technology. Functions such asstarting and stopping services, rebooting, and starting and terminating pro-cesses can all be performed directly from WMI. In order to view whichmethods may be called on any given WMI class, load WBEMTEST, con-nect to the container namespace (i.e. root\cimv2), click Create Class, thentype the name of the WMI Class (i.e. WIN32_PROCESS), and press con-tinue. The supported methods will be listed on-screen. The most generictask that can be performed with WMI is to start a process. This process(application) could then carry out any function that is required.Figure 12.5WMI querylanguage analyzerapplication.33412.6WMILike the previous WMI example, a connection, or scope, is required to theremote computer. This is created in exactly the same way. Instead of executinga WQL query, a ManagementClass is obtained for the Win32_Process class.This WMI class holds a method named Create that can spawn new pro-cesses. This method is passed parameters via a ManagementBaseObject object.Create a new project in Visual Studio .NET. Under Project→→→→Add Refer-ences, add a reference to System.Management. Draw four textboxes onto theform named tbHost, tbUsername, tbPassword, and tbExecute. Add a but-ton named btnExecute. Click on it and enter the following code:C#private void btnExecute_Click(object sender, System.EventArgs e){     ConnectionOptions Options = new ConnectionOptions(); if(tbPassword.Text != "" && tbUsername.Text != "") {  Options.Username = tbHost.Text + "\\" + tbUsername.Text;  Options.Password = tbPassword.Text; }       ManagementScope Scope = new ManagementScope("\\\\" + tbHost.Text + "\\root\\cimv2", Options); Scope.Connect(); ManagementClass ProcessClass = new ManagementClass("Win32_Process"); ManagementBaseObject inParams = ProcessClass.GetMethodParameters("Create"); ProcessClass.Scope = Scope; inParams["CommandLine"] = tbExecute.Text;             ProcessClass.InvokeMethod("Create", inParams, null);     }VB.NETPrivate  Sub btnExecute_Click(ByVal sender As Object, _    ByVal e As System.EventArgs)     Dim Options As ConnectionOptions =  New ConnectionOptions()  If tbPassword.Text <> "" and tbUsername.Text <> ""  Options.Username = tbHost.Text + "\\" + tbUsername.Text  Options.Password = tbPassword.Text End if Dim Scope as ManagementScope = New ManagementScope _12.6WMI335Chapter 12         ("\\" + tbHost.Text + "\root\cimv2" ,Options) Scope.Connect() Dim ProcessClass As ManagementClass =  New _         ManagementClass("Win32_Process")  Dim inParams As ManagementBaseObject = _          ProcessClass.GetMethodParameters("Create")  ProcessClass.Scope = Scope inParams("CommandLine") = tbExecute.Text             ProcessClass.InvokeMethod("Create", inParams, Nothing)End SubYou will also require a reference to the relevant namespaces, so add thiscode to the top of the application:C#using System.Management;VB.NETImports System.ManagementTo test the application, run it from Visual Studio .NET, type in local-host for the host, and provide the username and password if required. Typenotepad.exe into the command-line box as shown in Figure 12.6, andpress Execute. You should see Notepad opening on-screen.Again, this can be run remotely, as long as you have administrator privi-leges on a remote computer on the network. Figure 12.6WMI remoteprocess managerapplication.33612.7Conclusion12.7ConclusionThis chapter has dealt with a set of network protocols that are not suited tomoving bulk data among machines, but are particularly valuable in addingfeatures and improving the performance of distributed applications. Theseutility protocols can be used to test quickly if machines are online, whatdomain names or hosts are associated with them, and who is the registrantof the domain name. This provides vital extra information that ultimatelyadds value to your ﬁnal product. The chapter concluded with a look at a surprisingly versatile Microsofttechnology, WMI, which can pull virtually every conceivable piece of tech-nical information from a remote computer over. WMI is an absolutelyessential technology for internal IT support.The next chapter takes a microscope to the network and looks at exactlywhat gets sent down the phone line when you use the Internet. If you’re ona LAN, you might be surprised to see what passes through your computerwithout your knowledge. Be warned: Read the following chapter, and you’llnever play multiplayer games on your company network again! 337 13 Analyzing Network Packets 13.1Introduction Network programming is very much concerned with moving data from cli-ent to server, but when you need to look at what is moving between the cli-ent and server, you encounter a problem. In most cases, there is no need for a program to know what data is beingreceived by other applications. Furthermore, it is a security risk to have oneprogram that could scan third-party applications, such as FTP software,and retrieve the username and password for your Web site; however, if youare building a value-added package to a third-party application, such as acontent ﬁlter for a proprietary or legacy application, tapping into what isbeing sent between client and server is a good start.Packet capture isn’t something new. It has been around for many years.But very few applications actually leverage the technology to provide toolsthat can be used in conjunction with other software to provide virus orcomputer-misuse detection. What is available, though, are extensive toolsthat can tell you what each byte in every packet means, down to even thecomputer manufacturer that sent the packet. Figure 13.1 shows the demoversion of TracePlus from  www.sstinc.com.  Note:   In order to determine the manufacturer of a particular piece of equip-ment from its MAC address, access the listing at  http://standards.ieee.org/regauth/oui/oui.txt , which   contains most, if not all, network equipment man- ufacturers with their allocated MAC address space.Software that can leverage packet-level data can be useful for businesses.We have all heard of the scenario where a few employees decide to down-load their favorite band’s latest album on Mp3 the day of a big presentation, 338 13.1 Introduction causing a total misallocation of bandwidth within a company. This is wheretrafﬁc-detection software comes into its own, providing an early warningsystem for bandwidth misuse.Trafﬁc-detection software can be used to detect packets on a networkthat could uncover viruses, use of unauthorized software, and email forgery.Let’s look brieﬂy at how the applications mentioned above could be imple-mented using packet-level monitoring. You can use trafﬁc detection to discover the presence of viruses andattacks in progress, but unfortunately not to prevent them. It could, how-ever, be used to provide companywide detection of infected computers anddenial-of-service attacks. The telltale signs of virus propagation could berapid sequential accesses to computers within the subnet on port 80 (scan-ning for servers to infect) or heartbeat signals coming from a nonstandardport to an external server (ﬁrewall tunneling).Denial-of-service attacks could be detected from the presence of a largenumber of corrupted packets sent to a particular server. A fragmented pingrequest would indicate a ping-of-death attack. Large numbers of incom- Figure 13.1 TracePlus utility. 13.2 IP-level network tapping339Chapter 13 plete TCP connections would indicate a  SYN  ﬂood attack, in which the ﬁrstpacket of a TCP handshake is sent repetitively and rapidly. The victimattempts to establish TCP sessions for each of the packets by sending  ACK (acknowledge) packets to the attacker, which are not responded to. The vic-tim eventually becomes overwhelmed with pending TCP sessions anddenies all network trafﬁc.Detection of unauthorized software usage could be useful in a com-pany where employees may be partial to spending time playing computergames during work hours. Multiplayer computer games generally operateon a high port over TCP/IP or IPX. Games produce a lot of network traf-ﬁc and, thus, can be spotted easily in a TCP/IP trace. The IP addresses ofthe offending employee’s computers could be logged, and the employeecould be suitably warned.Email trafﬁc could also be monitored remotely using these techniques.This could be used to detect company secrets being sent to a competitor.Furthermore, a system to prevent email spooﬁng and forgery could beimplemented if SMTP trafﬁc were monitored. An application could keep arecord of each employee’s computer’s IP address and email address. In theevent of a mismatch between the IP and email address, an alarm could beraised, possibly sending an email to the recipient warning of the possibilityof email forgery.This chapter begins with information about how to read and interpretIP-level trafﬁc on your network. It then progresses to more complex exam-ples about how to drill down further into the network stack and extractlower-level data at the frame level. The chapter concludes with informationabout how to use new classes introduced in .NET 2.0 Whidbey to gathersystemwide network information. 13.2IP-level network tapping Network tapping anything that runs at the IP level includes TCP/IP andUDP 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 bedone natively in .NET.To implement a layer 3 network tap in .NET, open a new project inVisual 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 boxto Courier for easier reading.  340 13.2 IP-level network tapping After designing the user interface, you should add the following publicvariable, a reference to the main listener thread: C# public Thread Listener; VB.NET Public Listener as Thread Click on the Start button and enter the following code: C# private void btnStart_Click(object sender, System.EventArgs e){  btnStart.Enabled = false;  btnStop.Enabled = true;          Listener = new Thread(new ThreadStart(Run));  Listener.Start();} VB.NET Private Sub btnStart_Click(ByVal sender As Object, _        ByVal e As System.EventArgs)  btnStart.Enabled = False  btnStop.Enabled = True          Listener = New Thread(New ThreadStart(AddressOf Run))  Listener.Start()End Sub The  Run  method is where the network tap takes place. It is a processor-intensive task, so it is executed in its own thread, as can be seen from thecode. Click on the Stop button and enter the following code: C# private void btnStop_Click(object sender, System.EventArgs e){  btnStart.Enabled = true;  btnStop.Enabled = false;  if(Listener != null) 13.2 IP-level network tapping341Chapter 13   {        Listener.Abort();    Listener.Join();    Listener = null;  }} VB.NET Private Sub btnStop_Click(ByVal sender As Object, _        ByVal e As System.EventArgs)  btnStart.Enabled = True  btnStop.Enabled = False  If Not Listener Is Nothing Then    Listener.Abort()    Listener.Join()    Listener = Nothing  End IfEnd Sub This code simply kills the thread containing the network tap, whicheffectively stops reporting the arrival of new packets. C# public void Run() {  int len_receive_buf = 4096;  int len_send_buf = 4096;  byte[] receive_buf = new byte[len_receive_buf];  byte[] send_buf = new byte[len_send_buf];    int cout_receive_bytes;  Socket socket = new Socket(AddressFamily.InterNetwork,  SocketType.Raw, ProtocolType.IP);  socket.Blocking = false;  IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());  socket.Bind(new  IPEndPoint(IPAddress.Parse  (IPHost.AddressList[0].ToString()), 0));      socket.SetSocketOption(SocketOptionLevel.IP,      SocketOptionName.HeaderIncluded, 1);  byte []IN = new byte[4]{1, 0, 0, 0};  byte []OUT = new byte[4]; 342 13.2 IP-level network tapping   int SIO_RCVALL = unchecked((int)0x98000001);  int ret_code = socket.IOControl(SIO_RCVALL, IN, OUT);      while(true)  {       IAsyncResult ar = socket.BeginReceive(receive_buf, 0,     len_receive_buf, SocketFlags.None, null, this);    cout_receive_bytes = socket.EndReceive(ar);     Receive(receive_buf, cout_receive_bytes);  }} VB.NET Public Sub Run() Dim len_receive_buf As Integer = 4096 Dim len_send_buf As Integer = 4096 Dim receive_buf() As Byte = New Byte(len_receive_buf) {} Dim send_buf() As Byte = New Byte(len_send_buf) {} Dim cout_receive_bytes As Integer Dim socket As Socket = New _   Socket(AddressFamily.InterNetwork, _   SocketType.Raw, ProtocolType.IP) socket.Blocking = False Dim IPHost As IPHostEnTry = _ Dns.GetHostByName(Dns.GetHostName()) socket.Bind(New _   IPEndPoint(IPAddress.Parse _   (IPHost.AddressList(0).ToString()), 0)) socket.SetSocketOption(SocketOptionLevel.IP, _     SocketOptionName.HeaderIncluded, 1) Dim bIN As Byte() = New Byte() {1, 0, 0, 0} Dim bOUT As Byte() = New Byte() {0, 0, 0, 0} Dim SIO_RCVALL As Integer = &H98000001 Dim ret_code As Integer = socket.IOControl _       (SIO_RCVALL, bIN, bOUT) Do   Dim ar As IAsyncResult = socket.BeginReceive _     (receive_buf, 0, _      len_receive_buf, SocketFlags.None, Nothing, Me)    cout_receive_bytes = socket.EndReceive(ar)    Receive(receive_buf, cout_receive_bytes) Loop End Sub 13.2 IP-level network tapping343Chapter 13 The  Run  method is the core thread of the application. It creates a rawsocket bound to the local machine on the default adapter. The socket’s nor-mal operating parameters are then modiﬁed using the  IOControl  method,which accesses the underlying socket API function  WSAIoctl . This functionis passed a parameter  SIO_RCVALL  (98000001 Hex). Use of this parameterenables a socket to receive all IP packets on the network. The socket mustbe in  RAW  mode, using the IP protocol, and bound to a speciﬁc localadapter. This feature requires administrator privilege on the local machine.The packet parsing and display has been separated from the tapping threadto make the program more legible. This method is called  Receive  andshould be implemented thus: C# public void Receive(byte []buf, int len){ if (buf[9]==6) {   lbPackets.Items.Add   (Encoding.ASCII.GetString(buf).Replace("\0"," ")); }} VB.NET Public Sub Receive(ByVal buf as byte(), ByVal len As Integer) If buf(9)=6 then   lbPackets.Items.Add(Encoding.ASCII.GetString _    (buf).Replace(chr(0)," ")) end if End Sub In this example, trafﬁc is ﬁltered so that only TCP/IP packets are shown.This means that the screen is not cluttered with DNS queries, pings, andUDP data. TCP/IP packets will always have the ninth byte in the header setto 6. All null (ASCII code 0) characters are displayed as spaces so that thelist box does not crop the string at the ﬁrst null character.Finally, you need to add some standard namespaces to the code: C# using System;using System.Windows.Forms; 344 13.2 IP-level network tapping using System.Net.Sockets;using System.Net;using System.Threading;using System.Text; VB.NET Imports SystemImports System.Windows.FormsImports System.Net.SocketsImports System.NetImports System.ThreadingImports System.Text To test the application, run it from Visual Studio .NET, and visit aWeb site using your browser. You should see the raw TCP data ﬂowingbetween your browser and the Web server appear in the list box, as shownin Figure 13.2. 13.2.1Interpreting 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 provid-ing a useful tool that could be of real beneﬁt to network managers andadministrators. Figure 13.2 IP-layer packetsniffer application. 13.2 IP-level network tapping345Chapter 13 Raw network data can appear totally unordered, with HTTP packetsmixed in with NETBIOS (Microsoft ﬁle sharing) and ARP, each perform-ing different tasks, but working simultaneously. Some of these packets canbe recognized immediately: familiar snippets of HTML generally indicateHTTP (although it could be a rich-text email or a Web page beinguploaded). Most networks will primarily ferry IP trafﬁc, but will also carrynon-IP trafﬁc such as NETBIOS and ARP.Every packet carries a header that is in a strictly deﬁned binary format.To deﬁne the standards involved most concisely, the tables in this chapterlist the name and starting point of each ﬁeld in the relevant header. Everyﬁeld runs contiguously with the next; thus, the length of any ﬁeld can becalculated by subtracting its starting point from the following ﬁeld’s start-ing point.Because ﬁelds do not need to start at a byte boundary, the bit number isalso provided in the second column. Where a ﬁeld is commonly known asbeing part of a collective ﬁeld, it is separated in the description columnfrom the parent ﬁeld by a colon.The frame header (Table 13.1) is the only part that the hardware withinthe network card will actually read or process. It is 14 bytes long, contain-ing the hardware address of the source and destination computers. In thecase where the hardware address of the destination computer is unknown,this address is set to  FF-FF-FF-FF-FF-FF  (hex). Over PPP connections, thesource and destination MAC address may be omitted and replaced by  SRC and  DEST .The Ethernet type code is a two-digit hex number that speciﬁes thepacket protocol (Table 13.2). A fairly comprehensive list of Ethernet typecodes can be seen at  www.cavebear.com/CaveBear/Ethernet.  Table 13.1 Ethernet frame header. Byte OffsetBit OffsetDescription 11Destination MAC address61Source MAC address121Ethernet type code (2 bytes) 346 13.2 IP-level network tapping 13.2.2IP packets in detail In IP packets, the IP header (Table 13.3) immediately follows the frameheader at byte 14. IP is deﬁnitively described in RFC 791.  Table 13.2 Ethernet type codes. Type Code (Hex)Meaning 0800IP version 40805X.250806ARP8035RARP8037IPX809BAppleTalk80F3AppleTalk ARP814CSNMP86DDIP version 6 Table 13.3 IP header . Byte OffsetBit OffsetDescription 11Version15Header length21Type of service: Precedence24Type of service: Delay25Type of service: Throughput26Type of service: Reliability27Type of service: (Reserved)31Data length51Identiﬁcation71Flags74Fragment offset 13.2 IP-level network tapping347Chapter 13 (cid:1) Version:  Set to 0100 for IPv4 and 0110 for IPv6. (cid:1) Header length:  The length of the header divided by 4. Max length is60. (cid:1) Type of service:  May be used to increase quality of service on some net-works. (cid:1) Data length:  The combined length of the header and data. (cid:1) Identiﬁcation:  A random number used to identify duplicate packets. (cid:1) Flags:  Indicates the fragmentation status of the packet. Bit 2 is set to 1when the datagram cannot be fragmented (0 when it can). Bit 3 is setto 1 when there are more fragments in the datagram, and 0 when thispacket is the last fragment. (cid:1) Fragment offset:  Indicates the position a packet should occupy withina fragmented datagram. (cid:1) TTL:  Indicates the number of nodes through which the datagramcan pass before being discarded. Used to avoid inﬁnite routing loops. (cid:1) Protocol:  Set to 6 for TCP, 17 for UDP, 1 for ICMP, and 2 for IGMP. (cid:1) Checksum:  A checksum of the IP header calculated using a 16-bitones complement sum.  (cid:1) Source:  The IP address of the sending computer. 91TTL101Protocol111Header checksum131Source address171Destination address211Option: code: Flag 212Option: code: Class214Option: code: Number221Option: Length231Option: Data (variable length)Data (variable length) Table 13.3 IP header (continued).34813.2IP-level network tapping(cid:1)Destination: The IP address of the receiving computer.(cid:1)Option: An optional ﬁeld that may contain security options, routingrecords, timestamps, and so forth. 13.2.3ICMP packets in detailIn ICMP (ping) packets immediately following the IP header make up a 4-byte header (Table 13.4) followed by a body of variable length. A deﬁnitivedescription of ICMP can be found in RFC 792.Type: Deﬁnes the purpose or function of the packet. See Table 13.5.Code: A type code–speciﬁc identiﬁer that more accurately describes thefunction of the packet. In a destination unreachable (3) packet, 0 indicatesthe subnet is down, whereas 1 indicates that only the host is down.Checksum: The checksum is the 16-bit ones complement of the onescomplement sum of the ICMP message starting with the ICMP type.Table 13.4ICMP header.Byte OffsetBit OffsetDescription11Type21Code31ChecksumTable 13.5ICMP type codes .Type CodeMeaning0Echo reply3Destination unreachable4Source quench5Redirect8Echo request11Timeout12Parameter unintelligible13Timestamp request13.2IP-level network tapping349Chapter 1313.2.4TCP/IP packets in detailIn TCP/IP packets, immediately following the IP header is a 24-byte TCPheader (Table 13.6). A deﬁnitive description of TCP can be found inRFC 793.14Timestamp reply15Info request16Info reply17Address request18Address replyTable 13.6TCP header. Byte OffsetBit OffsetDescription11Source port31Destination port51Sequence number9 1 Acknowledgment number131Data offset134Reserved143Urgent (URG)144Acknowledge (ACK)145Push (PSH)146Reset (RST)147Synchronize (SYN)148Finish (FIN)151Window171Checksum191Urgent pointerTable 13.5ICMP type codes (continued).Type CodeMeaning35013.2IP-level network tapping(cid:1)Source port: The port the TCP connection is made from, usually ahigh port.(cid:1)Destination port: The destination port, 80 for HTTP and 25 forSMTP, etc.(cid:1)Sequence number: The sequence number of the ﬁrst data octet in thissegment (except when SYN is present). If SYN is present, the sequencenumber is the initial sequence number (ISN), and the ﬁrst data octetis ISN + 1.(cid:1)Acknowledgment number: If the ACK control bit is set, this ﬁeld containsthe value of the next sequence number the segment sender is expectingto receive. Once a connection is established, this is always sent.(cid:1)Data offset: The number of 32-bit words in the TCP header. Thisindicates where the data begins. The TCP header (even one includingoptions) is an integral number 32 bits long.(cid:1)Urgent (URG): When set to 1, the urgent pointer ﬁeld is signiﬁcant.(cid:1)Acknowledge (ACK): When set to 1, the acknowledgment ﬁeld is sig-niﬁcant.(cid:1)Push (PSH): Implements a push function.(cid:1)Reset (RST): When set to 1, it will reset the connection.(cid:1)Synchronize (SYN): Synchronizes sequence numbers.(cid:1)Finish (FIN): Indicates that there is no more data from the sender.(cid:1)Window: The number of data octets indicated in the acknowledg-ment ﬁeld that the sender of this segment is willing to accept.(cid:1)Checksum: The checksum ﬁeld is the 16-bit ones complement of theones complement sum of all 16-bit words in the header and text, ifsegments are zero-padded to form a multiple of 16-bit words forchecksum purposes. The pad is not transmitted as part of the seg-ment.  While computing the checksum, the checksum ﬁeld isreplaced with zeros. The checksum also covers a pseudoheader that is211Options241PaddingTable 13.6TCP header. (continued)Byte OffsetBit OffsetDescription13.2IP-level network tapping351Chapter 13preﬁxed to the TCP header while computing the checksum only. Thispseudoheader contains the source address, the destination address,the protocol, and TCP length. (cid:1)Urgent pointer: Indicates the current value of the urgent pointer as anoffset from the sequence number in this segment. The urgent pointerpoints to the sequence number of the byte following the urgent data.This ﬁeld is only to be interpreted in segments with the URG controlbit set.(cid:1)Options: This contains vendor-speciﬁc IP options that may not beimplemented on all systems. It is guaranteed to end on a 32-bitboundary with zero padding.TCP is a connection-oriented protocol with built-in protection fromduplicated, dropped, and out-of order packets. This is done by explicitlyopening a connection between client and server and assigning each packet asequence number. The client will reply to the server with an acknowledg-ment for every packet sent; if sequence numbers go missing, appear twice,or appear out of order to the client, the acknowledgment will not be sent,and the server can take appropriate action.A TCP connection is established with a three-way handshake. Initially,the client sends a SYN request to the server, and the server replies with anACK response, to which the client replies with an ACK reply. Similarly, a TCPconnection is closed with a two-way handshake, where one party sends aFIN request to the other, which then replies with an ACK response.13.2.5UDP packets in detailIn UDP packets, immediately following the IP header is an 8-byte UDPheader (Table 13.7). A deﬁnitive description of UDP can be found inRFC 768.Table 13.7UDP header.Byte OffsetBit OffsetDescription11Source port31Destination port51Length7 1 Checksum35213.2IP-level network tappingUDP is the most basic data-carrying protocol that is valid for the Inter-net. It contains no protection against lost or duplicated packets, and there-fore is not applicable to media that require high integrity. It is acceptable forlive streaming audio and video formats.(cid:1)Source port: The port the UDP connection is made from, usually ahigh port.(cid:1)Destination port: The destination port, 161 for SNMP and 53 forDNS, etc.(cid:1)Length: The number of bytes following the header, plus the headeritself.(cid:1)Checksum: Computed as the 16-bit ones complement of the onescomplement sum of a pseudoheader of information from the IPheader, the UDP header, and the data, padded as needed with zerobytes at the end to make a multiple of 2 bytes. If the checksum is setto 0, then check summing is disabled. If the computed checksum is0, then this ﬁeld must be set to 0xFFFF.13.2.6DNS packets in detailIn DNS packets, immediately following the UDP header (port 53) is a 12-byte DNS header (Table 13.8). A deﬁnitive description of DNS can befound in RFC 1035.Table 13.8DNS header .Byte OffsetBit OffsetDescription11ID31Query or response32Query36Authoritative answer37 Truncation38Recursive31Availability42Set to 0 for future use45Result code13.2IP-level network tapping353Chapter 13(cid:1)ID: Used to correlate queries and responses.(cid:1)Query or response: Identiﬁes the message as a query or response.(cid:1)Query: Field that describes the type of message: 0 for standard query(name to address), 1 for inverse query (address to name), and 2 forserver status request.(cid:1)Authoritative answer: Identiﬁes the response as one made by anauthoritative name server when set to 1.(cid:1)Truncation: Indicates the message has been truncated when set to 1.(cid:1)Recursive: Set to 1 to request the name server to perform recursivesearches.(cid:1)Availability: Indicates if the name server can provide recursive service.(cid:1)Result code (RCODE): Used to indicate errors in processing the DNSquery.(cid:1)Question count: Indicates the number of entries in the question sec-tion.(cid:1)Answer count: Indicates the number of resource records in the answersection.(cid:1)Authority count: Indicates the number of name server resource recordsin the authority section.(cid:1)Additional count: Indicates the number of resource records in theadditional records section.DNS is used primarily to resolve IP addresses from domain names; how-ever, it can also be used to locate mail exchanges and so forth.  51Question count71Answer count91Authority count111Additional countTable 13.8DNS header (continued).Byte OffsetBit OffsetDescription35413.3Layer 2 network tapping13.3Layer 2 network tappingWhen you tap into (sniff) network trafﬁc at level 2, you receive not onlydata from other applications on your computer, but also from other appli-cations on different computers that are on the same network. Furthermore,you get more than just IP trafﬁc: you start to see ARP requests, NETBIOSpackets, 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 directlyfrom a network adapter. It was developed by the Politecnico di Torino(Italy) and can be distributed in binary format with the addition of a copy-right notice and disclaimer to your application. WinPCap can be down-loaded from http://winpcap.mirror.ethereal.com.The WinPCap DLL is designed for use with Visual C++ and is difﬁcult touse directly from C# or VB.NET. A wrapper of some description is requiredto import this library into your .NET application. One of the well-knownwrappers is an ActiveX control named PacketX, which is available fromwww.beesync.com. This software is shareware and, therefore, may not beapplicable for inclusion in freeware packages.Alternatively, I have prepared a wrapper (a C++ DLL) namedrvpacket.dll that is available for download at http://network.programming-in.net/downloads/rvPacket.zip. This DLL is open source and can be redistrib-uted as required. The DLL is only a basic implementation of WinPCap, butthe source code is available for those who are savvy in C++ to extend thefunctionality.Developers should be aware of the following known limitations toWinPCap:(cid:1)It will not operate correctly on Windows NT, 2000, or XP dial-upconnections.(cid:1)It may reset dial-up connections on Windows 95.(cid:1)Wireless network cards are not fully supported.(cid:1)Some implementations of VPN are not supported.13.3.1Using rvPacket and WinPCapThis ﬁrst example uses WinPCap with the rvPacket wrapper to display allnetwork trafﬁc on a textbox. It is normal for network data to pass through13.3Layer 2 network tapping355Chapter 13the adapter faster than it can appear on-screen, so there may be some timelag between data passing through the network and what is displayed on-screen. The ﬁrst step in developing this application is to download andinstall WinPCap from http://winpcap.mirror.ethereal.com, then to downloadrvPacket from http://network.programming-in.net/downloads/rvPacket.zip,and copy the DLL into your Windows system folder. Create a new project in Visual Studio .NET, and draw a textbox namedtbPackets with multiline set to true. Two buttons named btnStart andbtnStop are required. VB.NET developers will need to add a reference toMicrosoft.VisualBasic.Compatibility using Project→→→→Add References.Click on the Start button and add the following code:C#private void btnStart_Click(object sender, System.EventArgs e){  short Qid;  string packetBuffer;        short openSuccess;  short packetQueue;  short packetLen;  string rawAdapterDetails = "";  int posDefaultAdapter;  getAdapterNames(rawAdapterDetails);    Adapter="\\"; // default adapter  openSuccess = openAdapter("\\");  if (openSuccess != ERR_SUCCESS)   {    MessageBox.Show(    "Unable to start. Check WinPCap is installed");    return;  }  while(true)  {    packetQueue = checkPacketQueue(Adapter);    for (Qid = 1; Qid<packetQueue;Qid++)    {       packetBuffer = new        StringBuilder().Append       (' ',MAX_PACKET_SIZE).ToString();       packetLen = getQueuedPacket(packetBuffer);35613.3Layer 2 network tapping      packetBuffer = packetBuffer.Substring(0, packetLen);      tbPackets.Text = tbPackets.Text +            packetBuffer.Replace("\0"," ");      tbPackets.SelectionStart = tbPackets.Text.Length;      Application.DoEvents();    }    Application.DoEvents();  }}VB.NETPrivate Sub cmdStart_Click(ByVal eventSender As _       System.Object, ByVal eventArgs As _    System.EventArgs) Handles cmdStart.Click Dim Qid As Short Dim packetBuffer As String Dim adapters() As String Dim openSuccess As Short Dim packetQueue As Short Dim packetLen As Short Dim rawAdapterDetails As String Dim posDefaultAdapter As Short rawAdapterDetails = Space(MAX_ADAPTER_LEN) getAdapterNames(rawAdapterDetails) posDefaultAdapter = _ rawAdapterDetails.IndexOf(ADAPTER_DELIMITER) Adapter = rawAdapterDetails.Substring(0, posDefaultAdapter) openSuccess = openAdapter(Adapter)     If openSuccess <> ERR_SUCCESS Then  MsgBox("Unable to start. Check WinPCap is installed")  Exit Sub End If     Do   packetQueue = checkPacketQueue(Adapter)  For Qid = 1 To packetQueue   packetBuffer = Space(MAX_PACKET_SIZE)   packetLen = getQueuedPacket(packetBuffer)   packetBuffer = packetBuffer.Substring(0, packetLen)   tbPackets.Text = tbPackets.Text & Replace _         (packetBuffer, Chr(0), " ")   tbPackets.SelectionStart = Len(tbPackets.Text)13.3Layer 2 network tapping357Chapter 13   System.Windows.Forms.Application.DoEvents()  Next   System.Windows.Forms.Application.DoEvents() Loop End Sub  The code listed above performs two functions; ﬁrst, detects all of thenetwork adapters on the system, bearing in mind that computers can havemore than one means of connecting to a network, either by modem,Ethernet, or some other system. The getAdapterNames returns a list ofadapter names separated by a pipe character (“|”). Here the ﬁrst defaultadapter is used.  Network trafﬁc regularly arrives faster than it can be read and handledby an application; thus, the data is buffered internally in a linked-list struc-ture. The rvPacket library has two functions for reading this buffer, check-PacketQueue and getQueuedPacket. As the names suggest, the former is anonblocking function that retrieves the number of packets in the queue,and the latter will then read each packet in the queue sequentially. Thequeue is guaranteed not to grow in size between calls to checkPacketQueue,and no data on this buffer will be altered.The threading model is primitive, using nothing more than a DoEventscall to maintain responsiveness for the user interface. This has the side effectof pushing CPU usage to 100%, which looks unprofessional in a consumerproduct. In a more polished version, proper threading should be used. Note: All bytes of value 0 are replaced with a space character in the codeabove to help display the text on-screen; this serves no other purpose.Although not strictly necessary, the adapter should be closed after use. Ifit is not closed before the application is destroyed, then a memory leak willresult. More importantly, if two separate processes open the adapter at once,Windows will crash. Click on the Stop button, and enter the following code:C#private void btnStop_Click(object sender, System.EventArgs e){    closeAdapter(Adapter);}35813.3Layer 2 network tappingVB.NETPrivate Sub btnStop_Click(ByVal sender As _     System.Object, ByVal e As System.EventArgs) _     Handles btnStop.Click        closeAdapter(Adapter)  End Sub Several API declarations have to be made to make the rvPacket libraryaccessible. Insert this code directly after the form constructor:C#[DllImport("rvPacket.dll")]public static extern short getAdapterNames (string s);[DllImport("rvPacket.dll")]public static extern short openAdapter (string Adapter);[DllImport("rvPacket.dll")] public static extern shortcheckPacketQueue(string Adapter);[DllImport("rvPacket.dll")] public static extern short getQueuedPacket(string s);[DllImport("rvPacket.dll")] public static extern void closeAdapter(string Adapter);const short SIMULTANEOUS_READS = 10;const short MAX_ADAPTER_LEN = 512;const string ADAPTER_DELIMITER = "|";const short MAX_PACKET_SIZE = 10000;const short ERR_SUCCESS = 1;const short ERR_ADAPTER_ID= 2;const short ERR_INVALID_HANDLE= 3;const short ERR_INVALID_ADAPTER= 4;const short ERR_ALLOCATE_PACKET= 5;string Adapter = "";VB.NET Private Declare Function getAdapterNames Lib _         "rvPacket.dll" (ByVal s As String) As Short Private Declare Function openAdapter Lib _13.3Layer 2 network tapping359Chapter 13         "rvPacket.dll" (ByVal Adapter As String) As Short Private Declare Function checkPacketQueue Lib _         "rvPacket.dll" (ByVal Adapter As String) As Short Private Declare Function getQueuedPacket Lib _         "rvPacket.dll" (ByVal s As String) As Short Private Declare Sub closeAdapter Lib _         "rvPacket.dll" (ByVal Adapter As String)   Private Const SIMULTANEOUS_READS As Short = 10 Private Const MAX_ADAPTER_LEN As Short = 512 Private Const ADAPTER_DELIMITER As String = "|" Private Const MAX_PACKET_SIZE As Short = 10000 Private Const ERR_SUCCESS As Short = 1 Private Const ERR_ADAPTER_ID As Short = 2 Private Const ERR_INVALID_HANDLE As Short = 3 Private Const ERR_INVALID_ADAPTER As Short = 4 Private Const ERR_ALLOCATE_PACKET As Short = 5 Public Adapter As String  The rvPacket library is developed in unmanaged C++; therefore, theserather cryptic function declarations must be used, in the same way as theywere required to access the Windows API. The calling conventions for eachof the functions are identical: they all accept a string and return a number. GetAdapterNames is used to retrieve a list of network adapters present onthe system. One of these adapter names would be passed to openAdapter,which effectively begins the snifﬁng process on that network adapter. CheckPacketQueue returns the number of packets that are currently heldin the network card buffer. The GetQueued packet can then retrieve each ofthese packets one at a time. CloseAdapter stops the snifﬁng process and frees up the adapter for anyother process to use it. Immediately following the declarations are severalconstants that can be used within the code to better explain errors to users,and so forth. Finally, C# programmers require the following namespaces:C#using System.Text;using System.Runtime.InteropServices;36013.3Layer 2 network tappingTo test the application, make sure you are connected to an active net-work and that WinPCap and rvPacket have been installed. Run the programfrom Visual Studio .NET and press Start. If you open a browser and startusing the Web, you will see the HTTP sent and received in the textbox(Figure 13.3). Note: Data sent to the loop-back address 127.0.0.1 (localhost) does notactually register with WinPCap because it never actually moves through thenetwork card.13.3.2Using PacketX and WinPCapThe following example uses BeeSync’s packetX control (www.beesync.com)to illustrate the concept. There would be no problem in using rvPacket forthis example, but packetX is a useful alternative to know how to use. Theobject of the example is to log packets that match a certain criterion. In thiscase, only TCP/IP trafﬁc will be logged. TCP/IP trafﬁc can be isolated from raw network trafﬁc by checking twobytes in the packet. In an IP packet, the IP header follows the frame headerand, thus, will appear at the 14th byte in the packet. The ﬁrst byte in the IPheader will always be 69 when IPv4 is used with standard priority. The sec-Figure 13.3Frame-layer packetsniffer withrvPacket.13.3Layer 2 network tapping361Chapter 13ond byte to check is the protocol byte, the 10th byte in the IP header. Thisbyte will always be 6 when TCP/IP is used.You will need to have downloaded and installed both WinPCap andPacketX before starting to code this program. Start a new project in VisualStudio .NET, right-click on the toolbox on the left, and click CustomizeToolbox (or Add/Remove Items in Visual Studio .NET 2003), click theCOM tab, check PacketXCtrl Class, and press OK. Drag the new icononto the form. Draw a List View control named lvPackets onto the formas well as a Start button named btnStart.To start with, add a few column headers into the list view so that theresults it displays will be evenly tabulated. Add the following lines of codeto the load event:C#private void Form1_Load(object sender, System.EventArgs e){  lvPackets.Columns.Add("From", lvPackets.Width / 3,                          HorizontalAlignment.Left);  lvPackets.Columns.Add("To", lvPackets.Width / 3,                           HorizontalAlignment.Left);  lvPackets.Columns.Add("Size", lvPackets.Width / 3,                           HorizontalAlignment.Left);  lvPackets.View = View.Details;}VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load With lvPackets  .Columns.Add("From", .Width / 3, HorizontalAlignment.Left)  .Columns.Add("To", .Width / 3, HorizontalAlignment.Left)  .Columns.Add("Size", .Width / 3, HorizontalAlignment.Left)  .View = View.Details End WithEnd SubThe PacketX control will not start detecting packets until the Startmethod is called. This is to facilitate choosing nondefault adapters:C#private void btnStart_Click(object sender, System.EventArgs e)36213.3Layer 2 network tapping{  axPacketXCtrl1.Start();}VB.NETPrivate Sub btnStart_Click(ByVal eventSender As _    System.Object, ByVal eventArgs As System.EventArgs) _    Handles Command1.Click  axPacketXCtrl1.Start()End SubUnlike the polling mechanism of the rvPacket library, PacketX uses anevent to notify the host application of the arrival of packets. The eventdelivers an object that effectively derives from System.EventArgs, yet con-tains a PacketClass object that contains information on the packet con-tents and the exact time (with microsecond accuracy) the packet wasreceived.The packet contents are stored in a byte array named Data. This bytearray appears to .NET as a generic object; thus, to handle it without caus-ing type cast errors, Option Strict Off must be added as the ﬁrst line ofthe VB.NET code: C#private void axPacketXCtrl1_OnPacket(object sender, AxPACKETXLib._IPktXPacketXCtrlEvents_OnPacketEvent e){  short I;  string thisPacket;  string SourceIP;  string DestIP;  ListViewItem item = new ListViewItem();    thisPacket = "";  byte[] packetData = (byte[])e.pPacket.Data;  for (I = 0;I<e.pPacket.DataSize - 1;I++)  {    thisPacket = thisPacket + Convert.ToChar(packetData[I]);  }  if (packetData[14] == 69 && packetData[23] == 6)   {            SourceIP = packetData[26] + "." +      packetData[27] + "." +13.3Layer 2 network tapping363Chapter 13      packetData[28] + "." +      packetData[29];              DestIP = packetData[30] + "." +      packetData[31] + "." +      packetData[32] + "." +      packetData[33] + ".";        item.SubItems[0].Text = SourceIP;    item.SubItems.Add(DestIP);    item.SubItems.Add(e.pPacket.DataSize.ToString());    lvPackets.Items.Add(item);  }}VB.NETPrivate Sub axPacketXCtrl1_OnPacket(ByVal eventSender _     As System.Object, ByVal e As _     AxPACKETXLib.IPktXPacketXCtrlEvents_OnPacketEvent) _    Handles axPacketXCtrl1.OnPacket Dim I As Short Dim thisPacket As String Dim SourceIP As String Dim DestIP As String Dim item As New ListViewItem() thisPacket = "" For I = 0 To e.pPacket.DataSize - 1  thisPacket = thisPacket & Chr(eventArgs.pPacket.Data(I)) Next If e.pPacket.Data(14) = 69 And e.pPacket.Data(23) = 6 Then  SourceIP = e.pPacket.Data(26) & "." & _             e.pPacket.Data(27) & "." & + _             e.pPacket.Data(28) & "." & + _             e.pPacket.Data(29)  DestIP = e.pPacket.Data(30) & "." & _           e.pPacket.Data(31) & "." & + _           e.pPacket.Data(32) & "." & + _           e.pPacket.Data(33)  item.SubItems(0).Text = SourceIP  item.SubItems.Add(DestIP)36413.3Layer 2 network tapping  item.SubItems.Add(e.pPacket.DataSize)  lvPackets.Items.Add(item) End IfEnd SubThe actual network packet that is passed within the eventArgs or eparameter of the event takes the form of an object stored ine.pPacket.Data, which can be cast to a byte array (implicitly so, in the caseof VB.NET). This array is examined for key bytes, ﬁrst to ﬁlter out non-TCP/IP data and then to extract the Local and Remote IP addresses fromthe header. The extracted data is displayed on-screen in a list box. To test this application, run it from Visual Studio .NET and wait for aTCP/IP connection to take place on the network. Alternately, simply open-ing a browser should generate a TCP/IP connection to the server hostingyour browser’s home page.The example shown in Figure 13.4 is one single HTTP request betweentwo computers on a LAN. Note that it does not involve simply one packetfor the request and one for the response, but a fair amount of handshakingtakes place between client and server, before a connection is made. Thismay be worth knowing if, for instance, you were using a TCP trace to buildup statistics on user browsing habits. You cannot equate the number ofpackets to the amount of Web pages or emails sent. It might be better tocount occurrences of the string HTTP/1.1 or HELLO in outgoing packets onports 80 and 25, respectively, in this instance. Figure 13.4Frame-layer packetsniffer withPacketX.13.3Layer 2 network tapping365Chapter 13A busy network may produce an overwhelming number of packets, andit is likely that .NET will not be able to process 10 Mb of packets per sec-ond, as is commonplace on LANs. In this case, we can use hardware ﬁltersthat are built into network cards to cope with high-volume trafﬁc.To detect only packets destined for the local machine, we can apply thedirected packet hardware ﬁlter to the WinPCap driver by setting a parameterin the PacketX object with: PacketXCtrl1.Adapter.HWFilter = 1The default hardware ﬁlter is promiscuous mode, which will pass upevery packet seen by the network adapter. The rvPacket library only oper-ates in promiscuous mode. Wireless network cards cannot operate in pro-miscuous mode; therefore, a nonpromiscuous hardware ﬁlter (Table 13.9)must be applied for wireless devices.Table 13.9WinPCap hardware ﬁlters .Filter IDPurpose1Directed packets that contain a destination address equal to the station address of the NIC2Multicast address packets sent to addresses in the multicast address list4All multicast address packets, not just the ones enumerated in the mul-ticast address list8Broadcast packets16All source routing packets32Default, promiscuous mode; speciﬁes all packets64SMT packets that an FDDI NIC receives128All packets sent by installed protocols4096Packets sent to the current group address8192All functional address packets, not just the ones in the current func-tional address16384Functional address packets sent to addresses included in the current functional address32768NIC driver frames that a Token Ring NIC receives36613.4Physical network tappingWinPCap also has the capability to send and receive packets. This func-tionality can be accessed through Adapter.SendPacket, which could beuseful for generating non-IP-based packets, such as ARP requests or rawserver message block (SMB) data. These packets would not be routable overthe Internet, but they may have applications within company networks. 13.4Physical network tappingAlthough there would be no conceivable reason for software to read data atthis low level, it might be important to know whether the phone line isconnected to the computer or not.A program might also want to determine the type of connection thecomputer 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 begiven higher weighting in the index server(s) than 56K dial-up connections.This would ensure that new clients do not waste time attempting to con-nect to dial-up connections, which would be more than likely discon-nected, but instead run queries against more reliable, faster connections. The Adapter.LinkType and Adapter.LinkSpeed properties of PacketXprovide information on the network type (Table 13.10) and link speed inbits per second, respectively. Using WinPCap and PacketX may seem like overkill to determinewhether a computer is connected to the Internet, but you could, of course,always ping a well-known Web site address or use the getInternetCon-nectedState API function call.In .NET version 2 (Whidbey), the NetworkInformation class provides asimple mechanism to determine whether a computer is connected to thenetwork as follows:Table 13.10Link types. Link Type CodeMeaning0None1Ethernet (802.3)2Token Ring (802.5)3FDDI (Fiber Distributed Data Interface)4WAN (Wide Area Network)5LocalTalk13.4Physical network tapping367Chapter 13C#NetworkInformation netInfo = new NetworkInformation();If (netInfo.GetIsConnected() == true){ // connected to network}VB.NETDim netInfo as new NetworkInformation()If  (netInfo.GetIsConnected()= True) ' connected to networkend ifThe NetworkInformation class (Table 13.11) inherits from Sys-tem.Net.NetworkInformation. It contains a host of useful properties,which describe low-level network activities. The last ﬁve methods listed intable 13.11 may be alternatively retrieved from the GetNetworkParamsWindows API function. The ActiveUdpListener class, as returned by GetActiveUdpListeners,is descried in Table 13.12. This is equivalent to calling the GetUdpTableWindows API, or running NETSTAT -p udp -a from the command line.      6DIX (DEC- Intel - Xerox)7ARCNET (raw)8ARCNET (878.2)9ATM (Asynchronous Transfer Mode)10WirelessTable 13.11Signiﬁcant members of the NetworkInformation class .Method or PropertyPurposeAddressChangedSets AddressChangedEventHandler (Object,EventArgs) delegate.GetActiveUdpListenersLists all active UDP ports. Returns ActiveUdpListener[].Table 13.10Link types. (continued)Link Type CodeMeaning36813.4Physical network tappingThe IcmpV4Statistics class, as returned by GetIcmpV4Statistics, isdescribed in Table 13.13 (all properties return int64 unless otherwisespeciﬁed). This class is equivalent to the GetIcmpStatistics Windows IPHelper API.GetIcmpV4StatisticsRetrieves statistics of ping (ICMP) activity. Returns IcmpV4Statistics.GetIPStatisticsRetrieves statistics of IP activity. Returns IPStatistics.GetIsConnectedDetermines if the computer is connected to the network. Returns Boolean.GetNetworkInterfacesRetrieves information about connected network hardware. Returns NetworkInterface[].GetTcpConnectionsRetrieves statistics of TCP/IP activity. Returns TcpStatistics.GetUdpStatisticsRetrieves statistics of UDP/IP activity. Returns UdpStatistics.DhcpScopeNameGets the DHCP scope name. Returns String.DomainNameGets the locally registered domain name. Returns String.HostName  Gets the host name for the local computer. Returns String.IsWinsProxySpeciﬁes if the computer is acting as a WINS proxy. Returns Boolean.NodeType  Gets the NetBIOS node type of the computer. Returns NodeType (e.g., broadcast, P2P, mixed, hybrid).Table 13.12Signiﬁcant members of the ActiveUdpListener class.Method or PropertyPurposeLocalEndPointThe logical location of the port holding the active UDP connection. Returns IPEndPointTable 13.11Signiﬁcant members of the NetworkInformation class (continued).Method or PropertyPurpose13.4Physical network tapping369Chapter 13Table 13.13Signiﬁcant members of the IcmpV4Statistics class .Method or PropertyPurposeAddressMaskRepliesReceived  Gets the number of address mask replies receivedAddressMaskRepliesSent   Gets the number of address mask replies sentAddressMaskRequestsReceived   Gets the number of address mask requests receivedAddressMaskRequestsSent  Gets the number of address mask requests sentDestinationUnreachableMessagesReceived  Gets the number of destina-tion unreachable messages receivedDestinationUnreachableMessagesSent  Gets the number of destina-tion unreachable messages sentEchoRepliesReceived   Gets the number of echo replies receivedEchoRepliesSent   Gets the number of echo replies sentEchoRequestsReceived  Gets the number of echo requests receivedEchoRequestsSent   Gets the number of echo requests sentErrorsReceived   Gets the number of errors receivedErrorsSent   Gets the number of errors sentMessagesReceived   Gets the number of messages receivedMessagesSent   Gets the number of messages sentParameterProblemsReceived Gets the number of parame-ter problems receivedParameterProblemsSent   Gets the number of parame-ter problems sent37013.4Physical network tappingThe IPStatistics class, as returned by GetIPStatistics, is describedin Table 13.14 (all properties return int64 unless otherwise speciﬁed). Thisis equivalent to calling the GetIpStatistics Windows IP Helper API, orrunning NETSTAT -s from the command line.RedirectsReceived   Gets the number of redirects receivedRedirectsSent   Gets the number of redirects sentSourceQuenchesReceivedGets the number of source quenches receivedSourceQuenchesSent   Gets the number of source quenches sentTimeExceededMessagesReceived  Gets the number of time exceeded messages receivedTimeExceededMessagesSent   Gets the number of time exceeded messages sentTimestampRepliesReceived  Gets the number of times-tamp replies receivedTimestampRepliesSent   Gets the number of times-tamp replies sentTimestampRequestsReceived   Gets the number of times-tamp requests receivedTimestampRequestsSent  Gets the number of times-tamp requests sentTable 13.14Signiﬁcant members of the IPStatistics class .Method or PropertyPurposeDefaultTtl  Gets the default TTLForwardingEnabledDetermines if forwarding is enabled; returns BooleanInterfaces Gets the number of interfaces Table 13.13Signiﬁcant members of the IcmpV4Statistics class (continued).Method or PropertyPurpose13.4Physical network tapping371Chapter 13IPAddressesGets the number of IP addressesOutputPacketRequestsGets the number of output packet requestsOutputPacketRoutingDiscardsGets the number of output packet routing discardsOutputPacketsDiscardedGets the number of output packets discardedOutputPacketsWithNoRouteGets the number of output packets with no routePacketFragmentFailuresGets the number of packet fragment failuresPacketReassembliesRequiredGets the number of packet reassemblies requiredPacketReassemblyFailuresGets the number of packet reassembly failuresPacketReassemblyTimeoutRetrieves the packet reassem-bly timeoutPacketsFragmentedGets the number of packets fragmentedPacketsReassembledGets the number of packets reassembledReceivedPacketsGets the number of received packetsReceivedPacketsDeliveredGets the number of received packets deliveredReceivedPacketsDiscardedGets the number of received packets discardedReceivedPacketsForwardedGets the number of received packets forwardedReceivedPacketsWithAddressErrorsGets the number of received packets with address errorsReceivedPacketsWithHeadersErrorsGets the number of received packets with headers errorsTable 13.14Signiﬁcant members of the IPStatistics class (continued).Method or PropertyPurpose37213.4Physical network tappingThe NetworkInterface class, as returned by GetNetworkInterfaces, isdescribed in Table 13.15.ReceivedPacketsWithUnknownProtocolGets the number of received packets with unknown proto-colRoutesGets the number of routes usedTable 13.15Signiﬁcant members of the NetworkInterface class .Method or PropertyPurposeGetInterfaceStatisticsRetrieves information on network activity on the interface. Returns InterfaceStatistics.GetIPAddressInformationReturns information on the IP address assigned to the interface. Returns IPAddressInformation.GetIPv4Properties  Gets information concerning local IP routing, etc. Returns IPv4Properties.GetPhysicalAddressRetrieves the interface’s MAC address. Returns byte[].DescriptionA friendly name for the interface. Returns String.DnsEnabledDetermines if DNS is enabled on the interface. Returns Boolean.DynamicDnsEnabledDetermines if Dynamic DNS is enabled on the interface. Returns Boolean.Ipv4IndexDetermines the IP version 4 index on the interface. Returns int64.Ipv6IndexDetermines the IP version 6 index on the interface. Returns int64.IPVersionSupportedDetermines the IP version(s) supported by the interface. Returns IPVersionSupportedFlags.   IsConnected  Determines if the interface is connected to an active network. Returns Boolean.Table 13.14Signiﬁcant members of the IPStatistics class (continued).Method or PropertyPurpose13.4Physical network tapping373Chapter 13The InterfaceStatistics class, as returned by GetInterfaceStatis-tics, is described in Table 13.16 (all properties return int64 unless other-wise speciﬁed).Mtu   Determines the maximum transmission unit of the interface. Returns int64.NameGets a name for the interface. Returns string.OperationalStatusGets the operational status of the interface. Returns  OperationalStatus.Type  Determines the interface hardware. Returns InterfaceType  (e.g., modem, ISDN, ADSL, Ethernet, etc.).Table 13.16Signiﬁcant members of the InterfaceStatistics class .Method or PropertyPurposeBytesReceived  Gets the number of bytes receivedBytesSent   Gets the number of bytes sent IncomingPacketsDiscardedGets the number of incoming packets discarded IncomingPacketsWithErrors Gets the number of incoming packets with errors IncomingUnknownProtocolPackets  Gets the number of incoming unknown protocol packetsNonUnicastPacketsReceived  Gets the number of non-Unicast packets receivedNonUnicastPacketsSent   Gets the number of non-Unicast packets sentOutgoingPacketsDiscarded  Gets the number of outgoing packets discarded OutgoingPacketsWithErrors   Gets the number of outgoing packets with errors OutputQueueLength Gets the number of output queue length Table 13.15Signiﬁcant members of the NetworkInterface class (continued).Method or PropertyPurpose37413.4Physical network tappingThe IPAddressInformation class, as returned by GetIPAddressInfor-mation, is described in Table 13.17.The IPv4Properties class, as returned by GetIPv4Properties, isdescribed in Table 13.18. These properties may be alternatively ascertainedon an adapter-by-adapter basis through the GetAdaptersInfo Windows IPHelper API function.Speed   Gets the speed of the interfaceUnicastPacketsReceived  Gets the number of Unicast packets receivedUnicastPacketsSent  Gets the number of Unicast packets sentTable 13.17Signiﬁcant members of the IPAddressInformation class.Method or PropertyPurposeAddressGets the IP addressDnsEligibleDetermines if the address is eligible for DNSTransientDetermines if the address is transientTable 13.18Signiﬁcant members of the IPv4Properties class .Method or PropertyPurposeGetDhcpServerAddresses Retrieves the local DHCP server addresses. Returns IPAddress[].GetGatewayAddresses    Retrieves the local gateway addresses. Returns IPAddress[].  GetWinsServersAddresses Retrieves the local WINS servers addresses. Returns IPAddress[]. AutomaticPrivateAddressingActive Determines if automatic private addressing is active. Returns Boolean.Table 13.16Signiﬁcant members of the InterfaceStatistics class (continued).Method or PropertyPurpose13.4Physical network tapping375Chapter 13The TcpStatistics class, as returned by GetTcpStatistics, isdescribed in Table 13.19 (all properties return int64 unless otherwisestated). This is equivalent to calling the GetTcpTable Windows IP HelperAPI, or running NETSTAT -p tcp -a from the command line.AutomaticPrivateAddressingEnabledDetermines if automatic private addressing is enabled. Returns Boolean.DhcpEnabled   Determines if DHCP is enabled. Returns Boolean.RoutingEnabled   Determines if routing is enabled. Returns Boolean.UsesWins  Determines if the computer uses WINS. Returns Boolean.Table 13.19Signiﬁcant members of the TcpStatistics class .Method or PropertyPurposeConnectionsAccepted   Determines the number of connections accepted ConnectionsInitiated Determines the number of connections ini-tiatedCumulativeConnections  Determines the number of cumulative con-nectionsCurrentConnections   Determines the number of current connec-tionsErrorsReceived   Determines the number of errors receivedFailedConnectionAttempts Determines the number of failed connection attemptsMaximumConnectionsDetermines the maximum number of con-nectionsMaximumTransmissionTimeOutDetermines the maximum transmission time out Table 13.18Signiﬁcant members of the IPv4Properties class (continued).Method or PropertyPurpose37613.5Conclusion The UdpStatistics class, as returned by GetUdpStatistics, isdescribed in Table 13.20 (all properties return int64 unless otherwisestated). This is equivalent to the GetUdpStatistics Windows IP HelperAPI function.13.5ConclusionThis chapter has shown three different means to tap nonintrusively into thedata that ﬂows between computers. When local system trafﬁc monitoring isall that is required, then use of the pure .NET implementation is highly rec-ommended, but for an enterprisewide implementation, then PacketX com-MinimumTransmissionTimeOutDetermines the minimum transmission time out ResetConnectionsDetermines the number of reset connectionsSegmentsReceivedDetermines the number of segments received SegmentsResent Determines the number of segments resentSegmentsSent  Determines the number of segments sentSegmentsSentWithResetDetermines the number of segments sent with resetTable 13.20Signiﬁcant members of the UdpStatistics class.Method or PropertyPurposeDatagramsReceivedDetermines the number of datagrams receivedDatagramsSent   Determines the number of datagrams sent IncomingDatagramsDiscarded  Determines the number of incoming data-grams discarded  IncomingDatagramsWithErrorsDetermines the number of incoming data-grams with errorsUdpListeners  Determines the number of active UDP lis-tenersTable 13.19Signiﬁcant members of the TcpStatistics class (continued).Method or PropertyPurpose13.5Conclusion377Chapter 13bined with WinPCap is possibly the best option. Where ﬁnancial constraintsprevent the use of a third-party commercial component, then rvPacket willprobably point you in the right direction.It would be impossible to document the format of every protocol thatcould exist on a network, so only IP and TCP have been described in thischapter. Interested readers are advised to consult the relevant RFC for infor-mation on any speciﬁc protocol.The next chapter deals with a form of telecommunication that has beenwith us since the 1880s (i.e., the ubiquitous phone call); however, the chap-ter is taken from a Computer Telephony Integration (CTI) developer’s per-spective. Prepare to be introduced to the telephony API.This page intentionally left blank  379 14 Adding Digital Telephony 14.1Introduction If you call any large cinema looking for times for ﬁlms, you will undoubt-edly be forwarded to an automated system that tells you when each ﬁlm ison. This system is made possible by digital telephony.Computer Telephony Integration, or CTI, systems routinely cost$10,000 and upward for enterprise-scale systems. The high cost is largely aresult of the misconceived idea that any telephony system requires loads ofspecialized hardware and, thus, is out of reach for the humble developer. Infact, you can put a simple system together using no more than a cheapmodem. Any company that employs staff to answer phone calls can save moneyby implementing a CTI system. Such a system can be used to route calls todifferent departments automatically or to match a caller with customer IDand associated purchase history. This chapter is mainly devoted to one rather large code example built upin three sections. The ﬁrst section explains how to pick up and drop a call.The following section explains how to detect key presses on the remotehandset, and the chapter concludes with a demonstration of how to playback audio to the caller. Note:  You will need a voice modem and phone line to test the followingexamples. Access to a second phone (such as a mobile phone) is beneﬁcial. Calls made from any phone line may incur charges if the line is opened. 380 14.2 Basic telephony 14.2Basic telephony This chapter is focused on using the telephony API, but it is possible tocontrol a modem by issuing COM port commands. These will provide theability to dial telephone numbers and control the physical connection tothe phone line. Even if your modem is internal or connected via USB, it will always bemapped to a COM port. To discover the number of this COM port, youcan look at Start  →→→→ Control Panel  →→→→ phone and Modem options  →→→→ Modems.Under the Attached To tab will be the number of the COM port to whichthe modem is attached.Any command that is sent to this COM port will be interpreted by themodem. A list of common AT commands shown in Table 14.1.The responses the modem will send back shown in Table 14.2. Table 14.1 AT commands. AT CommandPurpose ATDT< phone number ><enter> Dials the speciﬁed phone number using touch-tone dialing. A comma in the number represents a pause, a W waits for a second dial tone, and an @ waits for a ﬁve-second silence. ATPT< phone number ><enter> Dials the speciﬁed number using pulse dialing. AT S0=< number > Picks up the line after the speciﬁed number of rings. +++  Drop line. Table 14.2 Modem responses . ResponseMeaning OK                       The command has executed without errors. CONNECT         A connection to the remote phone has been made. RING                  An incoming call is detected. NO CARRIER   No carrier signal has been detected (in GSM modems, this can mean that there is no network). ERROR The command is not understood. 14.2 Basic telephony381Chapter 14 To implement a simple phone dialer in .NET, open Visual Studio.NET and start a new Windows forms project. Right-click on the toolboxand click Customize Toolbox (or Add/Remove Items in Visual Studio.NET 2003). Click on the COM Controls tab, and then add the MicrosoftCommunications control ( MSCOMM.OCX ). Drag this onto the form, and setthe  comport  property to the COM port number to which your modem isconnected. Add a button to the form, named  btnPhone , click it, and addthis code: C# private void btnPhone_Click(object sender, System.EventArgs e){  axMSComm1.PortOpen=true;  axMSComm1.Output="ATDT00353877519575\r\n";} VB.NET Private Sub btnPhone_Click(ByVal sender As Object, _      ByVal e As System.EventArgs)  axMSComm1.PortOpen=True  axMSComm1.Output="ATDT00353877519575" + vbcrlfEnd Sub Note:  Running the code listed above may incur phone charges. It is advis-able to change the phone number listed (00353877519575) to some other, less expensive number.Only one program can control each COM port at a time. This code willfail if you are using the modem at the time. Several settings are associatedwith a COM port; in this case, however, the default parameters (9600 NO DIAL TONE There is no dial tone on the phone line. BUSY                  The remote end is too busy to take the call. NO ANSWER The remote end did not take the call. Table 14.2 Modem responses (continued). ResponseMeaning 382 14.3 Listening for incoming phone calls baud, no parity, 8 data bits, 1 stop bit—or “9600,n,8,1”) are suitable forcommunication with a modem. When the modem begins communicationat full speed, it will use a baud rate of 56 Kbps. This can be set using the settings  property of the Microsoft communications control. 14.3Listening for incoming phone calls You can only do a certain number of things with a modem by sending com-mands back and forth via a COM port. In order to develop serious applica-tions, you have to use the Telephony Application Programming Interface(TAPI). The TAPI libraries were designed with C++ in mind, not .NET, sothere is a steep learning curve. It is worthwhile to evaluate the various com-mercial components available before tinkering with low-level TAPI code. Afew interesting Web sites, such as  www.shrinkwrapvb.com and www.pron-exus.com,  contain a wealth of information on TAPI.The overall architecture of TAPI is modeled on a collection of phonelines that are connected to the computer. Not all of these phone lines arephysical connections. Some of them are software representations of phonelines used for various internal processes. Each phone line may be opened orclosed, which is analogous to a phone being on or off hook. An open phoneline does not necessarily incur charges, unless a call is active. When a phone line is open (off hook), it generates callbacks detailingany event that has happened on the line, such as an incoming call. A call-back is simply a function that is called asynchronously by an underlyingprocess. When an incoming call is detected, the callback will contain a handle thatcan be passed to a function that accepts the call. At this point, call charges areapplied to the line by the phone operator. Once the call is open, the modembehaves like a rudimentary audio device, which can play and receive basicaudio. The line can still generate callbacks, such as a line dropping or thedetection of the remote user pressing digits on the phone’s keypad. When the call is dropped, the line remains open, but the modem can nolonger function as an audio device. Phone charges will no longer be appliedwhen the call is dropped. Callbacks will be generated until the line is closed. Note:  Warning: If a line is not closed before the application exits, the com- puter may need to be restarted before the line can be reopened.  14.3 Listening for incoming phone calls383Chapter 14 Without further ado, here is the ﬁrst example of TAPI. This sampleapplication will enable you to open and close a phone line, as well as detectand accept incoming calls.Open a new project in Visual Studio .NET. Name the form  frmTapi ,and add to it three buttons:  btnStart ,  btnStop , and  btnAccept . You shouldalso include a textbox named  tbStatus  with  multiline  set to  true . Add a module named  TAPI , and add the following code. In C#, you adda class ﬁle instead of a module. Note that in C#, the class namespace isassumed to be  tapi1_cs , so substitute this for the name of your project. C# using System;using System.Runtime.InteropServices;namespace tapi1_cs{  public class TAPI  {    public static int hCall;    public static int hTAPI;    public static int lNumLines;    public static int hLine;    public static linedevcaps lpLineDevCaps;    public static frmTAPI userInterface;    public const int TAPIVERSION = 0x10004;    public const short LINECALLPRIVILEGE_OWNER = 0x4;    public const short LINECALLPRIVILEGE_MONITOR = 0x2;    public const short LINEMEDIAMODE_AUTOMATEDVOICE = 0x8;    public const int LINE_LINEDEVSTATE = 8;    public const int LINE_CALLSTATE = 2;    public const int LINECALLSTATE_OFFERING = 0x2;    public const int LINECALLSTATE_ACCEPTED = 0x4;    public const int LINECALLSTATE_DISCONNECTED = 0x4000;    public struct linedialparams    {      int dwDialPause;      int dwDialSpeed; 384 14.3 Listening for incoming phone calls       int dwDigitDuration;      int dwWaitForDialtone;    }    public struct lineextensionid    {      int dwExtensionID0;      int dwExtensionID1;      int dwExtensionID2;      int dwExtensionID3;    }    public struct linedevcaps    {      public int dwTotalSize;      public int dwNeededSize;      public int dwUsedSize;      public int dwProviderInfoSize;      public int dwProviderInfoOffset;      public int dwSwitchInfoSize;      public int dwSwitchInfoOffset;      public int dwPermanentLineID;      public int dwLineNameSize;      public int dwLineNameOffset;      public int dwStringFormat;      public int dwAddressModes;      public int dwNumAddresses;      public int dwBearerModes;      public int dwMaxRate;      public int dwMediaModes;      public int dwGenerateToneModes;      public int dwGenerateToneMaxNumFreq;      public int dwGenerateDigitModes;      public int dwMonitorToneMaxNumFreq;      public int dwMonitorToneMaxNumEntries;      public int dwMonitorDigitModes;      public int dwGatherDigitsMinTimeout;      public int dwGatherDigitsMaxTimeout;      public int dwMedCtlDigitMaxListSize;      public int dwMedCtlMediaMaxListSize;      public int dwMedCtlToneMaxListSize;      public int dwMedCtlCallStateMaxListSize; 14.3 Listening for incoming phone calls385Chapter 14       public int dwDevCapFlags;      public int dwMaxNumActiveCalls;      public int dwAnswerMode;      public int dwRingModes;      public int dwLineStates;      public int dwUUIAcceptSize;      public int dwUUIAnswerSize;      public int dwUUIMakeCallSize;      public int dwUUIDropSize;      public int dwUUISendUserUserInfoSize;      public int dwUUICallInfoSize;      public linedialparams MinDialParams;      public linedialparams MaxDialParams;      public linedialparams DefaultDialParams;      public int dwNumTerminals;      public int dwTerminalCapsSize;      public int dwTerminalCapsOffset;      public int dwTerminalTextEntrySize;      public int dwTerminalTextSize;      public int dwTerminalTextOffset;      public int dwDevSpecificSize;      public int dwDevSpecificOffset;       public int dwLineFeatures; // TAPI v1.4      public string bBytes;    }[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineAnswer (int hCall, ref string lpsUserUserInfo, int dwSize);  [DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineInitialize (ref int hTAPI,int     hInst, LineCallBackDelegate fnPtr ,     ref int szAppName, ref int dwNumLines);[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineNegotiateAPIVersion(int hTAPI,    int dwDeviceID,  int dwAPILowVersion,     int dwAPIHighVersion,     ref int lpdwAPIVersion,     ref lineextensionid lpExtensionID); 386 14.3 Listening for incoming phone calls [DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineOpen (int hLineApp, int       dwDeviceID, ref int lphLine, int dwAPIVersion,     int dwExtVersion, ref int dwCallbackInstance,    int dwPrivileges, int dwMediaModes,     ref int lpCallParams);[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineGetDevCaps (int hLineApp, int       dwDeviceID, int dwAPIVersion, int dwExtVersion,     ref linedevcaps lpLineDevCaps);[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineSetStatusMessages (int hLine,            int dwLineStates, int dwAddressStates);[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineDrop (int hCall, string lpsUserUserInfo, int dwSize);[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineShutdown(int hLineApp);}} VB.NET Option Strict OffOption Explicit OnModule VB_TAPI    Public LastTAPIEvent As Object    Public aditionalTAPIEventInfo As Object    Public hCall As Integer    Public hTAPI As Integer    Public lNumLines As Integer    Public hLine As Integer    Public lpLineDevCaps As linedevcaps    Public userInterface As frmTAPI    Public Const TAPIVERSION As Integer = &H10004    Public Const LINECALLPRIVILEGE_OWNER As Short = &H4S    Public Const LINECALLPRIVILEGE_MONITOR As Short = &H2S 14.3 Listening for incoming phone calls387Chapter 14     Public Const LINEMEDIAMODE_AUTOMATEDVOICE As Short = &H8S    Public Const LINE_LINEDEVSTATE = 8    Public Const LINE_CALLSTATE = 2    Public Const LINECALLSTATE_OFFERING = &H2    Public Const LINECALLSTATE_ACCEPTED = &H4    Public Const LINECALLSTATE_DISCONNECTED = &H4000    Structure linedialparams        Dim dwDialPause As Integer        Dim dwDialSpeed As Integer        Dim dwDigitDuration As Integer        Dim dwWaitForDialtone As Integer    End Structure    Structure lineextensionid        Dim dwExtensionID0 As Integer        Dim dwExtensionID1 As Integer        Dim dwExtensionID2 As Integer        Dim dwExtensionID3 As Integer    End Structure    Structure linedevcaps        Dim dwTotalSize As Integer        Dim dwNeededSize As Integer        Dim dwUsedSize As Integer        Dim dwProviderInfoSize As Integer        Dim dwProviderInfoOffset As Integer        Dim dwSwitchInfoSize As Integer        Dim dwSwitchInfoOffset As Integer        Dim dwPermanentLineID As Integer        Dim dwLineNameSize As Integer        Dim dwLineNameOffset As Integer        Dim dwStringFormat As Integer        Dim dwAddressModes As Integer        Dim dwNumAddresses As Integer        Dim dwBearerModes As Integer        Dim dwMaxRate As Integer        Dim dwMediaModes As Integer        Dim dwGenerateToneModes As Integer        Dim dwGenerateToneMaxNumFreq As Integer        Dim dwGenerateDigitModes As Integer 388 14.3 Listening for incoming phone calls         Dim dwMonitorToneMaxNumFreq As Integer        Dim dwMonitorToneMaxNumEntries As Integer        Dim dwMonitorDigitModes As Integer        Dim dwGatherDigitsMinTimeout As Integer        Dim dwGatherDigitsMaxTimeout As Integer        Dim dwMedCtlDigitMaxListSize As Integer        Dim dwMedCtlMediaMaxListSize As Integer        Dim dwMedCtlToneMaxListSize As Integer        Dim dwMedCtlCallStateMaxListSize As Integer        Dim dwDevCapFlags As Integer        Dim dwMaxNumActiveCalls As Integer        Dim dwAnswerMode As Integer        Dim dwRingModes As Integer        Dim dwLineStates As Integer        Dim dwUUIAcceptSize As Integer        Dim dwUUIAnswerSize As Integer        Dim dwUUIMakeCallSize As Integer        Dim dwUUIDropSize As Integer        Dim dwUUISendUserUserInfoSize As Integer        Dim dwUUICallInfoSize As Integer        Dim MinDialParams As linedialparams        Dim MaxDialParams As linedialparams        Dim DefaultDialParams As linedialparams        Dim dwNumTerminals As Integer        Dim dwTerminalCapsSize As Integer        Dim dwTerminalCapsOffset As Integer        Dim dwTerminalTextEntrySize As Integer        Dim dwTerminalTextSize As Integer        Dim dwTerminalTextOffset As Integer        Dim dwDevSpecificSize As Integer        Dim dwDevSpecificOffset As Integer        Dim dwLineFeatures As Integer ' TAPI v1.4        Dim bBytes As String    End Structure    Public Declare Function lineAnswer Lib "Tapi32" _       (ByVal hCall As Integer, ByRef lpsUserUserInfo _       As String, ByVal dwSize As Integer) As Integer    Public Declare Function lineInitialize Lib "Tapi32" _       (ByRef hTAPI As Integer, ByVal hInst As Integer, _ 14.3 Listening for incoming phone calls389Chapter 14        ByVal fnPtr As LineCallBackDelegate, ByRef _       szAppName As Integer, ByRef dwNumLines As _     Integer) As Integer    Public Declare Function lineNegotiateAPIVersion Lib _          "Tapi32" (ByVal hTAPI As Integer, ByVal _          dwDeviceID As Integer, ByVal dwAPILowVersion _           As Integer, ByVal dwAPIHighVersion As Integer, _          ByRef lpdwAPIVersion As Integer, ByRef _          lpExtensionID As lineextensionid) _          As Integer    Public Declare Function lineOpen Lib "Tapi32" _          (ByVal hLineApp As Integer, ByVal dwDeviceID _          As Integer, ByRef lphLine As Integer, ByVal _          dwAPIVersion As Integer, ByVal dwExtVersion _          As Integer, ByRef dwCallbackInstance _          As Integer, ByVal dwPrivileges As Integer, _          ByVal dwMediaModes As Integer, ByRef _          lpCallParams As Integer) As Integer    Public Declare Function lineGetDevCaps Lib "Tapi32" _          (ByVal hLineApp As Integer, ByVal dwDeviceID _          As Integer, ByVal dwAPIVersion As Integer, _          ByVal dwExtVersion As Integer, ByRef _          lpLineDevCaps As linedevcaps) As Integer    Public Declare Function lineSetStatusMessages Lib _          "Tapi32" (ByVal hLine As Integer, ByVal _            dwLineStates As Integer, ByVal _           dwAddressStates As Integer) As Integer    Public Declare Function lineDrop Lib "Tapi32" _           (ByVal hCall As Integer, ByVal lpsUserUserInfo _           As String, ByVal dwSize As _           Integer) As Integer    Public Declare Function lineShutdown Lib "Tapi32" _          (ByVal hLineApp As Integer) As IntegerEnd Module 390 14.3 Listening for incoming phone calls The code for the module may look daunting because these function def-initions are ported directly from the  TAPI.H  C++ code from the Windowsplatform SDK. It is not important to understand every parameter sent tothese API calls, but for the moment, Table 14.3 gives an overview of all theAPI calls involved.The core element of every TAPI application is the callback function  Lin-eCallBack . This is used to detect changes in the phone line, such as incom-ing calls, dropped calls, or key presses on the remote telephone keypad. Add the following code to the TAPI module: Note:  The purpose of the  LineCallBackDelegate  delegate is to ensure thatthe underlying telephony processes have something to call back to evenafter the program closes. This prevents Windows from crashing if your application does not shut down cleanly.  Table 14.3 Telephony API functions. API FunctionPurpose lineAnswer Picks up the phone when an incoming call is detected. This may incur phone charges. lineInitialize   Indicates the name of the callback function to TAPI, and retrieves the number of modems (vir-tual and physical) installed on the system. lineNegotiateAPIVersion Determines whether a modem can support a speci-ﬁed version of TAPI (i.e., 1.4 in this case). lineOpen Indicates to TAPI that the callback should now start receiving events for a speciﬁed modem. lineGetDevCaps Retrieves a host of technical information about a speciﬁed modem (see the  lineDevCaps  structure listed above). lineSetStatusMessages Indicates which, if any, events should be passed to the callback. lineDrop Shuts down a modem temporarily, dropping any active call. lineShutdown Shuts down a modem permanently, cleaning up any resources. 14.3 Listening for incoming phone calls391Chapter 14C#public delegate int LineCallBackDelegate(int dwDevice, int       dwMessage, int dwInstance, int dwParam1, int dwParam2,    int dwParam3);public static int LineCallBack(int dwDevice, int dwMessage, int dwInstance, int dwParam1, int dwParam2, int dwParam3){ string msgEvent=""; msgEvent = Convert.ToString(dwMessage); switch (dwMessage) {  case LINE_CALLSTATE:  switch(dwParam1)    {      case LINECALLSTATE_OFFERING:        msgEvent = "Incomming call";        hCall = dwDevice;        break;      case LINECALLSTATE_ACCEPTED:        msgEvent = "Call accepted";        break;      case LINECALLSTATE_DISCONNECTED:        msgEvent = "Call disconnected";        break;    }    break;  case LINE_LINEDEVSTATE:    msgEvent = "Ringing";    break;              }      userInterface.showMessage("Event: " + msgEvent + " Data:"                           + dwParam1 + "\r\n");  return 1;}VB.NETDelegate Function LineCallBackDelegate(ByVal dwDevice _As Integer, ByVal dwMessage As Integer, ByVal _dwInstance As Integer, ByVal dwParam1 As _ Integer, ByVal dwParam2 As Integer, ByVal dwParam3 _39214.3Listening for incoming phone callsAs Integer) As IntegerPublic Function LineCallBack(ByVal dwDevice As _Integer, ByVal dwMessage As Integer, ByVal dwInstance _As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 _As Integer, ByVal dwParam3 As Integer) As Integer        Dim msgEvent As String        msgEvent = CStr(dwMessage)        Select Case dwMessage            Case LINE_CALLSTATE                 Select Case dwParam1                    Case LINECALLSTATE_OFFERING                        msgEvent = "Incomming call"                        hCall = dwDevice                    Case LINECALLSTATE_ACCEPTED                         msgEvent = "Call accepted"                    Case LINECALLSTATE_DISCONNECTED                         msgEvent = "Call disconnected"                End Select            Case LINE_LINEDEVSTATE                msgEvent = "Ringing"            Case Else                msgEvent = dwMessage.ToString()        End Select        userInterface.tbStatus.Text += "Event: " & _             msgEvent & " Data:" & dwParam1 & vbCrLf    End FunctionTo explain the above code brieﬂy: Once a line has been opened, everyevent on that line will cause TAPI to make a call to this function. Theparameter dwMessage indicates broadly what has happened on the line, anddwParam1 deﬁnes the event more concisely.The most important message type is LINE_CALLSTATE. This indicates sig-niﬁcant state changes on the line. To determine the exact nature of the event, itis necessary to drill-down and look at dwParam1. When this parameter is set toLINECALLSTATE_OFFERING (0x2), a call has just been detected, and the handleto that call has been passed in dwDevice. This handle can be later passed tolineAnswer to pick up the phone. Other events such asLINECALLSTATE_ACCEPTED (0x4) and LINECALLSTATE_DISCONNECTED (0x4000)determine when a call becomes active and when the call is terminated. 14.3Listening for incoming phone calls393Chapter 14In some cases, the event can be assumed by looking at the dwMessageparameter only. A LINE_LINEDEVSTATE (0x8) event is most likely to be theringing sound from an incoming call, but it could also be that the phone lineis out of service, indicated by a dwParam1 of LINEDEVSTATE_OUTOFSERVICE(0x80), or that the phone line is under maintenance, indicated byLINEDEVSTATE_MAINTENANCE (0x100). Because this type of occurrence is rare,and a computer program can hardly resolve the problem, the event can beignored.At this point, the user interface should have already been prepared withthree buttons named btnStart, btnStop, and btnAccept on the form. Alarge textbox named tbStatus is required. The multiline property shouldbe set to true. Click the Start button and enter the following code:C#private void btnStart_Click(object sender, System.EventArgs e){   startModem();}VB.NET Private Sub btnStart_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles btnStart.Click        startModem() End SubClick the Stop button and enter the following code:C#private void btnStop_Click(object sender, System.EventArgs e){  stopModem();}VB.NET Private Sub btnStop_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles btnStop.Click39414.3Listening for incoming phone calls        stopModem() End SubClick the Accept button and enter the following code:C#private void btnAcceptCall_Click(object sender, System.EventArgs e){  acceptCall();}VB.NET Private Sub btnAccept_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles btnAccept.Click        acceptCall() End SubC# developers will also require the following function:C#public void showMessage(string message){  tbStatus.Text += message;}The reason for the extra function is that in VB.NET the TAPI moduleexposes functions and types contained within it globally. In C#, a class isused to hold the functions and types; therefore, any calls to these functionsmust be through a reference to the class. Because the functions are static,the only programmatic difference is the TAPI preﬁx; however, the classneeds to have a reference to the form so that it can display text on the screenwhen the TAPI callback occurs. A computer may have more than one modem attached and will almostcertainly have a few virtual modems, which are used for various other inter-nal purposes. Voice modems are much more useful when it comes to tele-phony applications, but a data modem can still pick up and drop calls, evenif it cannot communicate with a human user once the line is active. Thislimited functionality may be all that is required, however, if, for instance,14.3Listening for incoming phone calls395Chapter 14the computer needs to do only one task in response to an incoming phonecall, such as connecting to the Internet or rebooting.This code is designed to open the ﬁrst line it can ﬁnd that is capable ofdetecting incoming calls. A more advanced system would select a voicemodem over a data modem by selecting a modem with the lowest accept-able lMediaMode. A voice modem can work with a media mode set toLINEMEDIAMODE_INTERACTIVEVOICE (4 hex), whereas a data modem willgenerally only use LINEMEDIAMODE_DATAMODEM (10 hex). Hybrid modems doexist, so the code below will scan all media modes from 1 to 100.C#public void startModem(){  int nError=0;  TAPI.lineextensionid lpExtensionID = new  TAPI.lineextensionid();  int lUnused=0;  int lLineID=0;        int lNegVer=0;  long lPrivilege=0;  long lMediaMode=0;  IntPtr HInstance=(IntPtr)0;    lPrivilege = TAPI.LINECALLPRIVILEGE_OWNER +                TAPI.LINECALLPRIVILEGE_MONITOR;  lMediaMode = 4;          Module thisModule;        thisModule =  Assembly.GetExecutingAssembly().GetModules()[0];  HInstance = Marshal.GetHINSTANCE(thisModule);  TAPI.LineCallBackDelegate callback = new   TAPI.LineCallBackDelegate(TAPI.LineCallBack);  int Unused = 0;  nError = TAPI.lineInitialize(ref TAPI.hTAPI,  HInstance.ToInt32(),     callback, ref Unused, ref TAPI.lNumLines);              for (lLineID = 0;lLineID<TAPI.lNumLines;lLineID++)  {    nError = TAPI.lineNegotiateAPIVersion(TAPI.hTAPI,39614.3Listening for incoming phone calls    lLineID,        TAPI.TAPIVERSION,TAPI.TAPIVERSION,         ref lNegVer, ref lpExtensionID);    do    {      nError = TAPI.lineOpen(TAPI.hTAPI, lLineID,       ref TAPI.hLine,       lNegVer, lUnused, ref lUnused,        (int)lPrivilege, (int)lMediaMode, ref lUnused);       lMediaMode ++;    } while (nError < 0 && lMediaMode < 100);    if (nError == 0) break;  }  TAPI.lpLineDevCaps.dwTotalSize =  Marshal.SizeOf(TAPI.lpLineDevCaps);  TAPI.lpLineDevCaps.bBytes = new      StringBuilder().Append(' ',2000).ToString();  TAPI.lineGetDevCaps(TAPI.hTAPI, lLineID, lNegVer, lUnused,      ref TAPI.lpLineDevCaps);  TAPI.lineSetStatusMessages(TAPI.hLine,      TAPI.lpLineDevCaps.dwLineStates, 0);  }VB.NETPublic Sub startModem() Dim nError As Integer Dim lpExtensionID As lineextensionid Dim lUnused As Integer Dim lLineID As Integer Dim i As Short Dim lNegVer As Integer Dim lPrivilege As Long Dim lMediaMode As Long lPrivilege = LINECALLPRIVILEGE_OWNER + _     LINECALLPRIVILEGE_MONITOR lMediaMode = 4nError = lineInitialize(hTAPI, _Microsoft.VisualBasic.Compatibility.VB6.GetHInstance.ToInt32, _AddressOf LineCallBack, 0, lNumLines)14.3Listening for incoming phone calls397Chapter 14For lLineID = 0 To lNumLines  nError = lineNegotiateAPIVersion(hTAPI, _  lLineID,TAPIVERSION,TAPIVERSION, _        lNegVer, lpExtensionID)   Do    nError = lineOpen(hTAPI, lLineID, hLine, lNegVer, _          lUnused, lUnused, lPrivilege, lMediaMode, 0)   lMediaMode = lMediaMode + 1   Loop Until nError >= 0 Or lMediaMode = 100   If nError = 0 Then Exit For  Next  lpLineDevCaps.dwTotalSize = Len(lpLineDevCaps)  lpLineDevCaps.bBytes = Space(2000)  lineGetDevCaps(hTAPI, lLineID, lNegVer, lUnused, _     lpLineDevCaps)  lineSetStatusMessages(hLine, lpLineDevCaps.dwLineStates, 0)End SubIt is important to shut down the line after use because no other programcan use the modem until the line has been closed. If you close your programbefore the line is closed, there may be problems reopening the line, and youmay have to restart your computer.C#public void stopModem(){  int nError;  nError = TAPI.lineShutdown(TAPI.hTAPI);}VB.NETPublic Sub stopModem()  Dim nError As Integer  nError = lineShutdown(hTAPI)End SubWhenever an incoming call is detected, the callback function will set apublic variable named hCall to a reference number (a handle) that TAPIrecognizes. When this handle is passed to lineAnswer, the phone line is39814.3Listening for incoming phone callsopened. The modem is then in a position to send and receive audio datafrom the remote user, provided the modem supports that functionality. C#public void acceptCall(){   int nError;   string szUnused="";   nError = TAPI.lineAnswer(TAPI.hCall, ref szUnused, 0);}VB.NETPublic Sub acceptCall()   Dim nError As Integer   nError = lineAnswer(hCall, "", 0)End SubBecause this is a demonstration program, it is worthwhile to display inreal time what is happening to the callback function. A reference to theform is stored in a public variable so that the callback function can use thatreference to display status messages in tbStatus. C#private void frmTAPI_Load(object sender, System.EventArgs e){  TAPI.userInterface = this;}VB.NETPrivate Sub frmTAPI_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load        userInterface = MeEnd SubVB.NET developers will need to set option strict off at the top oftheir code and include a reference to Microsoft Visual Basic .NET Compat-ibility Runtime. C# developers will require the following namespaces, whereas VB.NETdevelopers will need to add a reference to the Microsoft.VisualBa-sic.Compatibility assembly in Project→→→→Add References.14.4DTMF tones399Chapter 14C#using System.Runtime.InteropServices;using System.Text;using System.Reflection;To test this program, run it from Visual Studio .NET and press startMo-dem (see Figure 14.1). Connect your modem to a phone line. With a sec-ond phone, dial the number of the phone line that is connected to yourmodem. When an incoming call is detected and displayed –on-screen, youcan press acceptCall. You will hear the ringing stop once the line is open.Hang up, or press stopModem to disconnect the call.14.4DTMF tonesDual-tone modulated frequency (DTMF) is a way of encoding a numberinto an audible sound composed of two sine waves played simultaneously.These sounds are generated when someone presses a digit on a phone’s key-pad. This is particularly useful for automated phone conversations, such as“Press 1 if you have a billing inquiry. Press 2 if you require technical sup-port,” and so on.These sounds are decoded by the modem hardware and passed up to theTAPI callback as an event with dwMessage set to LINE_MONITORDIGITS (9hex). The digit pressed is being held in dwParam1.To use DTMF within a TAPI application, a few small changes need tobe made. First, add a new API deﬁnition and two new constants to theTAPI module thus:C#public const short LINEDIGITMODE_DTMF = 0x2;Figure 14.1Basic TAPI call-receiverapplication.40014.4DTMF tonespublic const short LINE_MONITORDIGITS = 9;[DllImport("Tapi32.dll",SetLastError=true)]public static extern int lineMonitorDigits(int hCall,int dwDigitModes);VB.NETPublic Const LINEDIGITMODE_DTMF As Short = &H2SPublic Const LINE_MONITORDIGITS = 9Public Declare Function lineMonitorDigits Lib "Tapi32" _(ByVal hCall As Integer, ByVal dwDigitModes As _Integer) As IntegerThen add a new case to the callback function:C#public static int LineCallBack(...){ ... switch (dwMessage) {  ...    case LINE_MONITORDIGITS:   msgEvent = "DTMF";       break; }     ...}VB.NETPublic Function LineCallBack(...) As Integer ... Select Case dwMessage ...  Case LINE_MONITORDIGITS   MsgEvent = "DTMF"      End SelectThen add a call to lineMonitorDigits to acceptCall:14.5Audio playback401Chapter 14C#public void acceptCall(){   int nError;   string szUnused="";   nError = TAPI.lineAnswer(TAPI.hCall, ref szUnused, 0);   TAPI.lineMonitorDigits(TAPI.hCall,   TAPI.LINEDIGITMODE_DTMF);}VB.NETPublic Sub acceptCall()   Dim nError As Integer   nError = lineAnswer(hCall, "", 0)   lineMonitorDigits(hCall, LINEDIGITMODE_DTMF)End Sub14.5       Audio playbackPlaying audio back through a voice modem is the core feature of any CTIsystem. The following example demonstrates how to send a prerecordedwave ﬁle as audio to a standard telephone handset. Using prerecorded mes-sages should be adequate in most situations, where even dynamic data suchas 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 prerecordaudio snippets, a speech synthesizer such as such as the text-to-speech appli-cation contained in the Samples\CSharp\SimpleTTS folder of MicrosoftSAPI 5.1 (Speech Application Programming Interface) could be used. This,however, is beyond the scope of this book. To illustrate the principle of audio playback, the ﬁrst example demon-strates how to play a wave (.wav) ﬁle through your sound card. The sametechnique is then applied to playing audio over an active phone call. Thecode required to play a simple wave ﬁle may seem like overkill. It is true thatif all you require is to play a sound through the sound card, you should lookat API calls like sndPlaySound, or if sound recording is required, then themciSendString API should be of interest. The reason behind using low-levelcode to play a wave ﬁle though a sound card is that this method can be easilyadapted to play audio directly through the phone line, albeit at lesser quality.40214.5Audio playbackOpen a new project in Visual Studio .NET, and add a new module.Type the following code into it. In C#, you will create a new class. Ensurethat the namespace is the same as that used in your form; here it is assumedto be audio. You may replace this as necessary.C#namespace audio{  public class audio  {    public static WAVEHDR whdr;    public static WAVEFORMAT format_wave;    public static WAVEHDR outHdr;    public static int bufferIn;    public static int numSamples;    public static int hWaveOut;    public const short MMIO_READ = 0x0;    public const int CALLBACK_FUNCTION  = 0x30000;    public const short WAVE_MAPPED  = 0x4;    public const short MMIO_FINDCHUNK  = 0x10;    public const short MMIO_FINDRIFF  = 0x20;    public struct MMCKINFO    {      public int ckid;       public int ckSize;      public int fccType;      public int dwDataOffset;      public int dwFlags;    }       public struct mmioinfo    {      public int dwFlags;      public int fccIOProc;      public int pIOProc;      public int wErrorRet;      public int htask;      public int cchBuffer;      public string pchBuffer;14.5Audio playback403Chapter 14      public string pchNext;      public string pchEndRead;      public string pchEndWrite;      public int lBufOffset;      public int lDiskOffset;      public string adwInfo;      public int dwReserved1;      public int dwReserved2;      public int hmmio;    }      public struct WAVEFORMAT    {      public short wFormatTag;      public short nChannels;      public int nSamplesPerSec;      public int nAvgBytesPerSec;      public short nBlockAlign;      public short wBitsPerSample;      public short cbSize;    }    public struct WAVEHDR    {      public int lpData;      public int dwBufferLength;      public int dwBytesRecorded;      public int dwUser;      public int dwFlags;      public int dwLoops;      public int lpNext;      public int Reserved;    }[DllImport("winmm.dll",SetLastError=true)]public static extern int waveOutWrite(int hWaveOut,    ref WAVEHDR lpWaveOutHdr, int uSize);  [DllImport("winmm.dll",SetLastError=true)]public static extern int waveOutPrepareHeader(int hWaveIn,    ref WAVEHDR lpWaveInHdr, int uSize);[DllImport("winmm.dll",SetLastError=true)]40414.5Audio playbackpublic static extern int mmioRead (int hmmio,    int pch, int cch);  [DllImport("winmm.dll",SetLastError=true)]public static extern int waveOutOpen(ref int lphWaveIn, int    uDeviceID, ref WAVEFORMAT lpFormat, int dwCallback,    int dwInstance,int dwFlags);  [DllImport("kernel32.dll",SetLastError=true)]public static extern int GlobalAlloc (int wFlags, int dwBytes);  [DllImport("kernel32.dll",SetLastError=true)]public static extern int GlobalLock (int hmem);  [DllImport("winmm.dll",SetLastError=true)]public static extern int mmioAscend (int hmmio, ref MMCKINFO lpck, int uFlags);  [DllImport("kernel32.dll",SetLastError=true)]public static extern int GlobalFree (int hmem);  [DllImport("winmm.dll",SetLastError=true)] public static extern int mmioOpenA (string szFileName, ref mmioinfo lpmmioinfo, int dwOpenFlags);[DllImport("winmm.dll",SetLastError=true)] public static extern int mmioDescend (int hmmio, ref MMCKINFO lpck, int x, int uFlags);[DllImport("winmm.dll",SetLastError=true)] public static extern int mmioRead(int hmmio, ref WAVEFORMAT pch, int cch);  [DllImport("winmm.dll",SetLastError=true)] public static extern int mmioClose(int hmmio, int uFlags);[DllImport("winmm.dll",SetLastError=true)] public static extern int mmioStringToFOURCCA (string sz, int uFlags);  [DllImport("winmm.dll",SetLastError=true)] 14.5Audio playback405Chapter 14public static extern int mmioDescend (int hmmio, ref MMCKINFO lpck, ref MMCKINFO lpckParent, int uFlags);}}VB.NETOption Strict OffOption Explicit OnModule modAudio Public whdr As WAVEHDR  Public format_wave As WAVEFORMAT  Public outHdr As WAVEHDR Public bufferIn As Integer Public numSamples As Integer Public hWaveOut As Integer  Public Const MMIO_READ As Short = &H0s Public Const CALLBACK_FUNCTION As Integer = &H30000 Public Const WAVE_MAPPED As Short = &H4s Public Const MMIO_FINDCHUNK As Short = &H10s Public Const MMIO_FINDRIFF As Short = &H20s Structure MMCKINFO   Dim ckid As Integer   Dim ckSize As Integer   Dim fccType As Integer   Dim dwDataOffset As Integer   Dim dwFlags As Integer End Structure   Structure mmioinfo   Dim dwFlags As Integer   Dim fccIOProc As Integer   Dim pIOProc As Integer   Dim wErrorRet As Integer   Dim htask As Integer   Dim cchBuffer As Integer   Dim pchBuffer As String   Dim pchNext As String   Dim pchEndRead As String   Dim pchEndWrite As String40614.5Audio playback   Dim lBufOffset As Integer   Dim lDiskOffset As Integer   Dim adwInfo As String   Dim dwReserved1 As Integer   Dim dwReserved2 As Integer   Dim hmmio As Integer End Structure   Structure WAVEFORMAT   Dim wFormatTag As Short   Dim nChannels As Short   Dim nSamplesPerSec As Integer   Dim nAvgBytesPerSec As Integer   Dim nBlockAlign As Short   Dim wBitsPerSample As Short   Dim cbSize As Short End Structure   Structure WAVEHDR   Dim lpData As Integer   Dim dwBufferLength As Integer   Dim dwBytesRecorded As Integer   Dim dwUser As Integer   Dim dwFlags As Integer   Dim dwLoops As Integer   Dim lpNext As Integer   Dim Reserved As Integer End Structure   Declare Function waveOutWrite Lib "winmm.dll" (ByVal _ hWaveOut As Integer, ByRef lpWaveOutHdr As WAVEHDR, _ ByVal uSize As Integer) As Integer   Declare Function waveOutPrepareHeader Lib "winmm.dll" _ (ByVal hWaveIn As Integer, ByRef lpWaveInHdr As _ WAVEHDR, ByVal uSize As Integer) As Integer   Declare Function mmioRead Lib "winmm.dll" (ByVal hmmio _ As Integer, ByVal pch As Integer, ByVal cch As _ Integer) As Integer  14.5Audio playback407Chapter 14 Declare Function waveOutOpen Lib "winmm.dll" (ByRef _ lphWaveIn As Integer, ByVal uDeviceID As Integer, _ ByRef lpFormat As WAVEFORMAT, ByVal dwCallback As _ Integer, ByVal dwInstance As Integer, ByVal dwFlags _ As Integer) As Integer   Declare Function GlobalAlloc Lib "kernel32" (ByVal _ wFlags As Integer, ByVal dwBytes As Integer) As Integer   Declare Function GlobalLock Lib "kernel32" (ByVal hmem _ As Integer) As Integer   Declare Function mmioAscend Lib "winmm.dll" (ByVal _ hmmio As Integer, ByRef lpck As MMCKINFO, ByVal uFlags _ As Integer) As Integer   Declare Function GlobalFree Lib "kernel32" (ByVal hmem _ As Integer) As Integer   Declare Function mmioOpen Lib "winmm.dll"  Alias _ "mmioOpenA"(ByVal szFileName As String, ByRef _ lpmmioinfo As mmioinfo, ByVal dwOpenFlags As _ Integer) As Integer Declare Function mmioDescendParent Lib "winmm.dll" _ Alias "mmioDescend"(ByVal hmmio As Integer, ByRef lpck _ As MMCKINFO, ByVal x As Integer, ByVal uFlags As _ Integer) As Integer   Declare Function mmioReadFormat Lib "winmm.dll"  Alias _ "mmioRead"(ByVal hmmio As Integer, ByRef pch As _  WAVEFORMAT, ByVal cch As Integer) As Integer   Declare Function mmioClose Lib "winmm.dll" (ByVal _ hmmio As Integer, ByVal uFlags As Integer) As Integer   Declare Function mmioStringToFOURCC Lib "winmm.dll" _ Alias "mmioStringToFOURCCA"(ByVal sz As String, ByVal _ uFlags As Integer) As Integer   Declare Function mmioDescend Lib "winmm.dll" (ByVal _ hmmio As Integer, ByRef lpck As MMCKINFO, ByRef _40814.5Audio playback lpckParent As MMCKINFO, ByVal uFlags As Integer) As IntegerEnd ModuleThis code is ported from the C++ prototypes, so it may appear to becomplex. Again, it is not necessary to know every parameter passed to eachTable 14.4Windows Multimedia API functions .waveOutPrepareHeader Indicates the format of the raw audio data to the wave-out device, so that it can play the sound at the correct speed and knows its format mmioRead Reads data from an audio source into memoryGlobalAlloc Allocates a block of memory of a speciﬁed sizeGlobalLock Prevents other processes from using a speciﬁed block of memoryGlobalFree Releases a block of memorymmioOpen Opens an audio source (e.g., a wave ﬁle)mmioReadFormat Retrieves the format of an audio source and details including bit rate, stereo/mono, quality, etc.mioStringToFOURCC Converts a null-terminated string to a four-charac-ter codemmioDescend Descends into a chunk of a RIFF ﬁle that was opened by using the mmioOpen function; can also search for a given chunkwaveOutOpen Opens an audio output devicemmioAscend Ascends out of a chunk in a RIFF ﬁle descended into with the mmioDescend function or created with the mmioCreateChunk functionmmioDescendParent Descends into a chunk of a RIFF ﬁle that was opened by using the mmioOpen function; can also search for a given chunkmmioClose Closes an audio input or output devicewaveOutWrite Tells the audio output device to begin playing the sound 14.5Audio playback409Chapter 14of these API calls, but Table 14.4 provides a synopsis of the functionsinvolved.This application will load a wave ﬁle from disk into memory and thenplay it through the sound card on request. Loading a wave ﬁle into memoryis done in two stages. The ﬁrst is where the format of the audio is extractedfrom the wave ﬁle. The audio format includes details about the quality (16-bit or 8-bit), bit rate (44 kbps for CD quality), and whether the audio ismono or stereo. The audio format is stored in a public variable namedformat_wave.The next step is to pull the data segment of the wave ﬁle into memory. Awave ﬁle can be several megabytes in size, so for better performance, thememory is allocated directly from the heap using GlobalAlloc. The waveﬁle is then read into this memory using mmioRead. Once the operation iscomplete, the ﬁle is closed. Add the following code to the module:C#public static void LoadFile(ref string inFile){  int hmem = 0;  MMCKINFO mmckinfoParentIn = new MMCKINFO();  MMCKINFO mmckinfoSubchunkIn = new MMCKINFO();  int hmmioIn = 0;  mmioinfo mmioinf = new mmioinfo();  mmioinf.adwInfo =   (new StringBuilder()).Append(' ',4).ToString();  hmmioIn = mmioOpenA(inFile, ref mmioinf, MMIO_READ);  if (hmmioIn == 0) return;  mmioDescend(hmmioIn, ref mmckinfoParentIn, 0,   MMIO_FINDRIFF);  mmckinfoSubchunkIn.ckid = mmioStringToFOURCCA("fmt", 0);  mmioDescend(hmmioIn, ref mmckinfoSubchunkIn,       ref mmckinfoParentIn, MMIO_FINDCHUNK);  mmioRead(hmmioIn, ref format_wave,      Marshal.SizeOf(format_wave));  mmioAscend(hmmioIn, ref mmckinfoSubchunkIn, 0);  mmckinfoSubchunkIn.ckid = mmioStringToFOURCCA("data", 0);  mmioDescend(hmmioIn, ref mmckinfoSubchunkIn,       ref mmckinfoParentIn,      MMIO_FINDCHUNK);41014.5Audio playback  GlobalFree(hmem);  hmem = GlobalAlloc(0x40, mmckinfoSubchunkIn.ckSize);  bufferIn = GlobalLock(hmem);  mmioRead(hmmioIn, bufferIn, mmckinfoSubchunkIn.ckSize);  numSamples =       mmckinfoSubchunkIn.ckSize / format_wave.nBlockAlign;  mmioClose(hmmioIn, 0);}VB.NET  Sub LoadFile(ByRef inFile As String) Dim hmem As Integer Dim mmckinfoParentIn As MMCKINFO Dim mmckinfoSubchunkIn As MMCKINFO Dim hmmioIn As Integer Dim mmioinf As mmioinfo mmioinf.adwInfo = Space(4) hmmioIn = mmioOpen(inFile, mmioinf, MMIO_READ) If hmmioIn = 0 Then Exit Sub mmioDescendParent(hmmioIn, mmckinfoParentIn, 0, _ MMIO_FINDRIFF) mmckinfoSubchunkIn.ckid = mmioStringToFOURCC("fmt", 0) mmioDescend(hmmioIn, mmckinfoSubchunkIn, _ mmckinfoParentIn, MMIO_FINDCHUNK) mmioReadFormat(hmmioIn, format_wave, Len(format_wave)) mmioAscend(hmmioIn, mmckinfoSubchunkIn, 0) mmckinfoSubchunkIn.ckid = mmioStringToFOURCC("data", 0) mmioDescend(hmmioIn, mmckinfoSubchunkIn, _ mmckinfoParentIn, MMIO_FINDCHUNK) GlobalFree(hmem) hmem = GlobalAlloc(&H40S, mmckinfoSubchunkIn.ckSize) bufferIn = GlobalLock(hmem) mmioRead(hmmioIn, bufferIn, mmckinfoSubchunkIn.ckSize) numSamples = mmckinfoSubchunkIn.ckSize / _ format_wave.nBlockAlign mmioClose(hmmioIn, 0)End SubOnce the wave ﬁle is in memory, the sound card can be instructed toplay the audio with a call to this next function, named Play. This function14.5Audio playback411Chapter 14is asynchronous and can be called more than once during the playing of asound clip, provided the hardware supports it. The sound card will fetchthe audio from memory as required using a process known as direct mem-ory access (DMA).Because the audio format is stored in public variables, that data needs tobe transferred to the sound card such that it can correctly play back thesounds at the right speed and quality. Once waveOutPrepareHeader has setthe sound card up, waveOutWrite then starts the sound playing.C#public static void Play(short soundcard){  int rc = 0;  int lFlags = 0;  lFlags = CALLBACK_FUNCTION;  if (soundcard != -1) lFlags = lFlags | WAVE_MAPPED;  rc = waveOutOpen(ref hWaveOut, soundcard,        ref format_wave, 0, 0, lFlags);  if (rc != 0) return;  outHdr.lpData = bufferIn;  outHdr.dwBufferLength =        numSamples * format_wave.nBlockAlign;  outHdr.dwFlags = 0;  outHdr.dwLoops = 0;  waveOutPrepareHeader(hWaveOut, ref outHdr,        Marshal.SizeOf(outHdr));  waveOutWrite(hWaveOut, ref outHdr, Marshal.SizeOf(outHdr));}VB.NETSub Play(ByVal soundcard As Short) Dim rc As Integer Dim lFlags As Integer lFlags = CALLBACK_FUNCTION If soundcard <> -1 Then lFlags = lFlags Or WAVE_MAPPED rc = waveOutOpen(hWaveOut, soundcard, format_wave, 0, _ 0, lFlags) If (rc <> 0) Then Exit Sub outHdr.lpData = bufferIn outHdr.dwBufferLength = numSamples * format_wave.nBlockAlign outHdr.dwFlags = 041214.5Audio playback outHdr.dwLoops = 0 waveOutPrepareHeader(hWaveOut, outHdr, Len(outHdr)) waveOutWrite(hWaveOut, outHdr, Len(outHdr))End SubC# developers will also require the following namespaces:C#using System.Runtime.InteropServices;using System.Text;The next step is to design the user interface. Open the form and drag ontwo buttons named btnBrowse and btnPlaySound. Add a textbox nametbWave and a File Open Dialog control named OpenFileDialog. Click on the Browse button and add the following code:C#private void btnBrowse_Click(object sender, System.EventArgs e){  openFileDialog.ShowDialog();  tbWave.Text = openFileDialog.FileName;}VB.NETPrivate Sub btnBrowse_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnBrowse.Click   OpenFileDialog.ShowDialog()   tbWave.Text = OpenFileDialog.FileNameEnd SubThe –1 in the code below signiﬁes that we are using the default outputdevice and not a modem. Click on the Play sound button, and add the following code:C#private void btnPlaySound_CSlick(object sender, System.EventArgs e){   string filename = tbWave.Text;   audio.LoadFile(ref filename);   audio.Play(-1);}14.5Audio playback413Chapter 14VB.NETPrivate Sub btnPlaySound_Click(ByVal eventSender As _System.Object, ByVal eventArgs As System.EventArgs) _Handles btnPlaySound.ClickLoadFile(tbWave.Text)  Play(-1)End SubYou will need to set option strict off at the top of your code andinclude a reference to Microsoft Visual Basic .NET Compatibility Runtime. To test the application, run it from Visual Studio .NET, press Browse,and locate a wave ﬁle on your hard disk. Press Play sound, and you shouldhear the audio being played (Figure 14.2).14.5.1Audio playback over TAPIBy combining the previous two example programs, and with the additionof a few extra lines of code, we can now send audio down the phone line,completing this introduction to CTI in .NET.Open the ﬁrst example program and include the module from the sec-ond example program. Copy the user interface from the second exampleprogram (including openFileDialog) and place the buttons and textbox onthe form.The only hurdle in combining these two programs is to ﬁnd a way tomap a handle to a line to a handle to an output device. Luckily, an API calldoes that for us: lineGetID. Open the TAPI module and enter the follow-ing code:C#public const short LINECALLSELECT_CALL = 0x4;Figure 14.2Wave sound playerapplication.41414.5Audio playback  public struct varString  {    public long dwTotalSize;    public long dwNeededSize;    public long dwUsedSize;    public long dwStringFormat;    public long dwStringSize;    public long dwStringOffset;    public string bBytes;  }[DllImport("Tapi32.dll",SetLastError=true)]public static extern long lineGetID (long hLine, long      dwAddressID, long hCall, long dwSelect,   varString lpDevice, string lpszDeviceClass);VB.NETPublic Const LINECALLSELECT_CALL = &H4Structure varString Dim dwTotalSize As Long Dim dwNeededSize As Long Dim dwUsedSize As Long Dim dwStringFormat As Long Dim dwStringSize As Long Dim dwStringOffset As Long Dim bBytes As StringEnd StructurePublic Declare Function lineGetID Lib "Tapi32" _(ByVal hLine As Long, ByVal dwAddressID As _Long, ByVal hCall As Long, ByVal dwSelect As Long, _ByRef lpDevice As varString, ByVal lpszDeviceClass As _String) As LongGo to the user interface, click on the Browse button, and add the fol-lowing code:C#private void btnBrowse_Click(object sender, System.EventArgs e)14.5Audio playback415Chapter 14{  openFileDialog.ShowDialog();  tbWave.Text = openFileDialog.FileName;}VB.NETPrivate Sub btnBrowse_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnBrowse.Click  OpenFileDialog.ShowDialog()  tbWave.Text = OpenFileDialog.FileNameEnd SubBecause we are not playing through the default audio output device, wecan no longer specify –1 for the sound card parameter in Play; we have touse GetLineID.  C#private void btnPlaySound_Click(object sender, System.EventArgs e){  string filename = tbWave.Text;  audio.LoadFile(ref filename);  audio.Play((short)Convert.ToInt32     (TAPI.GetLineID("wave/out")));}VB.NETPrivate Sub btnPlaySound_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) Handles _btnPlaySound.Click  LoadFile(tbWave.Text)  Play(GetLineID("wave/out"))End SubThe ﬁnal step is to implement the GetLineID function. This retrievesthe audio output device from the current call handle. As the parametersimply, this function is only valid when an active call is in progress (i.e., youcan’t send audio down a phone line if no one is listening). The string manipulation is used to convert a C++ representation of avariable-length string to the .NET string type. 41614.5Audio playbackC#public static string GetLineID(string sWave){  long nError = 0;  string sTemp = "";  TAPI.varString oVar = new TAPI.varString();   System.Text.StringBuilder sb = new    System.Text.StringBuilder();  oVar.bBytes = sb.Append(' ',2000).ToString();  oVar.dwTotalSize = Marshal.SizeOf(oVar);  nError = lineGetID(hLine, 0, hCall,      LINECALLSELECT_CALL, oVar, sWave);  if (oVar.dwStringOffset == 0) return "-1";  sTemp = oVar.bBytes.Substring(0,    (int)oVar.dwStringSize).Trim();  return sTemp;}VB.NETPublic Function GetLineID(ByVal sWave As String) as String Dim nError As Long Dim sTemp As String Dim oVar As varString oVar.bBytes = Space(2000) oVar.dwTotalSize = Len(oVar) nError = lineGetID(hLine, 0, hCall, _ LINECALLSELECT_CALL, oVar, sWave) If oVar.dwStringOffset = 0 Then Return -1 sTemp = Trim(oVar.bBytes.Substring(0, oVar.dwStringSize)) Return sTempEnd FunctionTo test this program, run it from Visual Studio .NET and press startMo-dem (Figure 14.3). Connect your modem to a phone line. With a secondphone, dial the number of the phone line that is connected to your modem.When an incoming call is detected and displayed on-screen, you can pressacceptCall. You will hear the ringing stop once the line is open. PressBrowse, and locate a ﬁle on your hard drive, press Play Sound, and youshould hear it through your phone.14.6Conclusion417Chapter 14You may notice a distinct loss in sound quality when audio is sent overthe phone line. Choosing different ﬁle types can lessen this effect. The ofﬁ-cial format for TAPI is u-Law 56 Kbps (7 KHz, 8-bit mono) in the UnitedStates and a-law 64 Kbps (8 KHz, 8-bit mono) in Europe; however, frompersonal experience, I have found that 22,050 Hz is clearer, even overTAPI connections. 14.6ConclusionThis chapter detailed the technology involved in making a computer per-form a task that most of us do every day—answering the phone. Systemslike these can be used to assist any organization’s customer service activities,providing scalable call routing, and can answer simple queries withoutrequiring full-time phone operators. The applications of such a system are virtually unlimited because it canbe used to provide information and services to people who can’t or don’thave time to log into the Internet. They are used in cinemas, ticket bookingagencies, and mobile phone top-up centers. The next chapter deals with an interesting technology that solves theproblem of reliably sending data between a client and server that are notalways connected to each other. Say hello to MSMQ!Figure 14.3TAPI call receiverwith DTMF andplayback.This page intentionally left blank  419 15 Message Queues 15.1Introduction In all of the networking examples so far, it is necessary for the client andserver to be running at the same time in order for communication to besent between them. Message queuing software facilitates the constructionof queues between client and server over an impermanent connection,such that messages are stored until a connection is present. Microsoft Mes-sage Queue (MSMQ) is the most applicable system for .NET, but otherproducts such as IBM WebSphere MQ ( www.ibm.com/software/ts/mqseries )or TIBCO  RendezVous   (http://www.tibco.com/software/enterprise_backbone/rendezvous.jsp)  are alternatives. Message queuing is often used as a backup system for whenever a com-munication link fails between two servers. This improves overall system sta-bility in the event of catastrophic failure. This type of fallback mechanism isvital in systems where out-of-sync data between sites could cause opportu-nities for malicious users to defraud the system. One could imagine if a per-son were to withdraw funds from two ATMs simultaneously, during atemporary interbank communications link failure. If the ATMs did notpropagate the transactions back to the bank once the link was restored, thenthe person could run away with double the available balance. This chapter begins by describing the basics of MSMQ and providingexamples of how to send and receive objects via a message queue. This topicis then developed toward the end of the chapter by detailing other featuresof MSMQ, which help manage how messages are sent through the system. 420 15.3 Implementing a message queue 15.2MSMQ A common application for MSMQ is where a business may have many dif-ferent regional outlets and one head ofﬁce, where stock replacement andauditing takes place. Each outlet may have only a dial-up Internet connec-tion, but a system still needs to be in place to provide good, reliable dataconsolidation whenever the satellite ofﬁces are connected to the head ofﬁce.The amount of work involved in implementing a custom solution is sub-stantial, especially if the system is expected to scale upward.MSMQ is included with Windows XP Professional and available as partof the Windows NT4 Option Pack. To install it, click Start  →→→→ ControlPanel  →→→→ Add or Remove Programs  →→→→ Windows components, and check Mes-sage Queuing, and then Next. You can administer MSMQ by clicking Start  →→→→ Control Panel  →→→→     Admin-istrative Tools  →→→→ Computer Management  →→→→ Services and Applications  →→→→     Mes-sage Queuing (Figure 15.1). 15.3Implementing a message queue To run this example, you will need MSMQ running on your computer. Inthis example, a message will be passed between two computers with an Figure 15.1 ComputerManagementdialog, MSMQconsole. 15.3 Implementing a message queue421Chapter 15 impermanent link between them. If you are on a LAN, you can simulatethe dropout in connectivity by unplugging the Ethernet cable; for readerswith only one computer, the effect can be simulated by running the clientand server one after the other (not simultaneously).An application not too dissimilar from this example could be used to per-form database replication, where the string being sent via MSMQ could bean SQL statement, and the receiver could execute the statement, rather thansimply displaying it. In this way, each action performed on the local databasecould be mirrored on a remote database whenever they are connected. The types of data that can be placed on queues are not limited to strings.All objects that can be serialized can be placed on the message queue as XML.Create a new Visual Studio .NET application as Per usual. Add a refer-ence to  System.Messaging.dll  with Projects  →→→→ Add Reference. This code will ﬁrst check if a queue is available, and then create it if it isnot available. Once that is done, the contents of a textbox will be sent to thequeue. Draw a textbox named  tbMessage  and a button named  btnSend ,and then click on the button. C# private void btnSend_Click(object sender, System.EventArgs e){  string queueName = ".\\private$\\test";  MessageQueue mq;  if (MessageQueue.Exists(queueName))   {    mq=new MessageQueue(queueName);  }  else  {    mq = MessageQueue.Create(queueName);  }  mq.Send(tbMessage.Text);} VB.NET Private  Sub btnSend_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim queueName As String =  ".\private$\test"   Dim mq As MessageQueue  If MessageQueue.Exists(queueName) Then    mq=New MessageQueue(queueName) 422 15.3 Implementing a message queue   Else     mq = MessageQueue.Create(queueName)  End If  mq.Send(tbMessage.Text)End Sub Table 15.1 Signiﬁcant members of the  MessageQueue  class . Formatter Speciﬁes the formatter used to serialize or deserialize the message body; can be either  XmlMessageFormatter ,  ActiveXMessageFormatter , or  BinaryMessageFormatter  Label Speciﬁes a human-readable queue description Path Speciﬁes the location of the queue Transactional Speciﬁes whether the queue can accept nontransactional messages Authenticate Speciﬁes whether the queue can accept unauthenticated messages EncryptionRequired Speciﬁes whether the queue can accept unencrypted messages Close Frees all resources used by the handle to the queue Create Creates a new queue at the speciﬁed path Delete Removes a queue from MSMQ, deleting all messages contained therein GetAllMessages Returns an array of messages from the speciﬁed queue GetPrivateQueuesByMachine Returns an array of private message queues from the speciﬁed machine GetPublicQueues Returns an array of queues on the local network Receive Returns a message from the top of the speciﬁed queue Send Sends a message to the tail of the speciﬁed queue Purge Deletes all messages from a queue, but does not delete the queue itself 15.3 Implementing a message queue423Chapter 15 This code ﬁrst looks at MSMQ to see if a queue of the name  \private$\test  has been created on the local machine. If it has not, then a  Message-Queue  object (Table 15.1) points to the existing one. The contents of the text-box ( tbMessage ) are then sent as a message to this queue.More than one queue can be held on one MSMQ server. They arenamed  <  Server name  >\private$\<  Queue name  > , where  <  Servername  >  can be either the computer name of the MSMQ server or “ . ” forthe local server. When the MSMQ server is not on the local intranet, thenMSMQ over HTTP may be used. In this case, the server name can be an IPaddress or domain name. MSMQ HTTP support must be installed fromAdd/Remove Windows components in the Control Panel (Figure 15.2).HTTP message queues are hosted by IIS and reside in the  msmq  virtualfolder. In this way, queues over HTTP take the form: http://< domain name >/msmq/< queue name > You will also require the following namespace: C# using System.Messaging; VB.NET Imports System.Messaging     To test this application, ensure that you have MSMQ installed on yoursystem, and then run this program from Visual Studio .NET. You shouldthen type some text into the box provided and press Send, as shown in Fig-ure 15.3.Go to Message Queuing in Computer Management, and click on Pri-vate Queues  →→→→ Test  →→→→ Queue Messages. Double-click on the envelope icon onthe right-hand side, and click on the Body tab in the new window. Youshould see an XML representation of the text that was sent (Figure 15.4).To complete the example, it is also necessary to know how to read in amessage from a message queue, as well as how to write to it.Draw a textbox,  tbStatus , and a button,  btnListen . Ensure that  Mul-tiLine  is set to  true  for the textbox. Click on  btnListen  and enter the fol-lowing code: 424 15.3 Implementing a message queue C# private void btnListen_Click(object sender, System.EventArgs e){  Thread thdListener = new Thread(new ThreadStart(QThread));  thdListener.Start();} VB.NET Private  Sub btnListen_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim thdListener As Thread =  New Thread(New _  ThreadStart(AddressOf QThread))   thdListener.Start()End Sub Figure 15.2 MSMQ HTTPsupport. Figure 15.3 Basic MSMQapplication. 15.3 Implementing a message queue425Chapter 15 Because it is possible that there are no messages on the queue, the serverwill block (hang) at the point of calling the  Receive  method; therefore, QThread  is invoked as a new thread.  C# public void QThread(){  string queuePath = ".\\private$\\test";        MessageQueue queue = new MessageQueue(queuePath);  System.Messaging.Message msg;        ((XmlMessageFormatter)queue.Formatter).TargetTypes =     new Type[1];  ((XmlMessageFormatter)queue.Formatter).TargetTypes[0] =     "".GetType();  while(true)  {    msg= queue.Receive();    tbStatus.Text += msg.Body + "\n";          }} VB.NET Public  Sub QThread() Dim queuePath As String =  ".\private$\test"  Dim queue As MessageQueue =  New MessageQueue(queuePath)  Figure 15.4 Native XMLformat of a messagein MSMQ.42615.3Implementing a message queue Dim msg As System.Messaging.Message CType(queue.Formatter, XmlMessageFormatter).TargetTypes _ = New Type(0){} CType(queue.Formatter, XmlMessageFormatter).TargetTypes(0) _ = "".GetType() Do    msg= queue.Receive()   tbStatus.Text += msg.Body + VbCrLf         LoopEnd SubIn Figure 15.4, it is clear that messages are stored internally in XML for-mat. This permits the storage of complex types as well as simple strings, butit is necessary to indicate to MSMQ the target type of the object that youwant to read back into. In this case, we are reading a string, so theTargetType is set to string (obtained by the "".GetType() construct). It isnot necessary to specify the object type when sending to a message queuebecause .NET can determine this from reﬂection. The deserialized versionof the object is then held in the Message object (Table 15.2).Table 15.2Signiﬁcant members of the Message class .AcknowledgeTypeSpeciﬁes the events that require acknowledgment from MSMQAcknowledgmentDetermines the type of acknowledgment that is ﬂagged by the messageAdministrationQueueSpeciﬁes the queue to which acknowledgment messages are to be sent AttachSenderIdIncludes the ID of the sending machine in the messageBodySpeciﬁes the message payload, which can be any type of objectLabelIncludes a human-readable description of the messageMessageTypeIndicates that the message is normal, an acknowledgment, or a reportPriorityDetermines where in the queue the message is to be placed15.3Implementing a message queue427Chapter 15You will need a reference to System.Messaging.dll and the followingnamespaces:C#using System.Threading;using System.Messaging;VB.NETimports System.Threadingimports System.MessagingTo test this application, run them both from their .exe ﬁles. Type “helloworld” into the client, and press Send. The message will not appear on theserver because it is not listening yet. Press the Listen button on the server,and the message will appear in the status box, as shown in Figure 15.5. 15.3.1Queuing complex objectsIt is perfectly valid to use the serialization and deserialization techniquesdescribed in Chapter 2 to send complex objects as strings through MSMQ.In the interest of efﬁciency and simplicity, it is better to use the built-infunctionality in MSMQ to perform this task. In the following example, imagine a situation where a chain of hotelshas a central booking agency. This agency takes phone reservations fromoverseas customers and places each booking in a queue destined for a par-ticular hotel. This hotel would periodically dial in to collect the latest book-ings from this queue. RecoverableSpeciﬁes whether the message is stored in memory or disk SenderIdIndicates the sending machineTimeToReachQueueSpeciﬁes the maximum length of time it should take for a message to reach a queue UseDeadLetterQueueDetermines if the time-expired messages should go to the dead-letter queueUseJournalQueueDetermines if received messages should be archived in the journalTable 15.2Signiﬁcant members of the Message class (continued).42815.3Implementing a message queueA hotel needs to know the names of the tourists, when they are comingand leaving, and what type of room they are looking for. Furthermore, thereservation system at the hotel is automated, so the message has to be in awell-deﬁned format. Building on the previous example to send strings to a message queue,include a class that represents a hotel reservation. Add the following codedirectly at the start of the namespace: C#public class booking{  public enum RoomType  {    BASIC,    EN_SUITE,    DELUXE  }    public class Room  {    public Int16 occupants;    public RoomType roomType;  }  public string name;  public Room room;  public DateTime arrival;  public DateTime departure;Figure 15.5Basic MSMQreceiverapplication.15.3Implementing a message queue429Chapter 15}VB.NETPublic Class booking   Public Enum RoomType     BASIC    EN_SUITE    DELUXE  End Enum  Public Class Room    Public occupants As Int16    Public roomType As RoomType  End Class  Public name As String  Public myRoom As Room  Public arrival As DateTime  Public departure As DateTimeEnd ClassSelect the Form Design tab, and remove the textbox (tbMessage) fromthe form. Now drag on two textboxes named tbName and tbOccupants. Ifyou wish, you can use labels to indicate what each textbox is used for,although this is not essential. Draw on two Date-Picker controls nameddtArrival and dtDeparture. A combo box named cbType is also required.You must click on the Items property for the combo box and add threestrings: basic, en suite, and deluxe.Click on the Send button and add the following code:C#private void btnSend_Click(object sender, System.EventArgs e){  string queueName = ".\\private$\\test";  MessageQueue mq;  if (MessageQueue.Exists(queueName))   {    mq=new MessageQueue(queueName);  }  else  {43015.3Implementing a message queue    mq = MessageQueue.Create(queueName);  }  booking hotelBooking = new booking();  hotelBooking.name = tbName.Text;  hotelBooking.departure = DateTime.Parse(dtDeparture.Text);  hotelBooking.arrival = DateTime.Parse(dtArrival.Text);  hotelBooking.room = new booking.Room();  hotelBooking.room.occupants =  Convert.ToInt16(tbOccupants.Text);              switch(cbType.SelectedIndex.ToString())  {    case "basic":      hotelBooking.room.roomType = booking.RoomType.BASIC;      break;    case "en suite":       hotelBooking.room.roomType = booking.RoomType.EN_SUITE;      break;    case "deluxe":       hotelBooking.room.roomType = booking.RoomType.DELUXE;      break;  }  mq.Send(hotelBooking);}  VB.NETPrivate  Sub btnSend_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim queueName As String =  ".\private$\test"   Dim mq As MessageQueue  If MessageQueue.Exists(queueName) Then    mq=New MessageQueue(queueName)  Else     mq = MessageQueue.Create(queueName)  End If  Dim hotelBooking As booking =  New booking()   hotelBooking.name = tbName.Text  hotelBooking.departure = DateTime.Parse(dtDeparture.Text)  hotelBooking.arrival = DateTime.Parse(dtArrival.Text)  hotelBooking.myroom = New booking.Room()  hotelBooking.myroom.occupants = _    Convert.ToInt16(tbOccupants.Text)            15.3Implementing a message queue431Chapter 15  Select Case cbType.SelectedIndex.ToString()    Case "basic"      hotelBooking.myroom.roomType = booking.RoomType.BASIC      Exit Sub    Case "en suite"      hotelBooking.myroom.roomType = _        booking.RoomType.EN_SUITE      Exit Sub    Case "deluxe"      hotelBooking.myroom.roomType = booking.RoomType.DELUXE      Exit Sub  End Select  mq.Send(hotelBooking)End SubYou will need a reference to System.Messaging.dll and the followingnamespaces:C#using System.Threading;using System.Messaging;VB.NETimports System.Threadingimports System.MessagingTo test the application at this stage, you can run it from Visual Studio.NET. Type some reservation details into the boxes provided, and press send(Figure 15.6).If you open the test queue in Computer Management and right-click onProperties→→→→Body for the new message, you will notice a more verbose XMLrepresentation of the booking object:<?xml version="1.0"?><booking xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>Fiach Reid</name> <room>  <occupants>   1  </occupants>  <roomType>43215.3Implementing a message queue   BASIC  </roomType> </room> <arrival>  2002-04-28T00:00:00.0000000-00:00 </arrival> <departure>  2002-05-07T00:00:00.0000000-00:00 </departure></booking>Now, to deserialize the object at the receiving end, it is just a matter ofaltering the TargetType in the queue formatter from string to booking.You will also need to display the new booking, and of course, you still needto include the booking class after the namespace. Replace the code in the QThread function with the following:C#public void QThread(){  string queuePath = ".\\private$\\test";        MessageQueue queue = new MessageQueue(queuePath);  System.Messaging.Message msg;        ((XmlMessageFormatter)queue.Formatter).TargetTypes =     new Type[1];  ((XmlMessageFormatter)queue.Formatter).TargetTypes[0] = Figure 15.6Complex objectMSMQ transferexample.15.3Implementing a message queue433Chapter 15    (new booking()).GetType();  while(true)  {     msg= queue.Receive();     booking hotelBooking = (booking)msg.Body;     tbStatus.Text += "tourist name:" +      hotelBooking.name + "\n";     tbStatus.Text += "arrival:" +      hotelBooking.arrival + "\n";     tbStatus.Text += "departure:" +      hotelBooking.departure + "\n";     if (hotelBooking.room!=null)     {       tbStatus.Text += "room occupants:" +        hotelBooking.room.occupants + "\n";               tbStatus.Text += "room type:" +       hotelBooking.room.roomType.ToString() + "\n";      }  }}VB.NETPublic  Sub QThread()  Dim queuePath As String =  ".\private$\test"   Dim queue As MessageQueue =  New MessageQueue(queuePath)   Dim msg As System.Messaging.Message  CType(queue.Formatter, XmlMessageFormatter).TargetTypes = _     New Type(0) {}  CType(queue.Formatter, _     XmlMessageFormatter).TargetTypes(0) = _     (New booking()).GetType()   Do      msg= queue.Receive()     Dim hotelBooking As booking = CType(msg.Body, booking)      tbStatus.Text += "tourist name:" + _      hotelBooking.name + vbcrlf      tbStatus.Text += "arrival:" + _      hotelBooking.arrival + vbcrlf      tbStatus.Text += "departure:" + _      hotelBooking.departure + vbcrlf      if not hotelBooking.room is nothing then       tbStatus.Text += "room occupants:" & _43415.3Implementing a message queue          hotelBooking.myroom.occupants & vbcrlf _          tbStatus.Text += "room type:" & _          hotelBooking.myroom.roomType.ToString() & vbcrlf     end if     Loop End SubThis code locates an existing queue named \private$\test on the localmachine. Because the message contains only one type of object, the Tar-getTypes property is set to an array of one type. The ﬁrst and only objectpassed is a booking, and therefore element 0 in the array of target types isset to the booking type.The thread now enters an inﬁnite loop. Where it encounters the Receivemethod, the execution blocks until a new message appears in the queue. Thismessage is converted into a booking and then displayed on-screen. To test this, ﬁrst check that the top message in the test queue is one thatrepresents a hotel booking. If you are unsure, delete the queue, and thenrun the preceding program to post a new reservation to the queue. Now runthis program from Visual Studio .NET and press Listen. You should see thedetails of a new booking in the textbox, as shown in Figure 15.7.Figure 15.7Complex objectMSMQ receiverexample.15.3Implementing a message queue435Chapter 1515.3.2TransactionsLike databases, MSMQ supports transactions. A transaction is an atomicunit of work that either succeeds or fails as a whole. In a banking system, atransaction might involve debiting a checking account via one messagequeue and crediting a savings account via another queue. If a system failurewere to occurr in the middle of the transaction, the bank would be liable fortheft, unless the transaction were rolled back. After the system restarted, thetransaction could be carried out again.The following code attempts to add two messages to a queue. The codehas a deliberate division by zero error between the two message sends. Ifthis line is commented out, both operations are carried out; if not, then nei-ther operation is carried out.Open the client application in the previous example. Click on the Sendbutton, and replace the code with the following:C#private void btnSend_Click(object sender, System.EventArgs e){  int zero = 0;  string queueName = ".\\private$\\test2";  MessageQueueTransaction msgTx = new  MessageQueueTransaction();  MessageQueue mq;  if (MessageQueue.Exists(queueName))  {    mq=new MessageQueue(queueName);  }  else  {    mq = MessageQueue.Create(queueName,true);  }  msgTx.Begin();  try  {    mq.Send("Message 1",msgTx);    zero = 5 / zero; // deliberate error    mq.Send("Message 2",msgTx);    msgTx.Commit();  }43615.3Implementing a message queue  catch  {    msgTx.Abort();  }  finally  {    mq.Close();  }}VB.NETPrivate  Sub btnSend_Click(ByVal sender As Object, _     ByVal e As System.EventArgs)  Dim zero As Integer =  0   Dim queueName As String =  ".\private$\test2"   Dim msgTx As MessageQueueTransaction =  New _     MessageQueueTransaction()   Dim mq As MessageQueue  If MessageQueue.Exists(queueName) Then    mq=New MessageQueue(queueName)  Else     mq = MessageQueue.Create(queueName,True)  End If  msgTx.Begin()  Try    mq.Send("Message 1",msgTx)    zero = 5 / zero ' deliberate error    mq.Send("Message 2",msgTx)    msgTx.Commit()  Catch    msgTx.Abort()  Finally    mq.Close()  End TryEnd SubThis code creates a queue as before. The Begin method initiates a trans-action. This means that any changes to the queue will not physically takeplace until the Commit method is called. If the Abort method is called, orthe computer crashes, then any statements issued directly after the Beginmethod are ignored. In this case, an error occurs before the second message15.3Implementing a message queue437Chapter 15is posted to the queue. This error throws an exception, which causes code tobe executed that aborts the transaction.To test this application, run it from Visual Studio .NET with thedeliberate error left in the code. Press Send, and then open ComputerManagement and look at Message Queues. You will notice that a secondqueue has been created, but neither message has been posted. If you nowremove the deliberate error from the code and rerun the application, thenpress the Send button, you will see both messages appearing in the QueueMessages list.15.3.3AcknowledgmentsMost of the work done by MSMQ is behind the scenes and completelytransparent to the application. If MSMQ fails for some reason, the applica-tion—and therefore the user—will not know that today’s data was nevertransferred. Acknowledgments provide a mechanism for the sending appli-cation to verify that the receiving application has read the message and thatthe message queue is functioning correctly.  This example builds on the code for the ﬁrst example in this chapter, soopen that project in Visual Studio .NET and click on the Send button.C#private void btnSend_Click(object sender, System.EventArgs e){  string queueName = ".\\private$\\test";  MessageQueue mq;  if (MessageQueue.Exists(queueName))   {    mq=new MessageQueue(queueName);  }  else  {    mq = MessageQueue.Create(queueName);  }  System.Messaging.Message myMessage = new  System.Messaging.Message();  myMessage.Body = tbMessage.Text;  myMessage.AdministrationQueue =   new MessageQueue(".\\private$\\test");  myMessage.AcknowledgeType = AcknowledgeTypes.FullReachQueue 43815.3Implementing a message queue  AcknowledgeTypes.FullReceive;  mq.Send(myMessage);}VB.NETPrivate  Sub btnSend_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim queueName As String =  ".\private$\test"   Dim mq As MessageQueue  If MessageQueue.Exists(queueName) Then    mq=New MessageQueue(queueName)  Else     mq = MessageQueue.Create(queueName)  End If  Dim myMessage As System.Messaging.Message = New _   System.Messaging.Message()   myMessage.Body = tbMessage.Text  myMessage.AdministrationQueue = New MessageQueue( _  ".\private$\test")  myMessage.AcknowledgeType = _  AcknowledgeTypes.FullReachQueue or _  AcknowledgeTypes.FullReceive   mq.Send(myMessage)End SubThe preceding code checks for a private queue named \private$\test.If one is not found, a queue is then created. A message is then created, readyfor posting to this queue. This message is set to acknowledge reaching thequeue (AcknowledgeTypes.FullReachQueue) and reaching the end-recipi-ent (AcknowledgeTypes.FullReceive). Acknowledgments are set to appearin the same test queue. To test this piece of code, run it from Visual Studio .NET, type some textinto the box provided, and press send. On opening Computer Manage-ment→→→→Message Queuing→→→→Private Queues→→→→Test, you will notice acknowledg-ment messages interspersed throughout the list. Acknowledgment messageshave a body size of 0 and carry a green circle on the envelope icon (Figure15.8). The receiver program can recognize acknowledgment messages when amessage has its MessageType set to MessageType.Acknowledgment. 15.4Timeouts439Chapter 15Note: When each message is received, a second acknowledgment messagewill appear in the queue, labeled “The message was received.”15.4Timeouts“Late data is bad data” is an expression that applies particularly to MSMQ.Imagine a scenario in which MSMQ were used to coordinate last-minutehotel bookings. When a client (a hotel) could not be contacted for morethan 24 hours after a booking, it would be imperative that alternativeaction be taken, such as having an operator call the hotel to conﬁrm thebooking manually. Timeouts provide a mechanism to age messages, such that if they do notreach their destination in time, the message can be deleted or moved to adead-letter queue so that alternative actions can be taken.In this example, messages are sent with a ﬁve-second timeout. Thismeans they will only appear in the queue for ﬁve seconds after being sent,before they are either read by a receiving application or discarded to thedead-letter messages queue. This example builds on the preceding example.Open the preceding example in Visual Studio .NET, and click on theSend button. Then enter the following code:C#private void btnSend_Click(object sender, System.EventArgs e){  string queueName = ".\\private$\\test";Figure 15.8MSMQacknowledgments.44015.4Timeouts  MessageQueue mq;  if (MessageQueue.Exists(queueName))   {    mq=new MessageQueue(queueName);  }  else  {    mq = MessageQueue.Create(queueName);  }  System.Messaging.Message myMessage = new  System.Messaging.Message();  myMessage.Body = tbMessage.Text;  myMessage.TimeToBeReceived = new TimeSpan(0,0,0,5);  myMessage.UseDeadLetterQueue = true;        mq.Send(myMessage);}VB.NETPrivate  Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs)  Dim queueName As String =  ".\private$\test"   Dim mq As MessageQueue  If MessageQueue.Exists(queueName) Then    mq=New MessageQueue(queueName)  Else     mq = MessageQueue.Create(queueName)  End If  Dim myMessage As System.Messaging.Message = _  New System.Messaging.Message()   myMessage.Body = tbMessage.Text  myMessage.TimeToBeReceived = New TimeSpan(0,0,0,5)  myMessage.UseDeadLetterQueue = True        mq.Send(myMessage)End SubIn this code, the TimeToBeReceived for the message is set to ﬁve sec-onds. A related property TimeToReachQueue can also be used to time-outmessages that do not reach the queue in a timely fashion. By settingUseDeadLetterQueue to true, all messages that pass their expiration timeare moved into the dead-letter queue for administrative purposes.15.5Journal 441Chapter 15To test this piece of code, run it from Visual Studio .NET. Type some-thing into the box provided and press Send. Quickly open Computer Man-agement, and click on the test queue (you may need to right-click and pressRefresh). You should see a new message in the list. The messages will disap-pear again if you refresh the queue after ﬁve seconds. Click on SystemQueues→→→→Dead-letter messages to view expired messages (Figure 15.9).15.5Journal Journaling is where a record is kept of incoming and outgoing messages toand from remote machines. To specify that the message should be recordedin the journal, the UseJournalQueue method is used.In the following example, you will need to have the message receiver pro-gram described earlier in this chapter close at hand. When sending a messagethat uses the Journal queue, it will only be transferred to that queue once ithas been received. This differs from acknowledgment because the body ofthe message is stored rather than simply ﬂagging an empty message.Open the preceding example in Visual Studio .NET, and click on theSend button. Then enter the following code:C#private void btnSend_Click(object sender, System.EventArgs e){  string queueName = ".\\private$\\test";  MessageQueue mq;  if (MessageQueue.Exists(queueName))   {    mq=new MessageQueue(queueName);Figure 15.9MSMQ messagetimeouts.44215.5Journal   }  else  {    mq = MessageQueue.Create(queueName);  }  System.Messaging.Message myMessage = new  System.Messaging.Message();  myMessage.Body = tbMessage.Text;  myMessage.UseJournalQueue = true;        mq.Send(myMessage);}VB.NETPrivate Sub btnSend_Click(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim queueName As String =  ".\private$\test"   Dim mq As MessageQueue  If MessageQueue.Exists(queueName) Then    mq=New MessageQueue(queueName)  Else     mq = MessageQueue.Create(queueName)  End If  Dim myMessage As System.Messaging.Message = _    New System.Messaging.Message()   myMessage.Body = tbMessage.Text  myMessage.UseJournalQueue = True        mq.Send(myMessage)End SubThis piece of code creates a queue as before and posts a string as a mes-sage to the queue. Because UseJournalQueue is set, the message will bemoved to this system queue after it has been received. To test this piece of code, run it from Visual Studio .NET. Type some-thing into the box provided and press Send. Open Computer Managementand look at the test queue to conﬁrm that the message is in the system. Startthe message receiver program, and press Listen. The message should appearin the textbox of the receiver program and be removed from the queue.Clicking on System Queues→→→→Journal messages should show the messageonce again (Figure 15.10). 15.6Queued Components443Chapter 1515.6Queued ComponentsThe bulk of the MSMQ code examples to date are very much concernedwith the underlying plumbing of sending and receiving messages. You maywish to write code that abstracts away from the underlying MSMQ send &receive mechanisms and concentrate more on business logic. MSMQ can work in tandem with COM+ component services to providea means of asynchronous, queued invocation of object methods via QueuedComponents. In the below example, a component that can perform data-base updates is created, and a corresponding client is used to call methods onthis component. If there were an impermanent connection to this database,then the component may fail during an update, MSMQ handles retries, andqueues method calls whenever the component is unavailable. An example application of the below code is where a database update isrequired, but is of lower priority than other code which must not bedelayed whist waiting for the update to complete. You may create a queued component by ﬁrstly generating a strong namekey ﬁle by typing sn –k CompPlusServer.snk at the VS.NET commandprompt. You can then start a new class library project in Visual Studio.NET, and enter the following codeC#[assembly: ApplicationName("ComPlusServer")][assembly: ApplicationActivation(ActivationOption.Server)][assembly: AssemblyKeyFile("..\\..\\ComPlusServer.snk")][assembly: ApplicationQueuing(Enabled=true, QueueListenerEnabled=true)]namespace ComPlusServiceFigure 15.10MSMQ, Journalmessages.44415.6Queued Components{   public interface IComPlusServer   {      void ExecSQLAsync(string SQL,string strDSN);      }      [InterfaceQueuing(Interface="IComPlusServer")]   public class ComPlusServer : ServicedComponent,   IComPlusServer   {         public void ExecSQLAsync(string SQL,string strDSN)   {   OleDbConnection DSN =  new           OleDbConnection(strDSN);   DSN.Open();   OleDbCommand oSQL = new OleDbCommand("",DSN);     oSQL.CommandText = SQL;   oSQL.ExecuteNonQuery();   DSN.Close();   }   }}VB.NET<assembly: ApplicationName("ComPlusServer")><assembly: ApplicationActivation(ActivationOption.Server)><assembly: AssemblyKeyFile("..\..\ComPlusServer.snk")><assembly: ApplicationQueuing(Enabled := True, _    QueueListenerEnabled := True)>Public Interface IComPlusServerSub ExecSQLAsync(ByVal SQL As String, ByVal _    strDSN As String) End Interface <InterfaceQueuing([Interface] := "IComPlusServer")>  _Public Class ComPlusServer    Inherits ServicedComponent    Implements ServicedComponent, IComPlusServer        Public Sub ExecSQLAsync(ByVal SQL As String, _        ByVal strDSN As String)         Dim DSN As New OleDbConnection(strDSN)        DSN.Open()        Dim oSQL As New OleDbCommand("", DSN)15.6Queued Components445Chapter 15        oSQL.CommandText = SQL        oSQL.ExecuteNonQuery()        DSN.Close()        End Sub End Class The above code deﬁnes an interface, IComPlusServer, which contains afunction prototype for the ExecSQLAsync method. The latter methodopens a DSN connection to the speciﬁed database, executes an insert,update, or delete, and then closes the connection. A limitation of queuedcomponents is that they cannot have return values.  You will require the following namespaces at the head of your code.C#using System;using System.Reflection;using System.EnterpriseServices;using System.Data;using System.Data.OleDb;VB.NETImports SystemImports System.ReflectionImports System.EnterpriseServicesImports System.DataImports System.Data.OleDbIn order to use this DLL as a queued component, there are some furthersteps that must be taken.1.Import the DLL into the global assembly cache (GAC) by typinggacutil /I:ComPlusService.dll at the command prompt2.Import the DLL into component services by typing regsvcs Com-PlusService.DLL at the command prompt3.Disable authentication on the component by opening ComponentServices from Administrative Tools, Expand Computers→→→→MyComputer→→→→COM+ Applications. Right Click ComPlusServer,select properties→→→→Security. Uncheck Enforce access checks for thisapplication.4.Right click ComPlusServer, and click start.44615.6Queued ComponentsAt this point you can now write a client to begin calling methods on thiscomponent. Here, we simply create a Windows Forms application in VisualStudio .NET. Add a reference to the ComPlusService DLL created in theprevious example, and then draw two textboxes, tbSQL and tbDSN, and abutton named btnExecSQL. Double click the button and enter the follow-ing code:C#private void btnExecSQL_Click(object sender, System.EventArgs e){   ComPlusService.IComPlusServer ComPlusServer = null;   ComPlusServer = (IComPlusServer)   Marshal.BindToMoniker   ("queue:/new:ComPlusService.ComPlusServer");   ComPlusServer.ExecSQLAsync     (this.tbSQL.Text,this.tbDSN.Text);   Marshal.ReleaseComObject(ComPlusServer);}VB.NETPrivate Sub btnExecSQL_Click(ByVal sender As Object, _ByVal e As System.EventArgs) Handles btnExecSQL.Click  Dim ComPlusServer As ComPlusService.IComPlusServer = _  Nothing  ComPlusServer = _  CType(Marshal.BindToMoniker( _  "queue:/new:ComPlusService.ComPlusServer"), _  IComPlusServer)  ComPlusServer.ExecSQLAsync(Me.tbSQL.Text, Me.tbDSN.Text)  Marshal.ReleaseComObject(ComPlusServer)  End Sub The above code does not directly execute the ExecSQLAsync method onthe ComPlusService component. Instead it writes an instruction to theComPlusService queue in MSMQ, which is then read back by componentservices, which executes the method on the component. You will need the following namespaces at the head of the code in yourapplication.15.7Security447Chapter 15C#using ComPlusService;using System.Runtime.InteropServices;VB.NETImports ComPlusServiceImports System.Runtime.InteropServicesTo test the application, run the client from Visual Studio .NET, type ina valid DSN and SQL statement, then press the ‘Execute SQL’ button. Youwill see that the database is updated within a few moments (Figure 15.11).If you temporarily stop the component from component services, and con-tinue to use the client, then the changes will be applied as soon as yourestart the component. 15.7SecurityUsing MSMQ gives an attacker another point of access to sensitive infor-mation. Without encryption and authentication, MSMQ could never beused to handle credit card details or other ﬁnancial transactions.To encrypt a message in MSMQ, you set the UseEncryption propertyto true before sending the message. This prevents the message frombeing snooped while in transit, but it is decrypted transparently at thereceiving end. The encryption algorithm can be selected using the EncryptionAlgo-rithm property. This can be set to either RC2 or RC4. The latter is a streamcipher and is thus faster than RC2.To use authentication in a message in MSMQ, you set the UseAuthen-tication property to true before sending the message. This will guaranteeFigure 15.11Test COM+Client.44815.7Securityto the receiver that the message sender is legitimate, but it does not secureagainst packet snooping. The hashing algorithm can be selected using the HashAlgorithm prop-erty. This can be set to MAC, MD2, MD4, MD5, SHA, or none. Thedefault algorithm is MD5, although MAC (keyed hash) is the most secure. An authenticated message needs to be accompanied by an external cer-tiﬁcate, as contained within the SenderCertificate property. An externalcertiﬁcate must be registered with the directory service of MSMQ. Anexternal certiﬁcate contains information about the certiﬁcation authority,the certiﬁcate user, the validity period of the certiﬁcate, and the public keyof the certiﬁcate user, the certiﬁcation authority’s signature, and so forth.In cases where message properties usually set by MSMQ are not suitedfor a particular application, it is possible to tweak low-level security aspectsof MSMQ manually. This includes the DestinationSymetricKey property.The latter is simply a byte array used to encrypt and decrypt the message onsending and receipt. The ConnectorType property must be set to a genu-inely unique identiﬁer (GUID) to access this property.Low-level authentication properties that can be altered once Connector-Type is set are AuthenticationProviderName, AuthenticationProvider-Type, and DigitalSignature. These methods specify the name, type, andcredentials of authentication applied to the message, defaulting toMicrosoft Base Cryptographic Provider, Ver. 1.0, RSA_FULL and azero-length array, respectively.Where MSMQ is used over HTTP, it is possible to employ standardWeb security systems, such as HTTPS. In this case, the MSMQ serverdomain name would be preﬁxed with https://. As shown in chapters 8 and 9, it is easy to use ultrastrong encryptionalgorithms on strings (and serialized objects). Coupled with the use ofX.509 certiﬁcates, issued by an internationally trusted certiﬁcate authority,strong authentication could be easily applied to message queues.To illustrate the example based on the previous hotel booking centeranalogy, imagine that the booking center also forwarded credit card detailsto the hotelier via MSMQ. The booking center would need to be absolutelysure that when someone dialed into the MSMQ server, it was in fact thehotelier and not a hacker. Furthermore, it would be a disaster if a techni-cally savvy clerk at the hotel could snoop credit card details from the net-work installed at the hotel. 15.8Scalability449Chapter 15First, the hotel would need to acquire an X.509 certiﬁcate from a certiﬁ-cate authority such as Verisign or Thawte. The certiﬁcate containing theprivate key would remain at the hotel, but the public keyed certiﬁcatewould be sent to the booking center. When a phone order arrived at the booking center, a message would beplaced in the queue, which would be encrypted with the public key from thecertiﬁcate. At this point, a hacker could still receive the message, but couldnot read it; however, a problem still remains because the hotelier would notknow whether there was ever a new message or if it had been stolen.To avoid this situation, the booking center would require an acknowl-edgment from the hotel that the booking had been received. The acknowl-edgment would simply be an acknowledgment reference number encryptedwith the private key from the certiﬁcate. An attacker would not be able togenerate this message, so the message could be reposted awaiting pickupfrom the correct recipient. 15.8ScalabilityWhen computer systems scale upward, so does the volume of data beingsent between them. MSMQ needs to be able to handle larger volumes ofdata and larger networks, when needed.Note: When installing an MSMQ server behind a ﬁrewall, you will need toensure that TCP 1801 is open.MSMQ can consume a lot of disk space; therefore, it may be necessaryto ensure that some queues do not grow to a size that they ﬁll the hard diskand prevent other queues from operating. To do this, set the Queue Quotaby opening Computer Management, clicking on Message Queuing, andthen selecting the queue in question (i.e., Private Queues→→→→test2). Right-click on the queue and select Properties (Figure 15.12). The queue quota iscontained in the Limit message storage to (KB): box. The computer quotacan be set in the same manner. Another space-consuming item that is vital to the correct operation ofMSMQ is the MQIS database, an internal database that contains queueinformation and network topology. This is a distributed database, so morethan one MSMQ server can hold the data.45015.8ScalabilityIn situations where multiple segments in a network are all intercon-nected with impermanent connections, multiple MSMQ servers can bedeployed in each segment. A sample case would be an international chain ofshops that centralize their point-of-sale data at the regional ofﬁce for end-of-day processing and send it once a week to the head ofﬁce for auditing. In MSMQ terminology, the entire chain is called an enterprise, eachregional ofﬁce is a site, and every shop is a client. The MSMQ server locatedin the head ofﬁce is called the primary enterprise controller (PEC), and theservers at the regional ofﬁces are called Primary Site Controllers (PSCs).Three other types of MSMQ servers are available: backup site controllers(BSCs), routing servers, and connector servers. A BSC requires both a PEC and PSC and stores as a read-only backupof a PSC’s database. This ensures that if the PSC goes ofﬂine, clients canstill read from the BSC. A routing server provides a mechanism to forward messages throughalternate routes if a network connection goes down. To illustrate this fea-ture, envisage two sites, New York and Toronto, and a head ofﬁce in Dallas.Figure 15.12MSMQ queuesettings dialog.15.9Performance issues451Chapter 15If the link between Toronto and Dallas is broken, but links between theother cities are still operational, then a routing server could relay messagesfrom New York through Toronto. A connector server is used as a proxy between MSMQ and third-partymessaging systems, such as IBM MQSeries. The shops can be either dependent clients or independent clients. Thedifference is that an independent client can store messages locally and for-ward them to the regional ofﬁce whenever a connection becomes available.A dependent client requires an always-on connection to the regional ofﬁce.This may seem disadvantageous, but a dependent client uses less disk space,will run on Windows 95, and becomes one less point of administrationNote: You cannot install an independent client when disconnected to thePSC because it requires access to MQIS data to initialize properly.15.9Performance issuesMSMQ can operate in a multitude of ways, from running locally as aninterprocess communications (IPC) mechanism for applications or as acomplex structure of hundreds of machines working in tandem. MSMQ isan effective IPC mechanism when the messages are sent in the Express for-mat, where messages are held in RAM rather than on disk. This does meanthat the data will be erased on power failure, but the applications will alsobe stopped abruptly, so it shouldn’t matter. The only IPC that would out-perform MSMQ would be Windows messaging (speciﬁcally WM_COPY), butthis is not an easy undertaking.When operating MSMQ over a network, it is common for all messagesto be stored on disk to ensure that no data is lost in the event of a systemfailure. These messages are known as recoverable messages. They come in twoﬂavors: transactional and nontransactional.Transactions are carried out as a series of in-memory operations andthen committed to disk when the operation is complete. They can be coor-dinated by MSMQ or by the Microsoft Distributed Transaction Coordina-tor (MSDTC); the former is the more efﬁcient. Nontransactional messagescannot be rolled back, but they are faster than transactional messages. When many messages need to be written to a queue in one operation, ahigher performance can be achieved if a thread pool is used. This only45215.10Conclusionapplies to writing messages to a queue; reading from a queue using multiplethreads actually decreases performance. When using threads, it is importantto make sure the connection to the MSMQ server is not reopened in everythread, but rather, a connection is shared among all threads.Where network bandwidth is a concern (e.g., over dial-up connections), actions can be taken to reduce the size of the message body by using binary formatters rather than the default XML formatter.This can be implemented by setting the Formatter property to NewBinaryMessageFormatter()before calling the Send method. A new featurein MSMQ 3.0 is the use of multicast from within MSMQ. Where a singlemessage is destined for multiple recipients, multicasting can greatly reducenetwork trafﬁc. This does require access to the MBONE network and, thus,may not be applicable to all situations.The most common performance problem with MSMQ is handles toqueues being repeatedly opened and closed. This process is extremely waste-ful, and it is imperative that a handle to the queue should be maintained foras long as possible. A few bytes from each message can be cut by omittingthe system identiﬁcation (SID), but this is only an option if security fea-tures are not being used. Another pitfall could be that the client is request-ing too many acknowledgments from the server, which may put anunnecessary strain on both the client and server.15.10ConclusionThere is little real voodoo behind message queuing, and it would be an easytask to implement a store-and-forward-type proxy server using socket-levelprogramming; however, this chapter is meant to illustrate the advantage ofmoving to industry-standard techniques by demonstrating the wealth ofadditional functionality built into MSMQ. After an initial learning curve,MSMQ can easily be seen as a much more scalable solution than any in-house solution developed in the same timeframe. The next chapter deals with a subject that may not directly impinge ondeveloper’s lives now, but by 2005, it is set to overhaul the entire Internet aswe know it, and interoperability with it will become a major selling pointwith future-proof software products. Make way for IPv6.  453 16 IPv6: Programming for theNext-generation Internet  16.1Introduction IPv6 will be the largest overhaul of the Internet since its commercialization.It is due to arrive in 2005 and will incrementally replace the Internet proto-col (IP). Many existing network programs will become obsolete as theybecome incompatible with the emerging networks. This will inevitably cre-ate a great opportunity for network programmers who are familiar with thenew protocol.Such a large overhaul is extremely expensive, but the simple fact is thatthe IP cannot accommodate the explosion in market demand for Internetaccess. In the long run, the migration to IPv6 makes perfect sense and isinevitable. IPv6 will create a bigger, faster Internet that will continue toaccommodate the world’s bandwidth-hungry population into the twenty-second century. Making your application IPv6 compatible from the outsetwill ensure that you will not have to go through a costly overhaul once IPv6becomes mainstream. This chapter is divided into two sections, beginning with a discussion ofIPv6 in general and the utilities you can use to manage IPv6 on your net-work. The chapter concludes with an example of how to communicate overIPv6 from within a .NET application. 16.2What is IPv6? IP addresses are 32 bits long, which provides four billion unique addresses.The number of assigned IP addresses is fast approaching this mark. Con-tributing to this consumption of IP addresses are professionals in the devel-oped world who may have several computers dedicated for their use. Thelargest source of IP wastage is the way in which addresses are assigned in 454 16.3 The history of IPv6 blocks of 256 or 65,355, which could be hundreds or thousands moreaddresses than are required by one organization.IPv6 addresses this issue by extending the address to 128 bits, whichprovides 3 billion billion billion billion unique addresses. Even if theworld’s computer usage were to double every year, it would take 100 yearsfor the same problem to occur again.There is no provision in IPv4 for storing routing information. It is possi-ble for a router to predict the fastest route for a packet many hops inadvance, but the IPv4 packet can only hold the next-hop address, so thereceiving router has to recalculate the fastest route. This consumes routerprocessing power and delays packets unnecessarily.IPv6 can hold routing information in its header and can, therefore, beforwarded through routers with minimal time wastage. Furthermore, theheader is expandable with optional ﬁelds, such that it could hold the rout-ing information the whole way from a computer in Europe to a server inthe United States.IPX was once a strong contender to IP, but it was unfortunately notadopted by router vendors and, thus, cannot be used for Internet commu-nications; however, its unique identiﬁer was based on the hardware (MAC)address and, thus, was easily conﬁgurable, with no need for assignation pro-tocols such as DHCP, unlike IP. A MAC address is 48 bits long, and there-fore can be contained within the IPv6 address. This then negates the needfor ARP and DHCP, thus simplifying the job of the network administrator. Systems that implement security features, such as HTTP-form authenti-cation, are built on top of IP. This leaves security holes open for people whohave the resources to perform IP spooﬁng (i.e., the impersonation of IPaddresses). IPv6 contains built-in headers to protect against IP spooﬁng,with encryption, authentication, and privacy. The ﬁnal difference between IPv6 and IPv4 is the quality-of-service(QoS) provision. In this way, data with absolute priority, such as voice overIP (VOIP) will be forwarded through routers before emails, where it doesn’tmatter if they’re a few seconds late. 16.3The history of IPv6 In 1993, the IETF issued RFC 1550, “IP: Next Generation (IPng) WhitePaper Solicitation.” This could almost be described as a request for tendersfor a replacement of IP. This was followed by a requirements document inRFC 1726, “Technical Criteria for Choosing IP: The Next Generation 16.4 So what changes?455Chapter 16 (IPng).” In January 1995, the initial speciﬁcation for IPng, RFC 1752, wasissued as “The Recommendations for the IP Next Generation Protocol.”Despite the best efforts of the IETF, IPng became commonly known asIPv6 with the release of its ﬁnal speciﬁcation in RFC 1883.The ﬁrst layer 3 protocol to support large addresses was CLNP, more com-monly known as TUBA (TCP and UDP over bigger addresses). This hadhuge 160-bit addresses and was well-established within certain ﬁelds; how-ever, it was inefﬁcient in comparison to IP and lacked the ability to multicast.In 1993, two new protocols emerged: simple IP (SIP) and policy IP(PIP), both of which were extensions on IP. SIP addressed the scalabilityissue, proposing a 64-bit address, whereas PIP addressed policy routing forefﬁciency. The two protocols were merged to form SIPP and extended to128-bit addressing. The ﬁndings were published in 1994. This led to thedevelopment of an experimental protocol, IPv5. This protocol was designedto coexist with IPv4, but used the same addressing scheme. When the limi-tations in IPv5 were addressed, a new protocol, IPv6, emerged.IPv6 will have knock-on effects to routing and auxiliary protocols suchas OSPF, BGP, IDRP, and SNMP. It is predicted that RIP will be replacedby RIP-2 with the rollout of IPv6. 16.4So what changes? IPv6 is backward compatible with IPv4, so there will be a gradual migrationtoward the technology. When the last block of IP addresses is assigned,organizations will be given the chance to use IPv6 instead. This may involvebuying more expensive IPv6-compatible routers, hubs, and switches. Thehigher-level protocols, such as TCP and UDP, will not change, althoughsome higher-level protocols may have some compatibility problems (e.g.,the  PASV  command in FTP).Many applications designed for IPv4 will not work on IPv6, producinga market void in IPv6-compatible software. Because some large ﬁrms, suchas Microsoft, IBM, and Digital Equipment Corporation (DEC), havedeveloped their own IPv6 implementation, it is unlikely that thechangeover will affect their software.The ﬁrst and most obvious difference is the change in the format of theIP address. IPv6 addresses consist of a combination of six identiﬁers: (1) a3-bit format preﬁx (FP), which is always 001 for aggregatable unicastaddresses; (2) the 13-bit top-level aggregator (TLA), which would be anumber assigned to a backbone service provider; (3) an 8-bit reserved 456 16.5 IPv6 naming conventions ﬁeld, set to 0; (4) the 24-bit next-level aggregator (NLA), which wouldrepresent an ISP; (5) the 16-bit site-level aggregator (SLA), which wouldrepresent the subnet; and (6) the 64-bit interface ID, which identiﬁes aspeciﬁc interface on a host. The deﬁnitive description of this format isdescribed in RFC 2374.A global IPv6 address, therefore, takes the following form: [ FP ][ TLA ]:[ Reserved ][ NLA ]:[ SLA ]:[ Interface ID ] 16.5IPv6 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 awk-ward to write 16 three-digit numbers to represent a single IP address.Therefore, a new naming convention is used to quote IPv6 addresses. IPv6addresses are quoted in hexadecimal, not decimal. They are broken into 16-bit segments, rather than 8 bits, as was the case with IPv4. Therefore, onewould write  FFFF:FFFF  rather than  255.255.255.255 . To abbreviate long sequences of zeros, the double colon ( :: ) is used.Sequential blocks of 16 zero bits are replaced with the double colon forbrevity. The IPv6 address  2001:0db8:1000:0000:0000:0000:0000:0027  isabbreviated to  2001:db8:1000::27 .When an IPv6 address encapsulates an IPv4 address, the IPv4 address isrepresented in its standard form within the IPv6 address. An example ofthis would be  ::192.44.75.70 ::ffff:192.44.75.70 .In order to separate the IPv6 preﬁx from the IPv6 interface identiﬁer, itis common practice to append a forward slash followed by the length of thepreﬁx to the end of an IPv6 address. For example, in the case of 2001:db8:2000:240:290:27ff:fe24:c19f/64, the preﬁx is  2001:db8:2000:240. 16.6 Installing IPv6457Chapter 16 16.6Installing IPv6 If you have Windows XP, you can install IPv6 by simply typing  IPv6install  at the command prompt. To test IPv6 on Windows 2000 (ServicePack 1) or later, you need to download an add-on from  www.microsoft.com/windowsserver2003/technologies/ipv6/default.mspx.  You will need an EthernetNetwork adapter, and TCP/IP must be installed. To install IPv6 on Windows 2000, follow these steps:1.Download the add-on from Microsoft.com.2.Click Start  →→→→ Settings  →→→→ Network and Dial-up Connections. 3.Right-click on your Ethernet card and click Properties. 4.Click Install.5.In the Select Network Component Type box, click Protocol andthen click Add.6.In the Select Network Protocol box, click Microsoft IPv5 Protocol. 7.Click OK and then Close.To install IPv6 on previous versions of Windows, links to third-partyvendors are located at  www.ipv6.com.  To uninstall IPv6 from a host, in Windows XP, you simply type  Ipv6uninstall  at the command line and reboot the computer.  16.6.1Auto conﬁguration Similar to the loopback address in IPv4 ( 127.0.0.1 ), you will always begiven an IP address whether you are online or not. In IPv6, the equivalentof the loopback address is the link-local address. This is  FE80::1 .Non-link-local addresses, such as site-local addresses or global addresses,are automatically assigned based on the receipt of IPv6 router advertise-ments (akin to DHCP in IPv4). An IPv6-compatible router is required toissue router advertisements. 458 16.7 Using IPv6 utilities 16.7Using IPv6 utilities If you have Windows XP, you will be happy to know that several handyIPv6 utilities come preinstalled, which is helpful for implementing IPv6applications. 16.7.1IPv6 IPv6 is a command-line-based utility that is similar, in some ways, to ipconfig . To use this application in a meaningful way, you should installthe IPv6 protocol thus: IPv6 install After a few seconds, it should report “succeeded.” At this point, you canview your IPv6 address and other technical information about the IPv6stack. You will be allocated several interfaces, through which you can accessthe Internet. This includes the physical network interface and hybrid inter-faces. You can list the interfaces on your system by typing the following:  IPv6 if To specify an interface, you can type  IPv6 if <number> , such as in Fig-ure 16.1, where interface 4 was a network card.  Figure 16.1 IPv6 MS-DOSutility. 16.7 Using IPv6 utilities459Chapter 16 Global conﬁguration parameters for the IPv6 stack can be viewed bytyping  IPv6 gp . A typical response would be: DefaultCurHopLimit = 128UseAnonymousAddresses = yesMaxAnonDADAttempts = 5MaxAnonLifetime = 7d/24hAnonRegenerateTime = 5sMaxAnonRandomTime = 10mAnonRandomTime = 2m21sNeighborCacheLimit = 8RouteCacheLimit = 32BindingCacheLimit = 32ReassemblyLimit = 262144MobilitySecurity = on To view the preﬁx policy table for the IPv6 stack, you can use  IPv6 ppt ,typical response being the following: ::ffff:0:0/96 -> precedence 10 srclabel 4 dstlabel 4::/96 -> precedence 20 srclabel 3 dstlabel 32002::/16 -> precedence 30 srclabel 2 dstlabel 2::/0 -> precedence 40 srclabel 1 dstlabel 1::1/128 -> precedence 50 srclabel 0 dstlabel 0 16.7.2NETSH This useful utility can be used to read information from the IPv6 stack, aswell as to perform many of the tasks of the previous utility. It is more user-friendly than the  IPv6  utility and provides much the same level of informa-tion (Figure 16.2).To view your IPv6 address using  NETSH , type the following at the com-mand line: Netsh interface ipv6 show address 16.7.3Ping6 There are no prizes for guessing what  ping6  does. It is simply an IPv6implementation of the  ping  utility. It is a stripped-down version of ping,but it uses IPv6 addresses rather than IPv4 addresses (Figure 16.3). Parame-ters that are not supported by the  Ping6  utility (but not necessarily IPv6) 460 16.7 Using IPv6 utilities are TTL, fragment ﬂag, type of service (TOS), and loose and strict routing.The parameters that are supported are listed in Table 16.1.  16.7.4Tracert6 This program is a migration of the  tracert  utility from IPv4 to IPv6 (Figure16.4). Some command-line parameters have changed from the original ver-sion, including the  j ,  s , and  r  command-line parameters. Like  ping6 , Figure 16.2 NETSH MS-DOSutility. Table 16.1 Command-line parameters for Ping6. ParameterPurpose -t Pings the host until the user presses CTRL + C -a Resolves IP addresses to host names -n < count > Sends a speciﬁed number of pings -l Sends buffer size; default is 32 bytes -r Uses the routing header to test the reverse route as well as the forward route -s < address > Uses the speciﬁed source address in the ping requests -w < milliseconds>Discards any replies delayed longer than the speci-ﬁed time16.7Using IPv6 utilities461Chapter 16tracert6 no longer supports loose and strict routing (j parameter), althoughit does support the parameters in Table 16.2.   16.7.5IPSec6 IPSec6 is a utility that manages IP-level security. This is a long-awaitedsecurity feature that effectively makes it impossible for hackers to forge IPaddresses. To view the security policies on your system, you can typeIPSec6 sp at the command prompt. This displays the contents of theIPSec6 security policies database (illustrated in Figure 16.5; described inTable 16.3). You can also view the security associations database (Table16.4) by typing IPSec6 sa at the command prompt.      Figure 16.3Ping6 MS-DOSutility.Table 16.2Command-line parameters for Tracert6.ParameterPurpose-dSuppresses the resolving of IP addresses to domain names-h <hops>Speciﬁes the maximum number of hops between source and destination-w <milliseconds>Discards any replies delayed longer than the speci-ﬁed time-sUses the speciﬁed source address in the ping requests-rUses the routing header to test the reverse route as well as the forward route46216.7Using IPv6 utilitiesFigure 16.4Tracert6 MS-DOSutility.Table 16.3Fields in the IPv6 security policies database. FieldPurposePolicyAn identiﬁer for the policy, similar to a primary key in a databaseRemoteIPAddrThe IP address from which the packet has origi-natedLocalIPAddrThe IP address at which the packet is arrivingProtocolThe higher-level protocol in useRemotePortThe port from which the packet came LocalPortThe port at which the packet arrivedIPSecProtocolThe version of the IPSec protocol in use; default is NONEIPSecModeThe mode of operation of IPSec RemoteGWIPAddrThe remote gateway IP addressSABundleIndexThe security association bundle index; default is NONEDirectionThe direction of travel of the packet. The default is BIDIRECTActionThe action to be taken on any particular packet; default is BYPASSInterfaceIndexThe interface on which the packet arrives 16.7Using IPv6 utilities463Chapter 16Note: The default for all ﬁelds is * (all) unless otherwise speciﬁed.  16.7.6Windows 2000 speciﬁcThe IPv6 implementation comes with some extra utilities, such as 6to4cfgand checkv4. The ﬁrst of those two utilities is for use on hybrid IPv6/IPv4networks. The latter is used to help migrate C++ Windows socket code toFigure 16.5IPSec6 MS-DOSutility.Table 16.4Fields in the IPv6 security associations database.FieldPurposeSAEntryAn identiﬁer for the policy, similar to a primary key in a databaseDestIPAddrThe destination IP address of the packetSrcIPAddrThe source IP address of the packetProtocolThe higher-level protocol in useDestPortThe destination port of the packetSrcPortThe source port of the packetAuthAlgThe authorization algorithm in useKeyFileThe symmetric or public key in useDirectionThe direction of travel of the packetSecPolicyIndexThe security policy index46416.8IPv6 routingIPv6 compliance. C++ network programming code was concerned mainlywith interfacing directly into wsock32.dll, which luckily C# or VB.NETprogrammers are not obliged to do. 16.8IPv6 routingEvery device that runs IPv6 will maintain a routing table, opposed to allrouting information being stored in routers. It is logical that PCs shouldprovide some of the processing power to route packets, rather than leavingit up to routers. When a router encounters an IPv6 packet, it will match the destinationIPv6 address with entries in the destination cache to determine the next-hop address and forwarding interface. If no matching entry is found in thecache, then the destination IPv6 address is compared against the preﬁxes inthe routing table. The closest match with the lowest cost (as determinedfrom the preference or metrics ﬁeld) is used. A routing table consists of eight different ﬁelds:(cid:1)The address preﬁx, similar to a subnet mask, will match speciﬁc IPv6addresses to particular routes. The preﬁx can be any length between 0to 128 bits long. This column is named Prefix in the Windows XProuting table.(cid:1)The network interface for each preﬁx contains an index of the inter-face over which the packet should be retransmitted. This column isnamed Idx in the Windows XP routing table.(cid:1)The next-hop address holds the IPv6 address of the host or router towhich the packet is to be forwarded. This column is named Gateway/Interface Name in the Windows XP routing table. (cid:1)A preference or metric for any particular route is used to provide routerswith a means to select what route to send packets when two routesare available. The preference value could be set manually to reducethe cost of bandwidth billable connections. (cid:1)The route lifetime is an optional ﬁeld that can be used to provide forautomatic routing cache purges. (cid:1)The availability of a routing advertisement is, once again, an optionalﬁeld, but it can be used to determine if the connecting network isIPv6 compliant.16.8IPv6 routing465Chapter 16(cid:1)The aging of the route is an optional ﬁeld that aids in keeping routercaches up to date.(cid:1)The type of route is an optional ﬁeld that can determine if a destina-tion is directly attached, remote, an endpoint host, or the defaultroute.The routing table is constructed automatically and does not requiremanual input, in the same way as the ARP cache of IPv4 computers is notaccessible to users. It is not mandatory for every IPv6 device to maintain arouting table; in fact, it is quite likely that many computers will store onlythe default route (::/0, i.e., an IPv6 address consisting only of zeros) to thenearest router.To go into a little more detail on the route type ﬁeld, four different typesof routes can be stored in the routing table: directly attached, remote, end-point host, or the default route. It can be estimated from the route typehow far the destination is from the router.A directly attached route is where a subnet is physically attached to therouter. In this case, the preﬁx length would typically be 64 bits, and the dis-tance between the router and the destination would typically only be twohops. A remote route is where the packet is forwarded to a subnet or addressspace that is not physically connected. In this case, the preﬁx would be 64bits or less, and the distance to the destination would typically be over twohops.A host route is where the packet is being routed to an endpoint host,such as a computer. In this case, the preﬁx length is 128 bits long, and thedistance would typically be one hop or less. 16.8.1Route determination processWhen the sending host does not specify the source address, then the entirerouting table is checked for matches. If a source address is speciﬁed, thenonly entries for interfaces designated to handle that source address arechecked. The IPv6 destination address is compared against each entry in thetable, looking for matches between the preﬁx and the high-order bits in theIPv6 destination address. Where a preﬁx is only 64 bits long, and therefore46616.8IPv6 routingshorter than the IPv6 address, only the highest 64 bits of the IPv6 addressare relevant for this comparison process.If there is more than one match between the IPv6 destination addressesand the preﬁxes held in the routing table, the preﬁx with the longest lengthis selected. When two matching preﬁxes have the same length, the routewith the lowest cost (as determined from the metrics or preference ﬁeld) isselected. The default route is used only in the case where there are no suc-cessful matches. The route selection provides a next-hop address and a forwarding inter-face. In the case where the next-hop address is not the endpoint host, butanother router, then the next-hop address is stored in the next-hop addressﬁeld in the packet.In the case where the destination host or subnet is unreachable, then therouter returns an ICMPv6 packet to the sender of the packet, stating thatthe host was unreachable. This is analogous to the action of ICMP in IPv4. 16.8.2Administering the IPv6 routing tableThe IPv6 routing table is mostly self-maintaining. It builds entries fromrouting advertisements from other routers and phases out out-of-date orout-of-service routes over time. In the case where the network infrastructurehas changed signiﬁcantly, however, or for experimentation purposes, it maybe necessary to conﬁgure the routing table of an IPv6 router manually, or inthis case, a Windows XP server running the Internet connection sharing(ICS) service.To show the IPv6 routing table (Figure 16.6) on your PC, type the fol-lowing at the command line:netsh interface ipv6 show routes If any routes are stored in the routing table, information will follow theform described in Table 16.5. Assume that you wish to add a route to the routing table, where youwant packets with a destination IPv6 address starting with 3ffe to be for-warded to the loopback address (fe80::1). For debugging purposes, we canname this route a. Therefore, you may use the following syntax: netsh interface ipv6 add route 3ffe::/16 "a" fe80::116.8IPv6 routing467Chapter 16Figure 16.6IPv6 routing table.Table 16.5Fields in the IPv6 routing table.FieldPurposePublishSpeciﬁes whether the route is advertised in a rout-ing advertisement message. MetSpeciﬁes the metric or preference used to select between multiple routes with the same preﬁx. The lowest metric is the best matching route. PrefixSpeciﬁes the address preﬁx; it can have a length between 0 and 128 bits. Idx Speciﬁes the interface index, over which packets that match the address preﬁx may be transferred. Interface indexes can be viewed by typing netsh interface ipv6 show interface at the command prompt. Gateway/Interface NameSpeciﬁes either an interface name or a next-hop IPv6 address. Remote network routes list a next-hop IPv6. Directly attached network routes list the name of the relevant interface.Type Speciﬁes the type of the route. Routes conﬁgured by user applications are listed with a route type of Manual. Routes conﬁgured by the IPv6 protocol are listed with a route type of Autoconf. 46816.8IPv6 routingTo remove this link, you can use the following command:netsh interface ipv6 delete route 3ffe::/16 "a" fe80::1The IPv6 addresses assigned to interfaces can also be changed. ExtraIPv6 addresses can be added to each interface if required. To give a quickexample, if you ping6 the IPv6 address fe80::10, it will fail, but if theaddress is assigned to your local computer thus:netsh interface ipv6 add address "loopback" fe80::10if you ping6 fe80::10, the ping will succeed because your computer is nowmultihomed with a second IPv6 address. To remove this address, you canuse the command:netsh interface ipv6 delete address "loopback" fe80::1016.8.3IPv6 routing advertisementsAs previously mentioned, the IPv6 routing table is generally populatedfrom received advertisements from other routers. These advertisementscontain information about the advertising router’s subnet and optionalinformation such as the preﬁx information option and the route informa-tion option.The preﬁx information option is described deﬁnitively in RFC 2461. Itprovides the receiving router with the address preﬁx from which the auto-conﬁgured addresses derive. The route information option is speciﬁed in an Internet draft entitled“Default Router Preferences and More-Speciﬁc Routes.” This optional datacontains preﬁx and routing information for subnets accessible from theadvertising router. This provides a redundancy mechanism, such that if therouter that sent the advertisement ever goes ofﬂine, it may still be possibleto communicate with subnets behind that router if an alternative routeexists.A Windows XP machine will not send routing advertisements on thenetwork by default, but it can be set to do so with the following command:netsh interface ipv6 set interface <interface id> forwarding=enabled advertise=enabled16.9IPv6 coexistence469Chapter 16where the interface ID can be determined from the following command,under the Idx ﬁeld: Netsh interface ipv6 show interface16.9IPv6 coexistenceIPv6 will not replace IPv4 overnight. Instead, there will be a considerableperiod when both IPv6 and IPv4 will coexist on the Internet. In time, IPv4will be phased out entirely. This transitional period is important for devel-opers of mass-market software where the network infrastructure of the end-user can be one of several different hybrid systems.IPv6 deployment will take place in a much more organized way than theMBONE project did. The ad hoc deployment of MBONE created islandsof multicast supporting infrastructure and left entire countries with noaccess to multicast networks. With IPv6, networks that have not yetmigrated to IPv6 will still be capable of transporting IPv6 data, once it isencapsulated within IPv4 packets.The encapsulation of IPv6 data within IPv4 packets can take place usingone of two mechanisms: the intrasite automatic tunnel addressing protocol(ISATAP) or 6to4. The crucial difference between these two systems is theIPv4 address that each uses to represent the destination. An IPv6 addressconsists of an interface and subnet identiﬁer. With 6to4, this subnet identi-ﬁer is created from a public IPv4 address, whereas ISATAP uses a local IPv4address to create the subnet identiﬁer. Regardless of the mechanism used,the IPv4 packet will contain enough information to determine the sourceand destination of the packet. Having said that, there will be designatedpockets of the Internet that natively support IPv6, known as the 6bone.This region of the Internet will support more efﬁcient and faster transfer ofIPv6 data, but IPv6 accessibility will not be limited only to this region. 16.9.1The 6to4 protocolThe 6to4 mechanism is an elegant solution to ferry IPv6 data across IPv4networks. It is described deﬁnitively in RFC 3056. Although hosts employ-ing 6to4 address assignment would not require any manual conﬁguration, itis likely that 6to4-compliant routers will require some level of conﬁguration.6to4 generates a global IPv6 address from an IPv4 address by sufﬁxingthe IPv4 address with a global address preﬁx (2002 hex), appending the47016.9IPv6 coexistenceIPv4 address, and retaining the subnet and interface identiﬁer. This createsan IPv6 address of the form:2002 : HIGH IPv4 : LOW IPv4 : SUBNET : INTERFACEwhere high IPv4 is the high-order 16 bits from the IPv4 address and lowIPv4 is the low-order 16 bits from the IPv4 address. This type of address isnot suited to multicast applications.The high 64 bits of the 6to4 address (which includes the subnet) areused to provide routing information between 6to4-compliant routers.Routers advertise this route to other routers within the IPv6 intranet andalso to 6to4 routers outside the network.Any 6to4 network trafﬁc that does not belong within the intranet is for-warded to a router on the border of the intranet. This data then has to beencapsulated into an IPv4 packet because non-IPv6-compliant routerswould simply discard native IPv6 packets. The 6to4 router would extractthe IPv4 address from the IPv6 address and use it in the header to direct thepacket.In Windows XP, the 6to4 service is included with the IPv6 protocol.This service can be administered from Administrative Tools→→→→Services→→→→6to4. This service automatically generates 6to4 addresses for all IPv4addresses assigned to the local computer. It also provides a facility to encap-sulate IPv6 packets with IPv4 headers when required and attempts to locatea 6to4 relay router on the Internet using a DNS query.If ICS were enabled on that computer, as it would be with many homeor small ofﬁce networks, the computer will enable IPv6 forwarding on theprivate interface and broadcast router advertisements, such that any otherIPv6 hosts on the network could communicate natively. This effectivelyconﬁgures that computer to become a 6to4 router.Note: Technically, neither the ICS nor 6to4 service provides IPv4/IPv6address translation, but for all practical purposes, it can be seen as such.In situations where you cannot or do not want to run ICS on the server,it is possible to conﬁgure the machine as a 6to4 router without using ICS.In order to conﬁgure the computer as a 6to4 router, it must have a publiclyaccessible IPv4 address and must not have a 6to4 pseudointerface in itsrouting table. A 6to4 pseudointerface is created when a host computer16.9IPv6 coexistence471Chapter 16receives a routing advertisement from an IPv6 or ISATAP router. This gen-erates an entry in the routing table, preﬁxed 2002::/16 and pointing to a6to4 relay router on the IPv4 Internet. Enable forwarding and advertising on all interfaces connected to theInternet, and enable forwarding on the 6to4 pseudointerface using the fol-lowing command (with the interface ID substituted accordingly):netsh interface ipv6 set interface <interface id> forwarding=enabled advertise=enabledAdd routes to the 6to4 pseudointerface using the following command(with interface ID, subnet ID, and the IPv4 address substituted accord-ingly):Netsh interface ipv6 add route 2002:<High bytes IPv4>:<Low bytes IPv4>:<Subnet ID>::/64 <interface ID> publish=yesThe subnet ID is, by convention, the index of the interface being used,but it can be any two-byte hex number. 16.9.2The ISATAP protocolISATAP serves much the same purpose as 6to4 in providing a means totransport IPv6 data over the existing IPv4 architecture. It has not gained thesame level of support as 6to4, but it still remains a valid and usable technol-ogy. An ISATAP address uses the following form: ::0:5EFE:[IPv4 Address]The ISATAP address can be combined with an IPv6 preﬁx, including6to4 preﬁxes if required. Again, ISATAP is not suited to multicast applica-tions.On Windows XP, the ISATAP address for each local interface is auto-matically conﬁgured. This address enables ISATAP hosts to communicateover IPv4 within the same intranet. To allow communication over theInternet, the host must have previously received a router advertisementfrom the border router from the foreign network. Once this advertisementhas been received, the host can then determine other ISATAP addressesfrom within that network.47216.9IPv6 coexistenceISATAP uses an IPv4 address to derive the interface ID. Because itdefeats the purpose to have the uniqueness of an IPv6 address depend onthe availability of a unique IPv4 address, it is possible to have two identicalISATAP interface IDs for two hosts in separate sites because they wouldboth have different IPv6 addresses, thanks to the unique subnet ID.To conﬁgure a computer as an ISATAP router, it must have at least twoLAN connections, one of which is connected to the IPv6 network andanother connected to the IPv4 network. The ﬁrst step in conﬁguring aWindows XP machine as an ISATAP router is to enable forwarding on allinterfaces connected to the Internet and enable both forwarding and adver-tising on the automatic tunneling pseudointerface, using the followingcommand (with the interface ID substituted accordingly):netsh interface ipv6 set interface <interface id> forwarding=enabled advertise=enabled Add routes to the automatic tunneling pseudointerface using the follow-ing command (with interface ID, subnet ID, and the IPv4 address substi-tuted accordingly):Netsh interface ipv6 add route 2002:<High bytes IPv4>:<Low bytes IPv4>:<Subnet ID>::/64 <interface ID> publish=yesAdd a default route to the physical LAN interface using the followingcommand (with interface ID and the IPv6 address substituted accordingly):Netsh interface ipv6 add route ::/0 <interface id> nexthop=<IPv6> publish=yesISATAP hosts use DNS to discover the ISATAP router by resolving thename ISATAP (Windows Server 2003) or _ISATAP (Windows XP). It is there-fore necessary either to name the router computer ISATAP or to enter therecord into the local DNS server(s). If this cannot be done, you must run thefollowing command on all ISATAP hosts and the router computer itself:Netsh interface ipv6 isatap set state router=<IPv4 Address>16.10IPv6 in .NET473Chapter 1616.9.3The 6over4 protocol6over4 and 6to4 are similar protocols, but they are different in some signif-icant respects. They are both used to ferry IPv6 data over IPv4 networks;however, 6over4 is designed for use in multicast environments, unlike 6to4or ISATAP. 6over4 is deﬁned deﬁnitively in RFC 2529.6over4 is disabled by default on Windows XP, but it can be enabled withthe command line (substituting the IPv4 address with that of the local com-puter):netsh interface ipv6 add 6over4tunnel "6over4" <ipv4 address>From this point, 6over4 can be conﬁgured in an identical way toISATAP; however, it should be noted that the underlying IPv4 networkmust already support multicasting for IPv6 multicasting to be functional. 16.10IPv6 in .NETIPv6 is supported in version 1.1 of .NET. Migrating code to support IPv6isn’t difﬁcult. All that is required is a new parameter to be passed to thesocket constructor AddressFamily.InterNetworkV6. Locally bound end-points are created differently, too, using IPAddress.IPv6Any as the loop-back address.Support for IPv6 in .NET 1.1 is disabled by default, so before you canbegin programming, you need to adjust the conﬁguration for .NET. Usinga text editor, open the machine.config ﬁle, located in the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 folder. Replace the XML <!--<ipv6 enabled="false "/> --> with <ipv6 enabled="true"/>.The following example demonstrates how to transmit some text viaTCP/IPv6. There are two parts to this example: a client and a server. Weshall start by ﬁrst implementing the server. Create a new project in VisualStudio .NET. Draw a textbox on the form, named tbMessages, withmultiline set to true. First, add a public variable named port. This variable will hold the portnumber on which the server will listen:C#public class Form1 : System.Windows.Forms.Form{47416.10IPv6 in .NET  public static int port;VB.NETPublic Class Form1    Inherits System.Windows.Forms.Form    Public Shared port As IntegerThis server is dual threaded, where the main thread is used to keep the UIresponsive, and a secondary “worker” thread is used to accept and handleincoming connections. This server would be incapable of handling morethan one connection at a time. In the form Load event, the port is set to9999, and the worker thread is started.C#private void Form1_Load(object sender, System.EventArgs e){  port = 9999;  Thread thdListener = new Thread(new ThreadStart(listener));  thdListener.Start();}VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load  port = 9999  Dim thdListener As Thread = New Thread( _          New ThreadStart(AddressOf listener))  thdListener.Start()End SubThe Listener function works by binding a socket to the IPv6 loopbackaddress on port 9999. It uses Socket.SupportsIPv6 to determine if the.NET run time is capable of handling IPv6. If so, the socket is bound to theaddress and begins listening. Once a connection is received, a new socket iscreated, which will read data from the network in 256-byte chunks. It out-puts all data to the screen immediately. Once there is no more data, bothsockets close, and the server will no longer accept connections. C#public void listener(){16.10IPv6 in .NET475Chapter 16  Socket sckListener;  Socket clientSocket;  IPEndPoint ipepLocal =   new IPEndPoint(IPAddress.IPv6Any, port);  byte[] RecvBytes = new byte[Byte.MaxValue];      Int32 bytes;        if(!Socket.SupportsIPv6)   {    MessageBox.Show("Cannot support IPv6");    return;  }  sckListener = new Socket(  AddressFamily.InterNetworkV6, SocketType.Stream,   ProtocolType.Tcp  );  sckListener.Bind(ipepLocal);  sckListener.Listen(0);  clientSocket = sckListener.Accept();        while(true)        {    bytes = clientSocket.Receive(RecvBytes);    if (bytes<=0) break;    tbMessages.Text += Encoding.ASCII.GetString(RecvBytes);  }        tbMessages.Text += "\n";  clientSocket.Close();  sckListener.Close();}VB.NETPublic Sub listener()  Dim sckListener As Socket  Dim clientSocket As Socket  Dim ipepLocal As IPEndPoint = New _    IPEndPoint(IPAddress.IPv6Any, port)  Dim RecvBytes() As Byte = New Byte(Byte.MaxValue) {}  Dim bytes As Int32  If (Not Socket.SupportsIPv6) Then    MessageBox.Show("Cannot support IPv6")    Return  End If47616.10IPv6 in .NET  sckListener = New Socket( _   AddressFamily.InterNetworkV6, SocketType.Stream, _     ProtocolType.Tcp)  sckListener.Bind(ipepLocal)  sckListener.Listen(0)  clientSocket = sckListener.Accept()  Do    bytes = clientSocket.Receive(RecvBytes)    If bytes <= 0 Then Exit Do    tbMessages.Text += Encoding.ASCII.GetString(RecvBytes)  Loop  tbMessages.Text += vbCrLf  clientSocket.Close()  sckListener.Close()End SubYou will also need to add the following namespaces to your code:C#using System.Net;using System.Net.Sockets;using System.Threading;using System.Text;VB.NETImports System.NetImports System.Net.SocketsImports System.ThreadingImports System.TextOnce the server is written, let us turn our attention to implementing theclient. Open a new project in Visual Studio .NET. Draw two textboxes onthe form: tbIPv6Addr and tbMessage. Then add a button named btnSend. First, add a public variable named port. This variable will hold the portnumber, on which the server will listen:C#public class Form1 : System.Windows.Forms.Form{  public static int port;16.10IPv6 in .NET477Chapter 16VB.NETPublic Class Form1    Inherits System.Windows.Forms.Form    Public Shared port As IntegerAs in the server, the port needs to be set to 9999. This is set from withinthe form Load event.C#private void Form1_Load(object sender, System.EventArgs e){  port = 9999;}VB.NETPrivate Sub Form1_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load  port = 9999End SubNow, double-click on the Send button, and enter the code below. Thisworks by creating a socket with an address type of IPv6. This socket estab-lishes a connection with the remote server in the normal way and transfersthe string by converting it to a byte array.C#private void btnSend_Click(object sender, System.EventArgs e){  byte[] msg;   if(!Socket.SupportsIPv6)   {    MessageBox.Show("Cannot support IPv6");    return;  }  IPAddress ipAddrv6 = IPAddress.Parse(tbIPv6Addr.Text);  IPEndPoint ipEPv6 = new IPEndPoint(ipAddrv6, port);  Socket socket = new Socket(   AddressFamily.InterNetworkV6, SocketType.Stream,   ProtocolType.Tcp  );47816.10IPv6 in .NET  socket.Connect(ipEPv6);  msg = Encoding.ASCII.GetBytes(tbMessage.Text);  socket.Send(msg);  socket.Close();}VB.NETPrivate 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(tbMessage.Text)  ipv6Socket.Send(msg)  ipv6Socket.Close()End SubYou will also need to add the following namespaces to your code:C#using System.Net;using System.Net.Sockets;using System.Text;VB.NETImports System.NetImports System.Net.SocketsImports System.TextTo test this example, run the server from Visual Studio .NET. Find the IPv6address of the server computer, using the Ipv6 if command. If you are16.11Conclusion479Chapter 16using only one computer, then this is FE80::1. Now run the client pro-gram, enter the server IPv6 address into the box provided, and type a mes-sage into the second box. Press Send, and the message should appear in thetext window of the server, as depicted in Figure 16.7.16.11ConclusionIPv6 compliance may not be a big issue today, but in the near future it willbe key and may be an absolute requirement in certain software products.This chapter should provide enough information for you to set up anexperimental IPv6 network and provide a test platform for IPv6 compliancein software. It should also give insight into how to upgrade legacy protocolswithout alienating existing clients. IPv6 may be a headache for network administrators, but it is a goldmine of opportunity for developers. When IPv4 begins to make its exitfrom the Internet, it is almost certain that people will start looking at otherprotocols. FTP, for instance, is incompatible with IPv6 because of IPv4-dependent features of the protocol, such as the PASV command. Anotherexample is X.25, which currently forms the backbone of ﬁnancial institu-tions, but has no native mechanism to tunnel over IPv6, so it may also bereplaced. The list is virtually endless.The ﬁnal chapter deals with two of the technologies introduced with.NET that have been heralded as a revolution in distributed computing:remoting and Web services. Figure 16.7TCP/IPv6 clientand server.This page intentionally left blank  481 17 Web Services and Remoting          17.1Introduction Web services are one of the most hyped features of .NET. They facilitate thesharing of information and services among companies and individuals on aprogrammatic level more elegantly than any other existing technology. Thereal beneﬁt of using Web services in Visual Studio .NET is that you requirenothing more than a simple URL to begin coding against a Web serviceresiding on a remote server as if it were a local object. This cuts out thecomplexity of establishing network connections, formatting requests, andparsing replies. From a business perspective, Web services can drastically reduce thedevelopment and integration time in rolling out afﬁliate programs. Onlineretailers can make their price listings available publicly via Web services toenable third parties to resell their products, knowing that items sold are cur-rently in stock. Although Web services may make information freely available, thisdoes not imply that all of these services are free. Pay-per-use Web servicessuch as SMS, credit card processing, and postal address lookup can bebought and used by third parties as part of larger applications with mini-mal effort.  17.2Creating a Web service In order to create a Web service, you will require access to an IIS server withthe .NET framework installed. You will need administrative rights on thisserver to develop the Web service directly from Visual Studio .NET. Whenyou install VS.NET, it will install and conﬁgure IIS for you. 482 17.2 Creating a Web service  This ﬁrst Web service is used to report server variables from the serverthat hosts the service. This may not seem immediately useful, but one ofthe server variables ( REMOTE_HOST ) indicates the remote IP address of theclient connecting to it. This information is useful to determine if a client isrunning behind a ﬁrewall or proxy, because in this case the local IP addresson the client will not be the same as the IP address that would connect to aremote server. There is no easy way to determine this IP address using onlycode running on the client.Another use of tracking the requester’s IP address is to limit the numberof queries made against a service in any one day. This effectively prohibitsdata mining, but it could be a hindrance when many users use the servicebehind the same outgoing proxy or ﬁrewall. Start a new project in Visual Studio .NET, selecting a project of typeASP.NET Web Service. The default path for this new project is on the localIIS server ( http://localhost ).  Note:  If you receive an error concerning the “debugging users group” onIIS, this generally means you have not enabled Integrated Windows Authentication under Directory Security on the server. A server will have many variables associated with it, although the namesof these variables do not change from server to server; for ﬂexibility, we canprovide a method that returns an array of all the server variables stored onthis machine. Enter the following code in the  asmx  ﬁle: C# [WebMethod]public String[] getServerVariableNames(){  System.Collections.Specialized.NameValueCollection col;         col=Context.Request.ServerVariables;         String[] arr = col.AllKeys;           return arr;      } VB.NET <WebMethod> _ Public Function getServerVariableNames() As String() 17.2 Creating a Web service483Chapter 17   Dim col As _    System.Collections.Specialized.NameValueCollection _    col=Context.Request.ServerVariables     Dim arr() As String =  col.AllKeys   Return arrEnd Function Notice the  [WebMethod]  attribute placed before the function name. Thisexposes the function for use over the Internet. The array returned from thismethod would be instantly recognizable by any ASP or ASP.NET devel-oper. It would include strings such as  REMOTE_HOST ,   representing the IPaddress of the client, and  HTTP_USER_AGENT , representing the software beingused by the client. To retrieve the value of each of these variables, we can implement a sec-ond function as follows:  C# [WebMethod]public string[] getServerVariable(string variableName){  System.Collections.Specialized.NameValueCollection col;  col=Context.Request.ServerVariables;         String[] arr = col.GetValues(variableName);  return arr;} VB.NET <WebMethod> _ Public Function getServerVariable(ByVal variableName As _  String) As String()  Dim col As _  System.Collections.Specialized.NameValueCollection _  col=Context.Request.ServerVariables         Dim arr() As String =  col.GetValues(variableName)   Return arrEnd Function This function returns the value of a server variable when passed itsname. It returns an array because some server variables return more thanone result. To cite an example,  HTTP_ACCEPT , the variable that enumerates 484 17.2 Creating a Web service the MIME types that the browser can render, will generally return an arrayof several different ﬁle types. To test this service, run it from Visual Studio .NET, and you will see abrowser open with an automatically generated Web page that details thepublic functions of your Web service. This Web page should be used fordebugging purposes only because the default security setting is that theHTML interface is only available to browsers running on the localmachine. End-consumers will use a programmatic interface to access thisservice. Click on  getServerVariableNames  and then Invoke. You will see a new browser window opening with XML content, asshown in Figure 17.1. The XML is formatted as SOAP.Take note of a server variable of interest, such as  REMOTE_ADDR . PressBack on the ﬁrst browser window, and select  getServerVariable . Enter thename of the server variable in the box provided, and press Invoke. You willsee a new window open and the Web service’s XML response encoded asSOAP.You will notice the URL in the address bar changing as you navigatewithin this interface to the Web service. An HTTP  GET  request in the fol-lowing format can be used to invoke a Web service method:  Figure 17.1 SOAP resultreturned from aWeb service. 17.2 Creating a Web service485Chapter 17 http://[ ASMX file ]/[ function name ]?[ function parameters ] It is possible to use a  GET  request to invoke a Web service programmati-cally, but this is ill advised. Using the query string to pass objects is onlypossible with primitive types, and there are better ways to use a Web serviceprogrammatically.Another HTTP  GET  request can be made against the Web service in theform http://[ ASMX file ]?WSDL This displays the formal deﬁnition of the Web service in Web ServiceDeﬁnition Language (WSDL) format. The WSDL deﬁnition allows VisualStudio .NET to determine the methods exposed by the Web service and togenerate a suitable wrapper or proxy class for it. This step is generally donebehind the scenes, but for the interested reader, it is possible to perform thisstep manually using the WSDL.EXE utility provided with .NET. The call-ing syntax is as follows: WSDL http://[ ASMX file ]?WSDL This will generate a C# proxy class in the same folder as WSDL.EXE. Togenerate a VB.NET class, precede the URL with  /Language:VB . 17.2.1Deploying a Web service Having a Web service running on your local machine is ﬁne for develop-ment purposes, but in order to make the service meaningful, it should beuploaded to a publicly accessible IIS server. Web services that are deployedpublicly must have a unique namespace to distinguish them from otherWeb services on the Internet. Coding convention dictates that thenamespace should be in the form of a domain name that you control. Thenamespace may look like a URL, but it does not need to point to anythingon the Web in particular.  C# [WebService(Namespace="http://www.myserver.com/")] 486 17.3 Using a Web service VB.NET <WebService(Namespace:="http://www.myserver.com/")> _ If you want to make it easy for people to ﬁnd your Web service, one ofthe ﬁrst places you should advertise it is at  http://uddi.Microsoft.com  or http://test.uddi.Microsoft.com . These are public repositories for Web ser-vices and generally the ﬁrst place developers go when looking for a partic-ular online service. Universal description discovery integration (UDDI) is an open standardthat can be accessed programmatically by using the  Microsoft.Uddi.Sdk namespace provided with the UDDI SDK.   17.3Using a Web service As mentioned earlier, the automatically generated Web interface for a Webservice is not designed for public use. Instead, you generate a proxy classthat accesses the service programmatically, and you can code against theWeb service as if you are using a local object. In Visual Studio .NET, you don’t need to code a proxy class yourself; itwill be created for you. All you need to do is enter the URL of the Web ser-vice, and all of the behind-the-scenes work is taken care of. Start a new project in Visual Studio .NET and select Windows FormsApplication.   Click Project  →→→→ Add Web Reference, and then enter the URL ofthe ASMX ﬁle created in the previous example. Press Add Reference onceyou have found the Web service. In the following example, the Web serviceis assumed to reside on the local machine and to be named  Service1 .Draw a list view on the form, and name it  lvServerVariables . A but-ton named  btnPopulate  is also required. Click on the form and add the following code: C# private void Form1_Load(object sender, System.EventArgs e){  lvServerVariables.View=View.Details;  lvServerVariables.Columns.Add("Name",     lvServerVariables.Width/2,     HorizontalAlignment.Left);  lvServerVariables.Columns.Add("Value", 17.3 Using a Web service487Chapter 17    lvServerVariables.Width/2,   HorizontalAlignment.Left);} VB.NET Private Sub Form1_Load(ByVal sender As Object, ByVal _  e As System.EventArgs)  lvServerVariables.View=View.Details  lvServerVariables.Columns.Add("Name", _     lvServerVariables.Width/2, _     HorizontalAlignment.Left)  lvServerVariables.Columns.Add("Value", _     lvServerVariables.Width/2, _     HorizontalAlignment.Left)End Sub This code simply lays the list view out on the screen in a neat way, withthe column headers equally spaced across the screen.Click on the Populate button, and add the following code:  C# private void btnPopulate_Click(object sender, System.EventArgs e){  string[] serverVariableNames;  localhost.Service1 webservice = new localhost.Service1();  serverVariableNames = webservice.getServerVariableNames();  lvServerVariables.Items.Clear();  foreach (string serverVariableName in serverVariableNames)        {    ListViewItem lvItem = new ListViewItem();    lvItem.Text = serverVariableName;    string[] serverVariableValues;    serverVariableValues =     webservice.getServerVariable(serverVariableName);    if (serverVariableValues!=null)    {      lvItem.SubItems.Add(serverVariableValues[0]);      }    lvServerVariables.Items.Add((ListViewItem)lvItem.Clone());    }} 488 17.3 Using a Web service VB.NET Private  Sub btnPopulate_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  Dim serverVariableNames() As String  Dim webservice As localhost.Service1 = New _    localhost.Service1  serverVariableNames = webservice.getServerVariableNames()  lvServerVariables.Items.Clear()  Dim i As Integer  For each serverVariableName as string in _    serverVariableNames    Dim lvItem As ListViewItem = New ListViewItem    lvItem.Text = serverVariableName    Dim serverVariableValues() As String    serverVariableValues = _       webservice.getServerVariable(serverVariableName)    If Not serverVariableValues Is Nothing Then      lvItem.SubItems.Add(serverVariableValues(0))    End If    lvServerVariables.Items.Add(CType(lvItem.Clone(), _      ListViewItem))  NextEnd Sub This code would seem to have nothing to do with networking code, butin fact, it communicates extensively with the remote server via the proxyclass every time a method is called on the  webservice  object.If you would like to view the proxy class, you can click on show all ﬁlesin the Solution Explorer, and click Localhost  →→→→ Reference.map  →→→→  Refer-ence.cs. It is not advisable to edit the proxy class manually.The rest of the code above is concerned with displaying the datareturned from the Web service on-screen. Only the ﬁrst element in thearray returned from  getServerVariable  is actually rendered on-screen, forthe sake of simplicity. To test the Web service client, run it from Visual Studio .NET, ensurethat IIS is running on the local machine, and then press Populate. Youshould see a list appearing on-screen, which should resemble Figure 17.2. 17.4 Asynchronous calls to Web services489Chapter 17 17.4Asynchronous calls to Web services If the same Web service were deployed on several geographically separatedWeb servers, clients could connect to several Web services at once in orderto improve performance. This may only be applicable in situations whereseveral calls have to be made and each call takes a signiﬁcant amount oftime to complete.To understand the scenario, we could envisage a situation where anapplication displays live stock values of a large share portfolio. A Web ser-vice is hosted on a server in the United States, which is linked into theNASDAQ exchange, and another server is located in Japan, which is linkedinto the Nikeii exchange. A customer in question has shares in Microsoftand Toyota. If the client were to issue a request for the value of theMicrosoft shares, wait for the response, and then request the value of theToyota shares, the process would take twice as long as if both requests weremade simultaneously.Several techniques can be used to manage simultaneous Web servicecalls. The following code examples perform the same function: They maketwo calls to a Web service and measure the response times to the calls. IIS ismultithreaded, so it handles both of these requests in parallel. In a real-world example, the same Web service would be mirrored on more than oneserver, so that the two requests would be handled at exactly the same time. Figure 17.2 Web service clientapplication. 490 17.4 Asynchronous calls to Web services Each of the following samples requires a simple user interface consist-ing of only a button and a label. To create this interface, open a newproject in Visual Studio .NET, and select a Windows form application.Draw a button on the form and name it  btnMakeCall  and then draw alabel named  lblStatus . You will also require a Web reference to the Web service as described ear-lier in this chapter. This Web reference should be named  localhost , for thepurposes of these code examples. The Web service does not necessarily needto be hosted on the local machine. 17.4.1Wait handles A wait handle is equivalent to a  do-nothing while  loop using polling, butit is less processor intensive. This should only be used in a separate thread,or the client application will be nonresponsive to the user. This techniqueshould only be used when useful client-side processing can be performedbefore data is returned from any of the Web services. Click on the Make Call button and enter the following code: C# private void btnMakeCall_Click(object sender, System.EventArgs e){  long timeStart = DateTime.UtcNow.Ticks;  localhost.Service1 svc = new localhost.Service1();  IAsyncResult result1;  IAsyncResult result2;  result1 = svc.BegingetServerVariableNames(null,null);  result2 =  svc.BegingetServerVariable("REMOTE_ADDR",null,null);  result1.AsyncWaitHandle.WaitOne();  result2.AsyncWaitHandle.WaitOne();  string[] varNames = svc.EndgetServerVariableNames(result1);  string[] response = svc.EndgetServerVariable(result2);  lblStatus.Text = "Time elapsed:" +   (DateTime.UtcNow.Ticks - timeStart);  lblStatus.Text += " ticks";} VB.NET Private  Sub btnMakeCall_Click(ByVal sender As Object, _ 17.4 Asynchronous calls to Web services491Chapter 17   ByVal e As System.EventArgs)  Dim timeStart As Long =  DateTime.UtcNow.Ticks   Dim svc As localhost.Service1 =  New localhost.Service1()   Dim result1 As IAsyncResult  Dim result2 As IAsyncResult  result1 = svc.BegingetServerVariableNames( _      Nothing,Nothing)  result2 = _     svc.BegingetServerVariable( _     "REMOTE_ADDR",Nothing,Nothing)  result1.AsyncWaitHandle.WaitOne()  result2.AsyncWaitHandle.WaitOne()  Dim varNames() As String = _     svc.EndgetServerVariableNames(result1)   Dim response() As String = _     svc.EndgetServerVariable(result2)   lblStatus.Text = "Time elapsed:" & _     (DateTime.UtcNow.Ticks - timeStart)  lblStatus.Text += " ticks"End Sub To test this code, run the application from Visual Studio .NET, andpress the make Call Button. The user interface will become unresponsiveuntil the call is received. In a production environment, the code detailedabove should be contained within a separate thread. 17.4.2CallbacksCallbacks produce the least amount of processor overhead while waiting forWeb service calls to return. They are ideal in situations where no useful cli-ent-side processing can be performed before all of the data is received; how-ever, it could be difﬁcult to determine when the last call has returnedsuccessfully or erroneously. Click on the Make Call button and enter the following code:C#public localhost.Service1 svc;public long timeStart;private void btnMakeCall_Click(object sender, System.EventArgs e){49217.4Asynchronous calls to Web services  timeStart = DateTime.UtcNow.Ticks;  svc = new localhost.Service1();  svc.BegingetServerVariableNames(new         AsyncCallback(ServiceCallback1),null);           svc.BegingetServerVariable("REMOTE_ADDR",new              AsyncCallback(ServiceCallback2),null);}private void ServiceCallback1(IAsyncResult result){  string[] response = svc.EndgetServerVariableNames(result);      lblStatus.Text = "Time elapsed:" +   (DateTime.UtcNow.Ticks - timeStart);  lblStatus.Text += " ticks";}private void ServiceCallback2(IAsyncResult result){  string[] response = svc.EndgetServerVariable(result);      lblStatus.Text = "Time elapsed:" +   (DateTime.UtcNow.Ticks - timeStart);  lblStatus.Text += " ticks";}VB.NETPublic svc As localhost.Service1Public timeStart As LongPrivate Sub btnMakeCall_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  timeStart = DateTime.UtcNow.Ticks  svc = New localhost.Service1()  svc.BegingetServerVariableNames(New _       AsyncCallback(AddressOf ServiceCallback1),Nothing)  svc.BegingetServerVariable("REMOTE_ADDR",New _      AsyncCallback(AddressOf ServiceCallback2),Nothing)End SubPrivate  Sub ServiceCallback1(ByVal result As IAsyncResult)  Dim response() As String = _     svc.EndgetServerVariableNames(result)   lblStatus.Text = "Time elapsed:" & _17.5Interoperability493Chapter 17  (DateTime.UtcNow.Ticks - timeStart)  lblStatus.Text += " ticks"End Sub Private  Sub ServiceCallback2(ByVal result As IAsyncResult)  Dim response() As String = _    svc.EndgetServerVariable(result)   lblStatus.Text = "Time elapsed:" & _    (DateTime.UtcNow.Ticks - timeStart)  lblStatus.Text += " ticks"End SubTo test this code, run the application from Visual Studio .NET, andpress the Make Call button. The time displayed is the time that has elapsedbetween the issuing of the Web methods and the last response received.  Amore robust solution would be to use a global array that would track theprogress of each call.The BeginGetServerVariableNames function takes two parameters; theﬁrst parameter indicates the procedure to be called once the web-methodreturns, and the second, as shown in the code example above, is set to nullor Nothing. This parameter can optionally contain objects that can bepassed to the callback via the result object. 17.5InteroperabilityWhen developing a Web service, it should be straightforward for any devel-oper working on any platform to implement a client. The previous exampleshould demonstrate that it is easy to implement a Web service client in.NET, but if your service is to be made available to third-party Web sitedevelopers, you have to make sure that you do not needlessly complicatetheir job simply for the sake of using this new buzzword in Web technology.Although it may not seem like your responsibility to support third-party developers that integrate into your software, it would be lunacy (andbad for business!) to provide a service that was so difﬁcult to use from plat-forms other than .NET that developers would simply give up and ﬁnd adifferent supplier.  Most languages now support XML. With this, it is easy to extract prim-itive types such as strings, numbers, and arrays from SOAP responses;however, if complex objects such as datasets and nested classes are rendered49417.6Performanceas SOAP, it is likely that the average PHP Web developer will throw hishands up in despair. Therefore, if it is envisaged that there may be a userbase that may not use Microsoft scripting languages to run their Web sites,then the clarity of XML returned from Web service methods should beclosely examined. If the third party wishing to access your Web service is running aMicrosoft platform and does not intend to use .NET (e.g., if he or she areusing classic ASP or Visual Basic 6), then you cannot force these people tomigrate to .NET in order to use your Web service; however, you couldmention the SOAP toolkit from Microsoft (msdn.microsoft.com/webservices/building/soaptk/), which can greatly simplify the task of adding Web servicesupport to a legacy Windows application. 17.6PerformanceThe ﬁrst thing that may strike you when running the code sample above isthat it can take several seconds to populate a short list of information. Webservices are slow on ﬁrst access because of background .NET compilations.It may look as if Web services were designed more for interoperability thanspeed. In chapter 4, remoting was discussed. This technology is similar to Webservices. With remoting, there were many ways to improve performance byusing more simplistic protocols. With Web services, there is no easy way touse anything other than SOAP. Having said this, the one-protocol-only wayof doing things makes life easier for system integrators who are working ondifferent platforms. The trade-off between interoperability and performancehas to be decided on a case-by-case basis. It should be clear enough thatSOAP is more interoperable than Microsoft’s proprietary binary format.In benchmarking tests, a Web service and remoting object both madequeries to a database in response to client requests. Under high-load condi-tions (60 requests per second for a single database entry), a remoting objecthosted on a Windows service using a binary formatter over TCP outper-formed the Web service by 50%. Although remoting objects can be conﬁgured for higher performancethan Web services, when a remoting object communicates with SOAP overHTTP, it is actually slower than a Windows service by about 25% underthe same load as stated above. Furthermore, it is more difﬁcult to use aremoting object than a Web service because there is no automatic mecha-nism to discover the interface of a remoting object, whereas Web services17.7Security495Chapter 17use WSDL. Some other conﬁgurations of the remoting object also suc-ceeded in outperforming the Web service. They were binary format overHTTP and SOAP format over TCP. When a remoting object is hosted on IIS rather than in a Windows ser-vice, the performance level drops substantially. When a remoting objectuses the binary format, it only barely surpasses Web services performance at20 requests per second; however, using other conﬁgurations, such as SOAPover HTTP on IIS, dropped the performance to 35% under Web services. To sum up, in order to achieve maximum performance, with a user basethat is exclusively .NET clients, and where you have access to a dedicatedWindows server, then use a Windows service hosting a remoting objectusing binary format over TCP. If the user base could include non-.NET cli-ents, however, or if you have only shared access to a server, then you shoulduse a Web service. 17.7SecurityWeb services run on IIS servers, so an IIS server with SSL certiﬁcatesinstalled provides secure Web services. This rather simplistic view of secu-rity in Web services is nonetheless probably the best approach to take whenimplementing a secure Web service at the moment.Web site security is more concerned with ensuring that the server isauthenticated to the client than vice versa, but this makes good sensebecause it means that customers will know they are giving their credit carddetails to a reputable supplier, but the supplier doesn’t really care who entersthe details, as long as money is involved in the transaction. With Web services, the typical user would have paid for the privilege ofusing the service. The user would not care exactly who is providing the ser-vice, just that the information is correct; however, the Web service providerwould need to know that the client was in fact a paying customer.HTTPS provides for client authentication, so there is no need to rein-vent the wheel here. In an intranet environment, a Windows authenticationsystem will undoubtedly already be in place on the network. To provide cre-dentials with a Web service call, it is a matter of setting the Credentialsproperty of the Web service, such as in the following code snippet:C#localhost.Service1 webservice = new localhost.Service1();CredentialCache cache = new CredentialCache();49617.7Security NetworkCredential netCred =        new NetworkCredential( "user", "pass", "myServerName" );cache.Add( new Uri(svc.Url), "Basic", netCred );webservice.Credentials = cache;VB.NETDim webservice As localhost.Service1 =  New _   localhost.Service1() Dim cache As CredentialCache =  New CredentialCache() NetworkCredential netCred = _   New NetworkCredential("user", "pass", "myServerName")cache.Add(New Uri(webservice.Url), "Basic", netCred)webservice.Credentials = cacheOn the Web service side, it is possible to check credentials using the fol-lowing statement:Thread.CurrentPrincipal.Identity.NameWhich will return either an empty string or a user name in the followingform:[Domain]\[user]Of course, this type of authentication is only useful for intranet situa-tions. It is not applicable for globally accessible services when SSL is notused on the server. The best practice is to use client X.509 certiﬁcates, butthis would be overkill for everything less than ﬁnancial applications becauseit takes a lot of time and effort to get issued an X.509 client certiﬁcate withyour name on it. An X.509 certiﬁcate can be included in the client requestby adding it to the ClientCertificates collection thus:C#localhost.Service1 webservice = new localhost.Service1();X509Certificate x509 = X509Certificate.CreateFromCertFile(    "c:\\myCertificate.cer");webservice.ClientCertificates.Add(x509);17.8Web services enhancements497Chapter 17VB.NETDim webservice As localhost.Service1 =  New _   localhost.Service1() X509Certificate x509 = X509Certificate.CreateFromCertFile( _   "c:\myCertificate.cer")webservice.ClientCertificates.Add(x509)If your Web service needs to be secure enough to prevent nonpayingusers from accessing it, but doesn’t require the overhead of end-to-end strongencryption, an acceptable middle road is to use hashing, or as it is more cor-rectly called, digest authentication. This is where each customer is allocated ausername and password. The password is combined with the username andthen hashed. The hash digest is then sent as a parameter to the Web method.If the digest matches the hash of the username and password pair held in thedatabase, then the user can be authenticated. To increase security, a seconddigest could be created, composed from the current time (accurate to theminute) and the user’s password. A hashed timestamp more than one minuteold would be rejected. This means that a hacker listening on the wire couldnot record and replay Web service requests.17.8Web services enhancementsWeb services can be made more ﬂexible by installing Web ServicesEnhancements (WSE) from Microsoft. To save confusion over terminology,Global XML Web Services Architecture (GXA) was a joint proposal byIBM and Microsoft. WSE is Microsoft’s adaptation of GXA, which is, forall intents and purposes, identical. The added features are attachments,security, routing, and referral.WSE can be downloaded from http://msdn.microsoft.com/webservices/building/wse. Once installed, it can be integrated into any .NET projectby adding a reference to Microsoft.Web.Services.dll and by modifyingthe Web.Config ﬁle for the project by adding a type to soapExtension-Types thus:<configuration> <system.web>  ...  <webServices>   <soapExtensionTypes>    <add type= "Microsoft.Web.Services.WebServicesExtension,49817.8Web services enhancements     Microsoft.Web.Services,      Version=1.0.0.0,     Culture=neutral,     PublicKeyToken=31bf3856ad364e35"      priority="1" group="0" />   </soapExtensionTypes>  </webServices> </system.web></configuration>17.8.1Web service extensions: AttachmentsIf your Web service returns multimedia data, such as images or audio, youshould consider using SOAP attachments. Including binary data as a SOAPattachment as distinct from plain text offers a performance advantagebecause the data will not be encoded and bloated in size. SOAP attach-ments use the direct Internet message encapsulation (DIME) format. Thisfeature is included in WSE 1.0. Only the core features of the technology aredescribed here.To attach an image (such as c:\photo.jpg) to a SOAP response, youcould use code similar to the following:C#string filePath = "C:\\myPhoto.jpg";DimeAttachment dimeImage = new DimeAttachment(   "image/jpeg", TypeFormatEnum.MediaType,   filePath);dimeImage.Id = "uri:" + Guid.NewGuid().ToString();SoapContext cntxt = HttpSoapContext.ResponseContext;cntxt.Attachments.Add(dimeImage);VB.NETDim filePath As String =  "C:\myPhoto.jpg" DimeAttachment dimeImage = New DimeAttachment( _    "image/jpeg", TypeFormatEnum.MediaType, _     filePath)dimeImage.Id = "uri:" & Guid.NewGuid().ToString()Dim cntxt As SoapContext =  HttpSoapContext.ResponseContext cntxt.Attachments.Add(dimeImage)You will require the following namespaces:17.8Web services enhancements499Chapter 17C#using System.Web.Services;using Microsoft.Web.Services;using Microsoft.Web.Services.Dime;VB.NETImports System.Web.ServicesImports Microsoft.Web.ServicesImports Microsoft.Web.Services.DimeThe Web service client could extract the image data from the SOAPresponse by using the following code:C#localhost.Service1 webservice = new localhost.Service1();Stream attachment = webservice.ResponseSoapContext.Attachments[0].Stream;Bitmap myImage = new Bitmap(attachment);VB.NETDim webservice As localhost.Service1 =  New _   localhost.Service1() Dim attachment As StreamAttachment = _   webservice.ResponseSoapContext.Attachments(0).Stream Dim myImage As Bitmap =  New Bitmap(attachment)There are several limitations to DIME in WSE 1.0. One signiﬁcant lim-itation is that SOAP attachments are not reﬂected in the WDSL contractthat is generated with the Web service. This means that clients will not beaware, until they make a request to your Web service, that there are anyattachments in the response. Furthermore, DIME is not portable amongdifferent platforms and is proprietary to Microsoft. To make matters worse,COM clients using the SOAP toolkit will not be able to access attachmentsat all unless the WDSL is manually edited to contain the appropriate<dime:message> child elements and <wsdl:output> elements, as describedin the WDSL speciﬁcation. Another limitation of DIME in WSE 1.0 is that security does notextend to the attachment. Therefore, whenever attachments need to be keptsecure from prying eyes and made resistant to man-in-the-middle tamper-50017.9.NET remotinging, you will have to implement your own hashing and encryption mecha-nism. Alternately, as previously recommended, the Web service should runover SSL to provide end-to-end encryption and avoid security loopholessuch as this one. 17.8.2Web service extensions: Routing When a Web service begins to scale upward, it may quickly outgrow a sin-gle-server environment and require hosting on several servers in parallel.Because Web services run over IIS, they can be scaled upward in much thesame way as any Web site. This includes using load-balancing systems suchas Cisco Local Director or Microsoft NLB. Load-balancing systems do generally delegate workload equally amongservers, and sometimes you may require more logic behind the load balanc-ing. When talking speciﬁcally about Web services, you can use WSE to cre-ate an intermediary Web service. This Web service could be used to directWeb service calls to other servers, which may contain more up-to-date dataor be otherwise more appropriate for that particular call. 17.8.3A word on Project Hailstorm (MyServices)Project Hailstorm, or MyServices, is a technology that was shelved byMicrosoft in early 2002; therefore, it is best avoided. MyServices was aproject put forward by Microsoft to permit people to store data they woulduse on a day-to-day basis on their servers via an array of custom-built Webservices. Services such as .NET Contacts to store your personal addressbook, .NET Inbox to store your email, and .NET Wallet to store yourcredit card details would be available through MyServices. The idea is tech-nically sound, but many people and companies balked at the idea ofMicrosoft being in control of so much personal information. 17.9.NET remotingRemoting is .NET’s equivalent of  Java remote method invocation (RMI)and Visual Basic’s Distributed Common Object Model (DCOM). It facili-tates the use of complex objects on remote computers, using the same syn-tax as if they were in the same application. The advantage that remotingaffords is the abstraction of the network infrastructure. This greatly simpli-ﬁes the implementation of client/server applications in which the servermust perform a variety of tasks based on instructions from the client.17.9.NET remoting501Chapter 17Imagine a scenario in which a distributed billing system is being devel-oped, where the client’s systems are high-street hire-purchase outlets, and acentral server at the head ofﬁce handles customer invoicing, debt collection,and so forth. Clients would require the server to perform tasks such as per-form credit check, record start of lease, terminate lease, process credit cardpayment, and other such tasks. Of course, the same effect could be achievedby sending strings over TCP/IP, which the server would parse it on theremote side, but it is simpler to make a call to customer.terminateLease()and let .NET handle the network transmission.17.9.1How remoting worksWhen using remoting, you still need to create a client and server. You alsoneed to create an object that performs whatever functions you require. Bothends of the connection need to know the type of the object. The clientneeds to know the IP address and port of the server. Other than that, .NETdoes the rest.Although you don’t see what is being passed over the network, you dohave a choice whether to go for SOAP over HTTP (portable) or binary overTCP (performance). SOAP used for remoting differs from the industry for-mat somewhat and would be less portable than an equivalent Web service.Note: Channel sinks can be used to view or modify the data immediatelybefore it is sent across the wire. This can be used to add security or queuingfeatures. To prevent clients from draining the server’s resources by creating mil-lions of objects and abandoning their instances, remoting has a built-in gar-bage-collection system. Objects can be created so that their lifetime lastsonly as long as the execution time of the function (singlecall) or as longas the class (singleton) or a server-deﬁned lifetime (published objects).Remote object lifetimes, with the exception of published objects, are speci-ﬁed in the call to RemotingConfiguration.RegisterWellKnownService-Type, as we shall see later.Published objects are instantiated slightly differently, where, instead ofthe call to RegisterWellKnownServiceType, the object is created thus:C#RemoteObject obj = new RemoteObject(1234);RemotingServices.Marshal (obj,"RemotingServer");50217.9.NET remotingVB.NETDim obj as RemoteObjectobj = new RemoteObject(1234);RemotingServices.Marshal(obj,"RemotingServer")After which the object behaves as a singleton. The beneﬁt of creating anobject in this way is that it is possible to create objects with nondefault con-structors. This could include constructors that require user interventionand, thus, are unsuitable for arbitrary client activation. The key to remoting is to create a class that derives from MarshalBy-RefObject. This object is then capable of running within the context of aserver and exposes its methods and properties through that server. Whilerunning in the context of the server, local resources such as ﬁles and data-bases located on the server are accessible through the class. Objects thatare returned as a result of calling methods on this class, however, are runin the context of the client. These objects are called By Value objects.By Value objects cannot access server resources, such as databases orﬁles; however, they can be prepopulated with data taken from serverresources such as these. For instance, the ubiquitous DataSet is perfectlyacceptable as a By Value object. A remote object returns a By Value objectby serializing it and transferring it over the network to the client. Thismechanism will only work if two conditions are met: (1) the object must bemarked [Serializable] or implement ISerializable, and (2) the clientmust hold at the metadata for the By Value object, such that it can correctlydeserialize it. 17.9.2Implementing remotingThis example demonstrates a simple remoting application, where the clientapplication may request a number from a server, which is incremented onevery call.Start a new Class library project in Visual Studio .NET, and enter thefollowing code:C# using System;namespace RemoteObject{  public class IDGenerator : System.MarshalByRefObject  {17.9.NET remoting503Chapter 17    private int lastID =0;    public int getID()    {      return(lastID++);    }  }}VB.NETImports SystemNamespace RemoteObject  Public Class IDGenerator   Inherits System.MarshalByRefObject    Private lastID As Integer = 0     Public Function getID() As Integer      lastID = lastID + 1                   return(lastID)    End Function  End ClassEnd NamespaceYou will note that the class derives from System.MarshalByRefObject.This enables the object to be transferred over a remoting channel. Compile the object, and note the location of the resultant DLL. Thenext step is to create the server application to host this object.Create a new Windows form project in Visual Studio .NET. ClickProject→→→→Add References→→→→Browse, and then click on the DLL created in thelast compilation. You will also need to select the System.Runtime.Remotingnamespace.C#private void Form1_Load(object sender, System.EventArgs e){  HttpChannel channel = new HttpChannel(8085);  ChannelServices.RegisterChannel(channel);  RemotingConfiguration.RegisterWellKnownServiceType(        typeof(RemoteObject.IDGenerator),        "RemotingServer",        WellKnownObjectMode.Singleton);}50417.9.NET remotingVB.NETPrivate  Sub Form1_Load(ByVal sender As Object, _ByVal e As System.EventArgs)  Dim channel As HttpChannel =  New HttpChannel(8085)   ChannelServices.RegisterChannel(channel)  RemotingConfiguration.RegisterWellKnownServiceType( _  (New RemoteObject.RemoteObject.IDGenerator).GetType(), _           "RemotingServer", _           WellKnownObjectMode.Singleton)End SubCertain things can be immediately ascertained by looking at this code.The communications will take place on port 8085, using SOAP overHTTP. The object is to be created as a Singleton, which means that it isstate-full, and the value of LastID will be maintained between calls.You will also require the supporting namespaces:C#using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Http;using RemoteObject;VB.NETImports System.Runtime.RemotingImports System.Runtime.Remoting.ChannelsImports System.Runtime.Remoting.Channels.HttpImports RemoteObjectCreate a new Windows forms project in Visual Studio .NET. ClickProject→→→→Add References, click Browse, and then click on the DLL createdin the last compilation. Draw a button on the form and name it btnGetID.Now click on the btnGetID button and enter the following code:C#private void btnGetID_Click(object sender, System.EventArgs e){17.9.NET remoting505Chapter 17 RemoteObject.IDGenerator remObject =  (RemoteObject.IDGenerator)Activator.GetObject(        typeof(RemoteObject.IDGenerator),        "http://localhost:8085/RemotingServer"); if (remObject==null)  MessageBox.Show("cannot locate server"); else  MessageBox.Show(Convert.ToString(remObject.getID()));}VB.NETPrivate  Sub btnGetID_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)  Dim remObject As RemoteObject.IDGenerator = _     CType(Activator.GetObject( _     (New RemoteObject.IDGenerator).GetType(), _     "http://localhost:8085/RemotingServer"), _            RemoteObject.IDGenerator)  If remObject Is Nothing Then    MessageBox.Show("cannot locate server")  Else    MessageBox.Show(Convert.ToString(remObject.getID()))  End IfEnd SubIn this code, the call to the remote object is discretely written as remOb-ject.getID(). It is worthwhile to note that this is a synchronous call, andif the client could be doing other things while waiting for the method toreturn, then either an asynchronous or one-way call should be employed, asexplained later.Again, you will also require the supporting namespaces:C#using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Http;using RemoteObject;VB.NETImports System.Runtime.RemotingImports System.Runtime.Remoting.Channels50617.9.NET remotingImports System.Runtime.Remoting.Channels.HttpImports RemoteObjectTo test the application, execute the client and server together (Figure17.3), and then press the button on the client a few times. You will see thenumber in the message box increasing.17.9.3Asynchronous use of remote objectsAsynchronous use of remote objects can be achieved by using delegates,which are the .NET equivalent of function pointers. They are declaredwithin the same class as the client, but outside any of its methods. It wouldhave the same function prototype as the synchronous method you wish tocall. For instance, a remote method named getDetails() returning stringwould have a corresponding delegate such as the following:C#delegate String GetDetailsDelegate();VB.NETDelegate Function GetDetailsDelegate() as stringFigure 17.3Remoting Clientand remotingServer.17.9.NET remoting507Chapter 17Assuming the remote object is already instantiated and named obj, thegetDetails() method can be called thus:C#GetDetailsDelegate gdDelegate = new GetDetailsDelegate(obj.GetDetails);IASyncResult gdAsyncres = gnDelegate.BeginInvoke(null,null);VB.NETDim gdDelegate as GetDetailsDelegateDim gdAsyncres as IASyncResultgdDelegate = new GetDetailsDelegate(AddressOf obj.GetDetails)gdAsyncres = gnDelegate.BeginInvoke(nothing,nothing)This code returns immediately, and the server will begin executing theGetDetails method on the remote object. In order to retrieve the returnvalue from the call, the client must execute EndInvoke on the delegate. Thismethod is blocking and will only return once the server has responded. It iscalled as follows:C#String details = gdDelegate.EndInvoke(gnAsyncres);VB.NETDim details as stringDetails = gdDelegate.EndInvoke(gnAsyncres)Although this method should be sufﬁcient for most purposes, there isanother way to invoke a remote object asynchronously, by using the OneWayattribute. One-way calls are made in the same way as standard asynchro-nous calls from the client; however, the EndInvoke method will be non-blocking and is guaranteed to return immediately, whether the server hasresponded or not. This is useful for noncritical “call-and-forget” methods,where overall application speed is more important than guaranteeing execu-tion of selected peripheral functions. To implement a one-way function, simply mark a method within theinterface deﬁnition with the attribute [OneWay()].50817.9.NET remoting17.9.4Deployment of a remoting serviceWhen using remoting in a commercial application, a few tricks of the tradehelp your software become more robust and manageable. The client mustbe able to tell the type of the object it is to receive at compile time. Thismeans that if you have already deployed your client to a million usersworldwide, you can’t make changes to the object or all of the clients willstop working. The way around this dilemma is to have the client refer to theinterface of the object rather than the object itself, which means you canchange the implementation of the object’s methods without breaking com-patibility. Perhaps a more important aspect is that if you are sharing theimplementation of your software with third parties, they could possiblydecompile or otherwise reverse-engineer your DLL, using ILDASM orMSIL-to-C# (www.saurik.com). An interface to the RemoteObject.IDGenerator class above is as follows:C# using System;public Interface IIDGenerator{  public int getID();}VB.NETImports SystemPublic Interface IIDGenerator  Public Function NextOrder() As intEnd InterfaceUsing shared interfaces is not the only way to provide clients with accessto remote objects. The two main drawbacks are that third parties workingwith your remote object must be sent the new interface whenever any of thepublic methods or properties in the object change; furthermore, you cannotpass these objects as parameters to functions running in a different context.An alternate method is to use shared base classes. This is where the clientis provided with an abstract base class. The server would inherit from thisbase class and implement the required functionality. This would make theseclasses capable of being passed to functions running in different contexts;however, it is impossible to create new objects using the new operator, onlythe Activator.GetObject() can be used in this instance. 17.9.NET remoting509Chapter 17In order to address the deployment issue, Microsoft has created a utilitynamed soapsuds. This command-line utility can be used to extract metadatafrom remote objects. It is invoked from DOS thus:soapsuds –url:<URL>?wsdl -oa:<OUTPUT>.DLL –nowpThis generates a DLL ﬁle, which client programs can use to communi-cate with the remote object. This DLL does not contain any implementa-tion details, only interfaces. The –nowp, or no-wrap, parameter is used toindicate whether the URL of the remote object should be hard-coded intothe DLL. An unwrapped proxy DLL does not contain URL information,but a wrapped proxy DLL does. The beneﬁt of hard-coding the URL intothe DLL is that the remote object can be created using the new operator.Otherwise, the client has to use Activator.GetObject() to instantiatethe object.17.9.5ConﬁgurationOne major issue regarding deployment of remoting services is the ability toconﬁgure clients quickly and easily. For instance, if you are forced to changethe IP address of the server hosting the remote object, it could be tricky tochange the code to point to the new IP address, recompile the application,and request that all customers upgrade their software. A more acceptablesolution is to hold the location of the remote object in a separate XML ﬁle,which could be replaced with a hot-ﬁx patch when required. Therefore,.NET provides a means of providing conﬁguration ﬁles for remoting cli-ents. A conﬁguration ﬁle takes the following form:XML<configuration> <system.runtime.remoting>  <application>   <channels>    <channel ref="http" port="1234" />   </channels>   <service>    <wellknown mode="Singleton" type="myNamespace.myClass,      myAssembly" objectUri="myClass.soap">   </service>  </application> </system.runtime.remoting></configuration>51017.9.NET remotingAssuming this ﬁle is saved as MyApp.exe.config, you can instantiate theremote object from the client using the following code:C#String filename = "MyApp.exe.config";RemotingConfiguration.Configure(filename);MyClass obj = new MyClass();VB.NETDim filename as stringFilename = "MyApp.exe.config"RemotingConfiguration.Configure(filename)Dim obj as MyClassObj=new MyClass()Of course, the client still requires the deﬁnition of the class MyClass inorder to create an instance of the class. You could provide the implemen-tation of MyClass to the client, but this poses code security risks anddeployment problems. Neither the shared interface nor the shared baseclass method is suitable for the above example for providing class deﬁni-tions, so in this case you should use soapsuds to generate a DLL for theclient to reference in order to create these remote objects. The –nowpswitch should be used with soapsuds to ensure that the DLL does nothave hard-coded parameters. In most cases, this should be all that is required to deploy a remotingservice with conﬁguration ﬁles; however, some developers may run into aproblem where a remote object returns a By Value object, containing itsown methods. In this case, the client must have a local reference to the ByValue object, so it can deserialize the object and execute its methods. But aproblem occurs because the namespace generated by soapsuds will be thesame as the By Value object’s namespace. To avoid this namespace nameclash, you should manually edit the soapsuds-generated proxy DLL fromits source code, which can be obtained by calling soapsuds with the –gcswitch. Once the C# code can be edited, the namespace can be changed tosomething else, thereby avoiding the namespace clash. 17.9.6Hosting remote objects within IISRemote objects, as described thus far, have been hosted in simple Windowsapplications. In reality, remote object servers generally do not require a user17.9.NET remoting511Chapter 17interface, but they often require the ability to execute on a computerregardless of whether a user is actively logged in. For this reason, you willprobably want to run your remote object server as a service. Chapter 10covers this topic in more detail.Another alternative, which may be more applicable for shared hostingenvironments, is to host the remote object within IIS. This can be achievedby adding a little XML to the web.config ﬁle thus:XML<configuration>  <system.runtime.remoting>    <application>      <service>        <wellknown          mode = "Singleton"          type = "RemoteObject.IDGenerator,RemotingServer"          objectUri = "RemoteObject.soap"        />      </service>    </application>  </system.runtime.remoting></configuration>17.9.7Hosting remote objects within a Windows serviceWhen an application is designed to run unattended on a computer, and hasno need for a user interface, it should run as a Windows service. Windowsservices run in the background even when no user is currently logged on.They are controlled via Control Panel→→→→Administrative Tools→→→→Services,where you can start, stop, and restart the service, as well as view informa-tion about it.It is possible to use IIS as a host for remoting objects, but if you aredeveloping a mass-market software product, not all users have IIS on theircomputers, nor will they want to go to the hassle of installing it.This example requires the client and object from the previous example,so if you have not done so, now is a good time to type it in. Start a newWindows service (not application) project in Visual Studio .NET, scrolldown the code to the OnStart and OnStop methods, and add the followingcode:51217.9.NET remotingC#Thread thdServer;protected override void OnStart(string[] args){  thdServer = new Thread(new ThreadStart(serverThread));  thdServer.Start();}VB.NETDim thdServer As ThreadProtected Overrides  Sub OnStart(ByVal args() As String)        thdServer = New Thread(New ThreadStart( _                    AddressOf serverThread))        thdServer.Start()End SubThe two events OnStart and OnStop are triggered whenever the serviceis started or stopped from Administrative Tools→→→→Services. The above codewill simply start a new thread at the serverThread function. Note that thethread variable is outside of the method call, which provides a means forOnStop to disable the service by stopping the thread. C#protected override void OnStop(){  thdServer.Abort();}VB.NETProtected Overrides  Sub OnStop()  thdServer.Abort()End SubServerThread is taken verbatim from the chapter 4 example. It opens anHTTP channel on port 8085 for the RemoteObject assembly. C#public void serverThread(){  HttpChannel channel = new HttpChannel(8085);17.9.NET remoting513Chapter 17  ChannelServices.RegisterChannel(channel);  RemotingConfiguration.RegisterWellKnownServiceType(      typeof(RemoteObject.IDGenerator),      "RemotingServer",      WellKnownObjectMode.Singleton);}VB.NETPublic  Sub serverThread()  Dim channel As HttpChannel = New HttpChannel(8085)  ChannelServices.RegisterChannel(channel)  RemotingConfiguration.RegisterWellKnownServiceType( _    (New RemoteObject.RemoteObject.IDGenerator).GetType(), _      "RemotingServer", _      WellKnownObjectMode.Singleton)End SubAs before, the code establishes a connection channel using HTTP overport 8085. The object is hosted in singleton mode, meaning that only oneinstance of the object is ever created. This mode is required for this applica-tion because the object needs to maintain a private variable, which is sharedbetween all clients that call the remote object.Services cannot be run directly from the command line; they must beinstalled. To prepare a service for deployment, right-click on the service indesign view and select Add Installer. Click on ServiceInstaller1, and setthe ServiceName property to MyService. Set the Account property ofServiceProcessInstaller1 to LocalSystem.Finally, you need to add three references: one to System.Configura-tion.Install.dll, one to System.Runtime.Remoting, and another thatpoints at the compiled DLL for the IDGenerator assembly. Then add therequired namespaces as shown: C#using System.Configuration.Install; using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Http;using System.Threading;51417.9.NET remotingVB.NETImports System.Configuration.Install Imports System.Runtime.RemotingImports System.Runtime.Remoting.ChannelsImports System.Runtime.Remoting.Channels.HttpImports System.ThreadingCompile the application in Visual Studio .NET. You will not be able torun it directly. You need to install the service ﬁrst. To do so, you now mustgo to the DOS command prompt. Navigate to the path where the compiled.exe ﬁle resides, and then type the following at the command prompt:DOSpath %path%;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705installutil service.exenet start MyServiceNote: The path C:\windows\Microsoft.NET\Framework\v1.0.3705 maydiffer among computers. If you have two versions of the .NET frameworkon your machine, use the latest version.Figure 17.4Remoting Serverrunning as aWindows service.17.9.NET remoting515Chapter 17You may be prompted to enter a valid Windows username and passwordduring the installation process. To test the application, open the Servicessnap-in at Control Panel→→→→Administrative Tools→→→→Services. If you scrolldown the list, you should see an entry named “Service1” (Figure 17.4),which should be already started. Now open the client program that was cre-ated for the example in chapter 4. Press Get unique ID, and you should seea message box appear with a number on it. 17.9.8Distributed garbage collectionGarbage collection occurs when an object is loaded into memory and noprogram holds a reference to it. In a distributed environment, it is muchmore difﬁcult to monitor which programs hold references to a remoteobject, especially if the programs in question are hundreds of miles awayover unreliable dial-up connections.There are two ways to solve this problem: client activation and serveractivation. Server activation is where the client has no control over the life-time of the object, and client activation is where the client has full controlover the lifetime of the object.Table 17.1Signiﬁcant members of the ILease interface.Method or PropertyPurposeInitialLeaseTimeSpeciﬁes the amount of time a remote object will stay in memory before it is garbage-collected, assuming no action was taken on the object. The default is ﬁve minutes.CurrentLeaseTimeRepresents the amount of time left before the remote object is garbage-collected. RenewOnCallTimeSpeciﬁes the amount of extended lease time that should be added if the object is called. The default is two minutes.SponsorshipTimeoutIndicates the amount of time the lease manager will wait for a sponsor to respond before moving to another sponsor or garbage-collecting the object. The default is two minutes.LeaseManagerPollTimeSpeciﬁes the interval of time between scans for expired leases by the underlying lease manager pro-cess. The default is ten seconds.51617.9.NET remotingWhen the object is created using the Activator.GetObject() com-mand, then the object is server activated. Server activation comes in twoforms: singleton and single call. Singleton activation is where one, and onlyone, instance of the object is created on the server. This implies that stateinformation for the object is shared between all clients. Single-call activationis a second form of server activation in which the object is created wheneverany method is called on it. The object will be destroyed again once themethod call is complete. This implies that there is no state information heldin the object. Client-side activation manages objects by lifetime leases, which meansthat a client can instruct the server to create an object and to keep it inmemory for a speciﬁed time before destroying it. Client-side activationoccurs when the object is created using Activator.CreateInstance().In order to modify the lease parameters of a remote object, you simplyoverride the InitializeLifetimeService method and change the proper-ties of the ILease interface (Table 17.1).C# using System;using System.Runtime.Remoting.Lifetime;namespace RemoteObject{  public class IDGenerator : System.MarshalByRefObject  {    private int lastID =0;    public override Object InitializeLifetimeService()       {        ILease lease =         (ILease)base.InitializeLifetimeService();        if (lease.CurrentState == LeaseState.Initial)          {          lease.InitialLeaseTime = TimeSpan.FromMinutes(5);          lease.SponsorshipTimeout = TimeSpan.FromMinutes(6);          lease.RenewOnCallTime =           TimeSpan.FromSeconds(7);        }        return lease;      }          public int getID()      {17.9.NET remoting517Chapter 17     return(lastID++);    }  }}VB.NETImports SystemImports System.Runtime.Remoting.LifetimeNamespace RemoteObject  Public Class IDGenerator   Inherits System.MarshalByRefObject    Private lastID As Integer = 0     Public Overrides Function  _    InitializeLifetimeService() As [Object]       Dim lease As Ilease       Lease =  _       CType(MyBase.InitializeLifetimeService(), ILease)            If lease.CurrentState = LeaseState.Initial Then             lease.InitialLeaseTime = TimeSpan.FromMinutes(5)             lease.SponsorshipTimeout = _                     TimeSpan.FromMinutes(6)             lease.RenewOnCallTime = TimeSpan.FromSeconds(7)        End If        Return lease        End Function           Public Function getID() As Integer     lastID = lastID + 1     return(lastID)     End Function  End ClassEnd NamespaceLooking closely at the code above, you can see that this is the sameobject as described earlier in the chapter. The difference is the overriddenfunction, which provides access to the lease parameters of the object. Toobtain an interface to the lease parameters, a call is made to base.Initial-izeLifetimeService(), which returns the lease interface. Some lease parameters cannot be changed once the object has beeninstantiated, thus the lease’s current state is checked with the lease.Cur-51817.10ConclusionrentState property to ensure that the object is just in the process of beingcreated. The InitialLeaseTime, SponsorshipTimeout, and RenewOnCallTimeproperties are set to ﬁve, six, and seven minutes, respectively. This will keepthe object in memory longer than the default object lifetime. To test this code, compile the object, and run the client program asdescribed earlier. Calls to the object will work ﬁne for the ﬁrst ﬁve minutes,but any call made more than seven minutes after the previous call will causeincorrect operation because the object will have been garbage collected. 17.10ConclusionWeb services have been very much hyped as the next big thing in informa-tion technology. They are arguably one of the simplest remote procedurecall systems ever developed and possibly the most interoperable technologyever developed by Microsoft. Having said that, remoting can outperformWeb services under most conditions, and the technology is still in itsinfancy. Many features, especially within WSE 1.0, are underimplementedand could easily cause headaches for some developers. As this concludes this book on .NET networking, I hope it proves bene-ﬁcial to you and helps you to further your career as a professional developer.Good luck, and may your programs be bug free and efﬁcient! 519 Index 6over4 protocol, 4736to4cfg, 4636to4 protocol, 469–71deﬁned, 469IPv6 address, 470network trafﬁc, 470pseudointerface, 471Acknowledgments, 437–39C#, 437–38deﬁned, 437messages, 438, 439VB.NET, 438 See also  Message queues; Microsoft Message Queue (MSMQ) ActiveUdpListener  class, 367, 368Adaptive delta pulse code modulation (ADPCM), 290, 297Address bookaccessing, 156–57application, 157C#, 156–57VB.NET, 157 See also  Mail application programming interface (MAPI)Address resolution protocol (ARP), 6, 327Apache, 87Applicationsasymmetric encryption, 219digital certiﬁcate reader, 243DNS MX client, 314frame-layer packet sniffer ( PacketX ), 364frame-layer packet sniffer ( rvPacket ), 360hashing, 236“hello world,” 56–62HTML editor, 113HTTP client, 93, 96HTTP server, 121ICMP client, 321IP-layer packet sniffer, 344JPEG compression, 301load balancing, 272MAPI address book, 157MAPI client, 155MSMQ, 424MSMQ receiver (basic), 428MSMQ receiver (complex), 434MSMQ transfer, 432namespaces, 18scalable network, 254–55STA, 18symmetric encryption, 224TAPI call-receiver, 399TAPI call-receiver with DTMF and playback, 417TCP client, 62TCP Server, 66testing, 20, 30, 32, 39, 54third-party distributed, 74 520Index thread pool, 261time-critical, 25UDP client, 58UDP server, 61unresponsive, 81wave sound player, 413Web service client, 489WHOIS client, 326WMI query language analyzer, 333WMI remote process manager, 335ZIP compression, 295AspEmail, 132ASP.NET, 98–104, 130, 483deﬁned, 98HTML element modeling, 98mobile Web browsers, 130performance, 99Asymmetric encryption, 212–18application, 219C#, 214, 215, 216, 217–18deﬁned, 212–13RSA as, 213–18VB.NET, 214–15, 216, 217–18 See also  Encryption AsyncCallback  object, 84AT commands, 380Attachments, 151–52C#, 151, 152SOAP, 498–500VB.NET, 151, 152Audio compression, 296–98Audio playback, 401–17C#, 402–5, 409–10, 411, 412in CTI system, 401over TAPI, 413–17principle, 401VB.NET, 405–8, 410, 411–12, 413Audio-video interleaved (AVI) format, 302Authentication, 227–49anonymous, 228basic, 228client, 495digest, 229, 230, 497IIS, 228–30introduction, 227IP address validation, 228Kerberos, 229low-level, 448.NET Passport, 230–32NTLM, 229techniques, 227 See also  SecurityBackup site controllers (BSCs), 450Bandwidth optimization, 275–303Bankers Automated Clearing Service (BACS), 249Base64, 237 BinaryFormatter  object, 41–42Binary streams, 29–33 BinaryWriter  class, 33Border gateway protocol (BGP), 328C#, 9acknowledgments, 437–38address book access, 156–57asymmetric encryption, 214, 215, 216, 217–18attachments, 151, 152audio playback, 402–5, 409–10, 411, 412audio playback over TAPI, 413–15, 416binary/text streams, 29, 30, 31–32callbacks, 491–92code debugging, 73compiling in, 20complex object queuing, 428, 429–30, 431, 432–33database programming, 45, 47, 48dataset serialization, 51–52 Index521Index DNS MX implementation, 306–8, 309–10, 311–12, 313DTMF tones, 399–400, 401FTP implementation, 179, 180–81, 182, 183–84, 185, 187, 188, 189–90, 191, 192FTP with ITC, 175, 177ICMP, 315, 317, 318, 319, 320incoming phone call listening, 383–86, 391, 393, 394, 395–96, 397, 398, 399IP-level network tapping, 340–42, 343–44IPv6, 473–75, 476, 477journaling, 441–42JPEG compression, 299, 300leveraging WMI, 334, 335load balancing, 264–67, 268, 269, 270, 271MAPI, 153–54message queue implementation, 421, 423, 424, 425, 427multicast implementation, 284, 285, 286, 287–88.NET SDK, 17 PacketX  control, 361–63permissions, 246physical network tapping, 367POP3, 144, 145, 147posting data, 99, 100, 102proxy tunneling, 204queued components, 443–44, 445, 446, 447reading certiﬁcates, 242–43remote object hosting, 512–13remoting conﬁguration, 510remoting implementation, 502–3, 504–5remoting service deployment, 508–9serialization, 34–35, 37–39serializing to binary, 41–42SHA-1, 235shallow serialization, 43, 44SMTP implementation, 134, 135–37, 138–39SOAP attachments, 498, 499socket-level networking, 76–77, 81, 82, 83, 84–85streams for ﬁles, 22–23, 24, 25–26symmetric encryption, 219–21, 222, 223 System.Web.Mail , 149, 150TCP/IP client, 63, 64TCP/IP server, 66, 67, 69, 70–71telephony, 381thread pool implementation, 258, 259, 260, 261thread pooling, 257timeouts, 439–40transactions, 435–36UDP client, 57, 58UDP server, 59–61Visual Studio .NET, 14wait handles, 490Web server implementation, 114, 115, 118–19, 120, 122, 123, 124Web service deployment, 485Web services, 482, 483Web service security, 495–96Web service use, 486–87WHOIS, 324, 325 WinPCap  with  rvPacket , 355–56, 357, 358, 359WMI, 330–31, 332WYSIWYG editor, 108, 109, 110, 112ZIP compression, 292, 293, 295Caching, 276–77cache types, 276change monitoring, 277criteria, 276performance and, 276proxy, 276, 277server, 276 See also  Performance increasesCallback function, 84 522Index Callbacks, 491–93C#, 491–92deﬁned, 491VB.NET, 492–93 See also  Web servicesCascading style sheets (CSSs), 9CDOSYS, 148–49deﬁned, 148–49functionality, 149Certiﬁcate revocation list (CRL), 241Certiﬁcates, 236–38client, 239–44ﬁelds, 237issue, 237myths, 237reader application, 243reading, 241–44server, 238–39subject markers, 238X.509, 241–42, 449Channel sinks, 501Check4 utility, 463–64Chrominance, 302Cipher text, 212Classes ActiveUdpListener , 367, 368 BinaryWriter , 33 clsCryptography , 215, 217 Crypography , 214 FileStream , 22, 26, 28, 192 Form , 82 HttpWebListener , 124–25 HttpWebRequest , 103–4 HttpWebResponse , 95, 97, 101 IcmpV4Statistics , 368–70 InterfaceStatistics , 373–74 IPAddressInformation , 374 IPStatistics , 370–72 IPv4Properties , 374–75 ListenerWebRequest , 125–26 ListenerWebResponse , 126–27 Message , 426–27 MessageQueue , 422 NetworkInformation , 366, 367–68 NetworkInterface , 372–73 NetworkStream , 22, 183, 188 PacketXCtrl , 361 SoapFormatter , 41–42 Socket , 76 StreamReader , 31, 221 TcpClient , 65 TcpListener , 70, 72 TcpStatistics , 375–76 UdpClient , 59, 60 UdpStatistics , 376 WebProxy , 268 WebRequest , 95 XMLSerializer , 44Client activation, 515Client caches, 276Client certiﬁcates, 239–44availability, 239basic, 239CRL, 241MSCS, 240–41reading, 241–44viewing, 240 See also  CertiﬁcatesClientsauthentication, 495COM+, 447DNS MX, 314FTP, 166, 173, 177, 193HTTP, 93, 96, 103IMAP, 158MAPI, 155MSMQ, 450multicast UDP, 289POP3, 141in port conﬂict, 205remoting, 506SMTP, 151 Index523Index TCP/IP, 62–65telnet, 326UDP, 57–58WAP, 128Web service, 488, 489WHOIS, 326 ClsCryptography  class, 215, 217Codebooksdeﬁned, 290dynamic, 291static, 290Color-picker dialog box, 111COM+component services, 443test client, 447Common Information Model (CIM), 329Common Internet File (CIF) system, 163Common language runtime (CLR), 9Common Object Model (COM), 16Common Type System (CTS), 9Compact HTML (cHTML), 129Compilingwith C#, 20with VB.NET, 19COM ports, 380, 382Compression, 289–303audio, 296–98deﬁned, 289Huffman, 290image, 298–302JPEG, 298–302lossless, 289, 290–96lossy, 290, 296–303MPEG, 302–3types of, 289–90video, 302–3ZIP, 290, 291–96Computer Name Changes dialog, 197Computer Telephony Integration (CTI), 377audio playback, 401cost, 379Constructors, 18ControlsColor Dialog, 107COM, 176, 177Date-Picker, 429File Open Dialog, 291, 299File Save Dialog, 291, 299Open File Dialog, 151, 219, 242 PacketX , 360–66Save File Dialog, 179Web Browser, 108Cookies, 104–5copying, across domains, 105deﬁned, 105domain speciﬁc, 105retrieval, 105serializing, 105 Crypography  class, 214Cryptanalysis, 209–12Cryptographic algorithms (ciphers), 212CuteFTP, 73 CWD  command, 189Cyclic redundancy check (CRC), 234Datacompression, 289–90encoding, 28posting, 97–104SMB, 366WMI, 330Databasesconnection strings, 45creating, 49–51direct network access, 44opening connections to, 45programming overview, 45–48writing, to streams, 44–54Data miningdeﬁned, 88permission, 88 524Index unauthorized, preventing, 233Dataset serialization, 51–54C#, 51–52shallow, 53VB.NET, 51, 52–54 See also  SerializationDeadlocksavoiding, 261–62deﬁned, 261Debugging, 73–75 Deflate  compression algorithm, 296Delete statements, 46Delta pulse code modulation (DPCM), 297Denial-of-service attacks, 338 DESCryptoServiceProvider  object, 219Deserializationdeﬁned, 33example, 38–39 See also  SerializationDigest authentication, 229, 230deﬁned, 229, 497speed, 230 See also  AuthenticationDirect Internet message encapsulation (DIME) format, 498, 499Direct memory access (DMA), 411Discrete Cosine Transform (DCT), 298Distance vector multicast routing protocol (DVMRP), 283Distinguished Encoding Rules (DER), 237Distributed Common Object Model (DCOM), 74, 500Distributed garbage collection, 515–18DNS, 305–14format information, 309Google system, 252handling, 306header, 352–53mail exchange query, 132MX, implementing, 306–14packet detail, 352–53port 53 operation, 305queries, 309round-robin, 262in sending/receiving emails, 306DNS servers, 262IP address, 313MX, 311, 313queries, 305Web site listing on, 305Dual-tone modulated frequency (DTMF) tones, 399–401C#, 399–400, 401deﬁned, 399with TAPI, 399VB.NET, 400, 401 See also  TelephonyDynamic host control protocol (DHCP), 5Dynamic HTML, 7Dynamic IP addresses, 206Dynamic NATs, 202Emailsaddress form, 131attachments, 151–52deleting, 142, 158delivery, 132images, 153message header, 143–44sending, 131–32SPAM, 144–48trafﬁc, 339 See also  Post ofﬁce protocol 3 (POP3)Encapsulation, 6Encodingdata, 28entropy, 290run length (RLE), 290source, 290 Encoding  object, 102Encryption, 209–25 Index525Index algorithms, 210asymmetric, 212–18cryptanalysis, 209–12digest, 229end-to-end, 236introduction, 209NTLM, 163SSL, 236symmetric, 218–24terminology, 212Enterprise networks, 199–205building, 199–203ﬁrewalls, 200–203routers, 199–200tunneling out of, 203–5 See also  NetworksEntropy encoding, 290Ethernetframe header, 345type codes, 346Extensible markup language.  See  XMLExterior gateway protocols (EGPs), 203, 328Filesentropy, 27reading, 27streams for, 22–28transfer with TCP/IP, 62–73ZIP, 292, 294, 295, 296File sharing, 163–65Microsoft, 163–64Netware, 164–65 FileStream  class, 22, 26constructor, 192members of, 28File transfer protocol.  See  FTPFinancial network security, 246–49corporate transactions, 248–49ISO 8730, 247–48SWIFT, 248X.25, 247Firewalls, 200–203analogy, 200deﬁned, 200NATs, 201–3proxies, 201tunneling, 205, 206–7Font-chooser dialog box, 110 Form  class, 82FormsLogon, 179, 180Main, 179, 180, 182–83namespaces, 192Framework class library (FCL), 9Frequency analysisdeﬁned, 210software, 211FTP, 163–94authentication mechanism, 168background, 163command reference, 171–72command usage, 178 CWD  command, 189deﬁned, 163folder navigation, 170–71handshake, 168–70implementing, 172–74, 178–93implementing, with ITC, 174–78 LIST  command, 170 NOOP  command, 169over SSL, 168overview, 165–94 PASS  command, 169 PASV  command, 188port operation, 165port usages, 167–68protocol, 166 QUIT  command, 170root, 177, 189script ﬁle, 165site administration, 174 526Indexstatus codes, 167support in .NET 2.0, 193–94SYST command, 171with telnet, 165USER command, 169utility, 165, 166FTP clients, 166, 179with COM, 177downloading, 173ﬁle management dialog, 193Logon dialog, 192FTP servers, 165230 message, 182accessing, 172anonymous access, 169inactive user disconnect, 169opening connections to, 166port listening, 168pulling ﬁle/folder details, 185root directory listing, 188TCP connection to, 182testing, 173UNIX, 170–71uploading to, 168Windows, 170working folders, moving, 189FunctionsBeginGetServerVariableNames, 493ConvertIPtoLong, 318createPassiveConnection(), 186describeResponse, 318–19getHTTP, 100GetLineID, 415getMine(), 122getMirror, 267, 268Increment, 260LineCallBack, 390Listener, 474makeResponse, 309Play, 410–11proc, 311, 313QThread, 432reportMessage, 266sendFTPcmd(), 182sendPassiveFTPcmd(), 185sendPOP3cmd, 147socketReadLine, 135, 138, 139syncRead, 25–26Future prooﬁng, 255–56Garbage collectiondeﬁned, 515distributed, 515–18Gateways, 199General public license (GPL), 275GET command, 91, 485Ghost, 203Global assembly cache (GAC), 11, 445Global XML Web Services Architecture (GXA), 497Goldwave, 296Google case study, 251–53architecture, 253DNS system, 252document servers, 253Googlebot, 253index server cluster, 252Gzip compression algorithm, 296Hashed timestamps, 497Hashing, 232–36, 497algorithms, 234keyed hash, 233MD5, 232MSMQ and, 448SHA-1, 232, 234–35uses, 232–33“Hello world” application, 56–62HTML, 9autogenerated, 113Index527Indexdata extraction from, 88editor application, 113WML conversion, 130HTTP, 87–130connections, 281deﬁned, 88GET command, 91, 485headers, 118, 122, 123implementation, 117message queues, 423nonalphanumeric character conversion, 100port 80 operation, 88, 115POST command, 91posts, 117request, 88–91request headers, 89–90, 92response, 91–93response codes, 92–93root, 114socket-level implementation, 93state information and, 104stream, 98WAP over, 128HTTP clientsapplications, 93, 96applications with POST facility, 103wireless, 129HTTP serversapplication, 121heart of, 114HTTP root, 114implementing, 94HttpUtility object, 102HttpWebListener class, 124–25deﬁned, 124methods/properties, 124–25HttpWebRequest class, 103–4HttpWebResponse classmethods/properties, 97object, 95parameters, 101Huffman compression, 291Hybrid modems, 395Hypertext markup language. See HTMLHypertext transfer protocol. See HTTPIBM KeyMan, 238IcmpV4Statistics class, 368–70IComPlusServer, 445ILease interface, 516–18Image compression, 298–302IMode browser, 130Insert statements, 46Interborder gateway protocols (IGBPs), 203InterfaceStatistics class, 373–74Interlacing, 278Internet Assigned Numbers Authority (IANA), 5multicast IP address control, 282Web site, 282Internet control message protocol (ICMP), 314C#, 315, 317, 318, 319, 320client application, 321deﬁned, 314echo, 317header, 348packet detail, 348–49packets, 314type code, 348–49uses, 321VB.NET, 315–16, 317–18, 319–20Internet Corporation for Assigned Names and Numbers (ICANN), 5Internet Engineering Task Force (IETF), 8IPng, 454–55MSEC, 284Public Key Infrastructure (PKIX), 237SMTP guide, 132Internet Explorer528IndexCertiﬁcates dialog, 240designMode property, 107execCommand function parameters, 106as WYSIWYG editor, 105Internet group management protocol (IGMP), 284Internet Information Services (IIS), 87add/remove components, 173authentication, 228–30hosting remote objects within, 510–11installation, 113servers, 495Internet message access protocol (IMAP), 158–59client, 158implementation, 159port 143 operation, 158server, 158Internet service providers (ISPs), 2dynamic host control protocol (DHCP) server, 5POP3 servers, 140Internet standards, 7–9Internet Transfer Control (ITC), 176, 178deﬁned, 176limitations, 178Interprocess communications (IPC), 451IntranetWare, 164Intrasite automatic tunnel addressing protocol (ISATAP), 469, 471–72addresses, 471deﬁned, 471hosts, 472routers, 472I/Ofailure, 22in .NET, 21–54IP addresses, 3–6classes, 5deﬁned, 3DNS servers, 313dynamic, 206example, 4families, 4IPv6, 454, 455, 456length, 453multicast, 282POP3, 148port numbers, 68posting, 206SMTP server, 132splitting into bytes, 318tracking, 482validation, 228VPN gateway, 199IPAddressInformation class, 374IP header, 346–47IP-level network tapping, 339–53C#, 340–42, 343–44implementing, 339packet detail, 346–48raw data interpretation, 344–46VB.NET, 340, 341, 342, 343, 344See also Network tappingIPng, 454–55IPSec6, 461–63deﬁned, 461illustrated, 463security associations database, 461, 463security policies database, 461, 462IPStatistics class, 370–72IPv4Properties class, 374–75IPv6, 453–79addresses, 454, 455, 456auto conﬁguration, 457C#, 473–75, 476, 477changes, 455–56client and server, 479coexistence, 469–73as command-line utility, 458–59compliance, 479deﬁned, 453–54Index529Indexdestination addresses, 465global addresses, 456history, 454–55installing, 457interface, 456introduction, 453naming conventions, 456in .NET, 473–79packets, 464preﬁx, 456QoS provision, 454route advertisements, 468–69routers, 457routing, 464–69routing advertisements, 468–69routing table administration, 466–68uninstalling, 457VB.NET, 474, 475–76, 477, 478IPv6 utilities, 458–64IPSec6, 461–63MS-DOS utility, 458NETSH, 459, 460ping6, 459–60tracert6, 460–61Windows 2000 speciﬁc, 463–64ISO 8730, 247–48Journaling, 441–43C#, 441–42deﬁned, 441VB.NET, 442JPEGheaders, 120saving, from loaded image, 300JPEG compression, 298–302application, 301C#, 299, 300implementation, 299process, 298VB.NET, 300, 301Just-in-time (JIT) compiler, 10Keep-alive connections, 277–78Kerberos authentication, 229deﬁned, 229speed, 230See also AuthenticationKey distribution center (KDC), 247Keysdeﬁned, 212piracy and, 224private, 213public, 213RSA, 214, 215symmetric, 223Layer 2 network tapping, 354–66PacketX, 360–66rvPacket, 354–60WinPCap, 354–66See also Network tappingLayer 2 tunneling protocol (L2TP), 199Link types, 366–67LIST command, 170Listener sockets, 77endpoint, 80parameters, 80ListenerWebRequest class, 125–26ListenerWebResponse class, 126–27Load balancers, 254Load balancing, 262–72application, 272C#, 264–67, 268, 269, 270, 271deﬁned, 262implementation, 264NLBS, 263VB.NET, 265, 266, 267, 268, 269–70, 271Local area networks (LANs), 4530IndexLock command, 116Lossless compression, 290–96deﬁned, 289methods, 290ZIP, 290, 291–96See also CompressionLossy compression, 296–303audio, 296–98deﬁned, 290image, 298–302video, 302–3See also CompressionLuminance, 302Mail application programming interface (MAPI), 153–57address book access, 156–57address book application, 157C#, 153–54client application, 155deﬁned, 153interface, 155VB.NET, 154Managed code, 15Managed information base (MIB), 328Maximum segment size (MSS), 279Maximum transmission unit (MTU), 279, 280datagram size, 280deﬁned, 280Message class, 426–27Message Digest (MD5), 232MessageQueue class, 422Message queues, 419–52acknowledgments, 437–39adding messages to, 435–37as backup system, 419complex objects, 427–34HTTP, 423implementing, 420–39introduction, 419journal, 441–43performance issues, 451–52queued components, 443–47scalability, 449–51security, 447–49timeouts, 439–41transactions, 435–37MethodsAbort, 436Connect, 73Dispose, 180EndAccept, 84EndInvoke, 507execCommand, 109, 112executeNonQuery, 47executeReader, 47Form1_Load, 66getDetails, 506, 507handlerThread, 69, 117IOControl, 343Listen, 73listenerThread, 67, 115ReadToEnd, 101, 223Receive, 81, 343, 434Run, 340, 343select, 109SetLevel, 293Write, 222Microsoft Access, 50Microsoft Certiﬁcate Services (MSCS), 240–41Microsoft Distributed Transaction Coordinator (MSDTC), 451Microsoft ﬁle sharing, 163–64Microsoft intermediate language (MSIL) code, 9, 10Microsoft Message Queue (MSMQ), 207, 420acknowledgments, 437–39administering, 420Index531Indexapplications, 420basic application, 424basic receiver application, 428clients, 450COM+ component services and, 443complex object queuing, 427–34complex object receiver application, 434complex object transfer application, 432console, 420deﬁned, 419disk space consumption, 449enterprises, 450hashing algorithm selection, 448HTTP support, 423, 424journal messages, 443message authentication in, 447message encryption in, 447message timeouts, 441multicast and, 452native XML message format, 425object target type, 426performance issues, 451–52queue settings dialog, 450scalability, 449–51security, 447–49servers, 423, 448sites, 450transactions, 435–37Microsoft Outlookaddress book, 156–57new accounts, 140Outlook Express, 153Microsoft Outlook Object Library, 153Microsoft Terminal Services (MTS), 275Mobile Web browsers, 128–30iMode, 130SDK, 130Motion Picture Expert Group (MPEG)compression, 302–3deﬁned, 302MQSeries, 451Multicast, 282–89backbone (MBONE), 283basics, 282–83deﬁned, 275, 282ﬁrst, 282functionality, 287implementing, 284–89IP address, 282MSMQ and, 452operation, 285routing, 283–84UDP, 282–89UDP client and server, 289Multicast address dynamic client allocation protocol (MADCAP), 283Multicast open shortest path ﬁrst (MOSPF), 283Multihomed networks, 203Multipart Internet mail extensions (MIME) types, 93deﬁned, 93ﬁnding, 93viewing, 123Multithreading, 59MyClass class, 510MyServices, 500Namespaces, 18CIM, 329forms, 192importing, 134NETBIOS, 164, 345deﬁned, 164packets, 354.NETCompatibility Routine, 413deﬁned, 2, 9–11FTP support, 193–94I/O in, 21–54IPv6 in, 473–79532Indexlanguages, 9–10managed controls, 15network programming in, 2permissions in, 244–46remoting, 500–518socket-level networking in, 75–86starting with, 11–12.NET Passport authentication, 230–32deﬁned, 230Manager Administration dialog, 231preproduction passports, 230production passports, 230steps, 231test page, 232uses, 230.NET SDK, 11C#, 17deﬁned, 11using, 16–20VB.NET, 17–18NETSH, 459, 460Netstat utility, 75Netwaredirectory service (NDS), 164ﬁle sharing, 164–65Network address translators (NATs), 4, 201–3deﬁned, 201development, 202dynamic, 202overlapping, 202overloading, 202static, 202NetworkInformation class, 366, 367–68Networkingpitfalls, avoiding, 205–7socket-level, 75–86NetworkInterface class, 372–73Network Load Balancing Service (NLBS), 263Network news transfer protocol (NNTP), 159–61deﬁned, 160port 119 operation, 159posting, 161reading, 160tasks, 160Network programmingin .NET, 2for scalability, 251–73socket-level, 55–86understanding, 1–20Network programscode, debugging, 73–75functions of, 2–3problems, 74Networksbuilding, from scratch, 195–99enterprise, building, 199–203ﬁrewalls, 200–203monitoring, 305–36multihomed, 203P2P, 207routers, 199–200setting up, 196–98topology, 195–96tunneling out of, 203–5VPN, 198–99Network stack, 6–7modern, 7traditional, 6NetworkStream class, 22, 183, 188Network tapping, 339–76IP-level, 339–53layer 2, 354–66physical, 366–76Next-level aggregator (NLA), 456NOOP command, 169Novell core protocol (NCP), 165NT challenge/response (NTLM), 229Index533IndexOpen shortest path ﬁrst (OSPF), 328Open systems interconnection (OSI) network, 6Oracle, database access, 44Packet ﬁltering, 200Packetsanalyzing, 337–77capture, 337deﬁned, 55DNS, detail, 352–53ICMP, 314ICMP, detail, 348–49IP, header, 346–47IP-layer sniffer application, 344IPv6, 464NETBIOS, 354port numbers, 56TCP/IP, 279, 343TCP/IP, detail, 349–51with TTL value, 280UDP, detail, 351–52PacketX control, 360–66C#, 361–63frame-layer packet sniffer with, 364packet detection, 361, 365polling mechanism, 362start of, 361VB.NET, 361, 362, 363–64PacketXCtrl class, 361PASS command, 169PASV command, 188Peer-to-peer (P2P) architecture, 207Performance, Web services, 494–95Performance increases, 275–81caching, 276–77keep-alive connections, 277–78progressive downloads, 278tweaking, 278–81Permissions, 244–46C#, 246conﬁguration utility, 245VB.NET, 246Physical network tapping, 366–76C#, 367NetworkInformation class, 367–68VB.NET, 367See also Network tappingPing. See Internet control message protocol (ICMP)Ping6 utility, 459–60command-line parameters, 460deﬁned, 459illustrated, 461Ping utility, 314PiracyCD-R, 224hackers and, 225keys and, 224protection, 224–25Plain text, 29, 212Point-to-point protocol (PPP), 328Point-to-point tunneling protocol (PPTP), 199Policy IP (PIP), 455POP3 serversIP address, 148ISP, 140Microsoft Exchange as, 141space, 141See also Post ofﬁce protocol 3 (POP3)Port numbersadded to IP address, 68low-order byte, 188well-known, 8, 55–56Portsaddress translation, 202COM, 380, 382conﬂict, 205Posting data, 97–104534IndexASP.NET, 98–99C#, 99, 100, 102VB.NET, 99–100, 101, 102Post ofﬁce protocol 3 (POP3), 140–48C#, 144, 145, 147client application, 148clients, 141client SPAM ﬁlter example, 144–48as command-line-based protocol, 141deﬁned, 140, 141implementing, 141–48mailbox access, 142mailbox summary information, 142VB.NET, 144, 146, 147–48See also POP3 serversPrimary enterprise controller (PEC), 450Primary Site Controllers (PSCs), 450Private assembly, 11Private keys, 213Private virtual circuits (PVCs), 247Progressive downloads, 278Project Hailstorm, 500Protocol independent multicast (PIM), 283Proxies, 201application, 201circuit-level, 201deﬁned, 201tunneling, 204Proxy caches, 276Public assembly, 11Public keys, 213QueriesDNS, 309SQL, 53WHOIS, 322, 324WMI, 333WQL, 333Queued components, 443–47C#, 443–44, 445, 446, 447creating, 443VB.NET, 444–45, 446, 447QUIT command, 170Redundancydeﬁned, 253load balancer, 254Registry Editor utility, 122Remote method invocation (RMI), 500Remote objects, 494–95asynchronous use, 506–7hosting within IIS, 510–11hosting within Windows service, 511–15servers, 510–11Remoting, 500–518client, 506conﬁguration, 509–10functioning of, 501–2implementing, 502–6key, 502server, 506service deployment, 508–9Replicationdeﬁned, 253SQL, 254Request for comments (RFCs), 8–9Reverse ARP (RARP), 327Rivest-Shamir-Adleman (RSA), 213deﬁned, 213keys, 214, 215using, 213–18Routers, 199–200client processing, 203connections, 200deﬁned, 200hardware characteristics, 264IPv6, 457ISATAP, 472Routesdetermination process, 465–66Index535Indexdirectly attached, 465host, 465remote, 465selection, 466RoutingIPv6, 464–69multicast, 283–84as Web service extension, 500Routing information protocol (RIP), 327–28Routing table, 464–65administering, 466–68ﬁelds, 467illustrated, 467self-maintaining, 466See also IPv6Run length encoding (RLE), 290RvPacket library, 354–60accessibility, 358development, 359frame-layer packet sniffer with, 360functions, 357Scalabilityarchitectural, 256deadlocks and, 261–62deﬁned, 251future prooﬁng, 255–56Google case study, 251–53load balancing, 262–72message queues, 449–51programming for, 251–73redundancy, 253–54replication, 253–54thread pooling, 255, 256–61Scalable network applications, 254–55Secure Hash Algorithm (SHA-1), 232, 234–35C#, 235deﬁned, 234using, 234–35VB.NET, 235See also HashingSecure hash standard (SHS), 234Secure sockets layer. See SSLSecuritycode access, 245ﬁnancial network, 246–49in ﬁnancial transactions, 209message queues, 447–49Web services, 495–97See also EncryptionSerialization, 33–44to binary, 41–42C#, 34–35, 37–39cookies, 105dataset, 51–54deﬁned, 33.NET classes, 40shallow, 42–44from SQL query, 53VB.NET, 35–37, 38–39Serial Line Internet Protocol (SLIP), 328Server activation, 515Server certiﬁcates, 238–39obtaining, 238testing, 239use steps, 238–39See also CertiﬁcatesServersDNS, 262, 305FTP, 165, 166HTTP, 94, 114, 121IIS, 495IMAP, 158MSMQ, 423, 448multicast UDP, 289POP3, 140–41proxy, 202remote object, 510–11remoting, 506SMTP, 132536IndexTCP/IP, 65–73UDP, 58–62Web, 113–24WHOIS, 321, 322Server-side caches, 276Shallow serialization, 42–44C#, 43, 44dataset, 53deﬁned, 42performing, 43VB.NET, 43, 44See also SerializationShared base classes, 508SharpDevelop, 11Simple IP (SIP), 455Simple mail transfer protocol (SMTP), 132–40client application, 151complaints department example, 134–40deﬁned, 132guide to, 132implementing, 133–40port 25 operation, 133UNIX design, 133See also SMTP serversSimple network management protocol (SNMP), 328Simple object access protocol (SOAP), 39attachments, 498–500formatting, 41over HTTP, 504result from Web service, 484toolkit, 494Single-call activation, 516Single threaded apartment (STA) applications, 18Singleton activation, 516Site-level aggregator (SLA), 456SmartFTP, 73SmtpMail object, 150SMTP serversIP address, 132single-threaded, 135stopping, 139virtual, 132SoapFormatter class, 40BinaryFormatter substitution, 41–42methods/properties, 41object, 42Soapsuds utility, 509Socket class, 76Sockets, 55–86adding, to array list, 68addressing schemes, 78–79array list, declaring, 114deﬁned, 55–56generic object, 77listener, 77, 80networking and, 75–86null, 69–70protocol types, 79–80SOCKS protocol, 201Source encoding, 290SPAMdeﬁned, 144POP3 client ﬁlter example, 144–48See also EmailSpeech waveform, 297SQL Server, 44, 49SSL, 227, 236deﬁned, 236encryption and authentication, 124end-to-end encryption, 236FTP over, 168HTTP over (HTTPS), 236NNTP over (SNNTP), 236Static NATs, 202StreamReader classmethods/properties, 31object, 221Streams, 21–54asynchronous use, 22Index537Indexbinary, 29–33capabilities, 21cryptographic, 222for ﬁles, 22–28fileStream, 22HTTP, 98networkStream, 22for reading ﬁles, 31synchronous use, 22text, 29–33writing databases to, 44–54writing objects to, 37Strength, 212Structured query language (SQL)database creation, 49–50queries, 53replication, 254statements, 45SurrogateSelector object, 37SWIFT, 248Symmetric encryption, 218–243DES, 218–24application, 224C#, 219–21, 222, 223decryption process, 223deﬁned, 218key, 223procedure, 221uses, 218VB.NET, 219, 220, 221, 222–23See also EncryptionSYST command, 171System.Web.Mail, 148–50C#, 149, 150VB.NET, 150TcpClient class, 65TCP/IPclients, writing, 62–65connections, opening, 64ﬁle transfer with, 62–73header, 349–50once-off transfer, 284packet detail, 349–51packets, 279, 343settings, tweaking, 279trafﬁc, 360use complexity, 56TCP/IP serversC#, 66, 67, 69, 70–71multithreaded, 65with socket-level code, 82VB.NET, 66, 67, 69, 70–71writing, 65–73TcpListener classconstructor for, 70methods/properties, 72TcpStatistics class, 375–76TelephonyAPI functions, 390AT commands, 380audio playback, 401–17basic, 380–82C#, 381callbacks, 382CTI, 377, 379DTMF tones, 399–401incoming phone call listening, 382–99modem response, 381VB.NET, 381Telephony Application Programming Interface (TAPI), 382architecture, 382audio playback over, 413–17callbacks, 382, 394call-receiver application, 399call-receiver with DTMF and playback, 417DTMF tones with, 399example, 383–99libraries, 382538IndexLineCallBack function, 390Telnet, 326–27clients, 326as de facto standard, 326deﬁned, 326FTP through, 165MS-DOS utility, 327opening, 81secure version, 327uses, 326Text streams, 29–33Thread pooling, 255, 256–61C#, 257deﬁned, 255implementation, 258–61population, 259sample application, 261uses, 257VB.NET, 257Threadsapplication responsiveness and, 256inﬁnite loop, 116, 434passing parameters to, 68Timeouts, 439–41C#, 439–40deﬁned, 439as message aging mechanism, 439VB.NET, 440See also Message queuesTop-level aggregator (TLA), 455TracePlus, 75, 337, 338Tracert6, 460–61command-line parameters, 461deﬁned, 460–61illustrated, 462See also IPv6 utilitiesTracert utility, 321Trafﬁc-detection software, 338Transactions, 435–37C#, 435–36deﬁned, 435support, 435VB.NET, 436See also Message queues; Microsoft Message Queue (MSMQ)Transmission control protocol (TCP). See TCP/IPTriple Data Encryption Standard (3DES), 213deﬁned, 218using, 218–24See also Symmetric encryptionTrivial ﬁle transfer protocol (TFTP), 163Tunneling, 203–5deﬁned, 204ﬁrewall, 205, 206–7proxy, 204Type-safe code, 15UdpClient classmethods/properties, 59object, 60UDP serversmultithreading, 59purpose, 58writing, 58–62UdpStatistics class, 376Unicode Transformation Format 8 (UTF8), 28Universal description discovery integration (UDDI), 486Universal Serial Bus (USB), 196Unshielded twisted pair (UTP), 195–96Update statements, 46URL path conversion, 118USER command, 169User datagram protocol (UDP)client, writing, 57–58connections, opening, 307deﬁned, 352header, 7, 351Index539Indexlatency, 56multicast, 282–89packet detail, 351–52server, writing, 58–62use, 56See also UDP serversVB.NET, 9acknowledgments, 438address book access, 157asymmetric encryption, 214–15, 216, 217–18attachments, 151, 152audio playback, 405–8, 410, 411–12, 413audio playback over TAPI, 414, 415, 416binary/text streams, 30, 32callbacks, 492–93code debugging, 73–74compiling in, 19complex object queuing, 429, 430–32, 433–34database programming, 45, 46, 47, 48dataset serialization, 51, 52–54DNS MX implementation, 307, 308–9, 310–11, 312–13DTMF tones, 400, 401FTP implementation, 179, 180, 181–83, 184, 186, 187–89, 190, 191–92FTP with ITC, 175, 176, 177ICMP, 315–16, 317–18, 319–20incoming phone call listening, 386–89, 391–92, 393–94, 396–97, 398IP-level network tapping, 340, 341, 342, 343, 344IPv6, 474, 475–76, 477, 478journaling, 442JPEG compression, 300, 301leveraging WMI, 334–35load balancing, 265, 266, 267, 268, 269–70, 271main function coding, 19MAPI, 154message queue implementation, 421–22, 423, 424, 425–26, 427multicast implementation, 284, 285–87, 288, 289.NET SDK, 17–18PacketX control, 361, 362, 363–64permissions, 246physical network tapping, 367POP3, 144, 146, 147–48posting data, 99–100, 101, 102proxy tunneling, 204queued components, 444–45, 446, 447reading certiﬁcates, 242, 243remote object hosting, 512, 513, 514remoting conﬁguration, 510remoting implementation, 503, 504, 505–6remoting service deployment, 508serialization, 35–37, 38–39serializing to binary, 41–42SHA-1, 235shallow serialization, 43, 44SMTP implementation, 135, 137–38, 139SOAP attachments, 498, 499socket-level networking, 77, 81, 82, 83, 84, 85streams for ﬁles, 23, 24–25, 26symmetric encryption, 219, 220, 221, 222–23System.Web.Mail, 150TCP/IP client, 63–64TCP/IP server, 66, 67, 69, 70–71telephony, 381thread pool implementation, 258, 259, 260, 261thread pooling, 257timeouts, 440transactions, 436UDP client, 57, 58540IndexUDP server, 60, 61Visual Studio .NET, 13wait handles, 490–91Web server implementation, 114, 115, 116–17, 118–19, 120, 122, 123, 124Web service deployment, 486Web services, 482–83Web service security, 496, 497Web service use, 487, 488WHOIS, 325WinPCap with rvPacket, 356–57, 358–59WMI, 331–32, 333WYSIWYG editor, 108, 109, 110–11, 112ZIP compression, 292, 293–94, 295Video compression, 302–3Virtual private networks (VPNs)advantages, 199deﬁned, 198gateway address, 199operation, 199setting up, 198–99Visual Studio .NET, 11Add Reference dialog, 149C# code, 14Customize Toolbox dialog, 13, 51deﬁned, 11form design view, 14.NET Add Reference dialog, 102New Project dialog, 12Properties tool window, 15using, 12–16VB.NET code, 13Voice over IP (VOIP), 454Wait handles, 490–91C#, 490deﬁned, 490VB.NET, 490–91See also Web servicesWave sound player application, 413Web browsersiMode, 130Internet Explorer, 105–7, 240mobile, 128–30WebProxy class, 268WebRequest class, 95Web scripting languages, 99Web servers, 113–24implementing, 114–24installation, 113response, 95Web Service Deﬁnition Language (WSDL), 485Web services, 481–500asynchronous calls to, 489–93attachments, 498–500benchmarking tests, 494beneﬁts, 481C#, 482, 483callbacks, 491–93client application, 489clients, testing, 488creating, 481–86deploying, 485–86enhancements, 497–500extensions, 498–500interoperability, 493–94performance, 494–95routing, 500security, 495–97SOAP result, 484using, 486–89VB.NET, 482–83wait handles, 490–91Web Services Enhancements (WSE), 497, 498, 499WHOIS, 321–26C#, 324, 325client application, 326deﬁned, 321Index541Indexdesign, 321queries, 322, 324reply, 323servers, 321, 322VB.NET, 325Widgets, 18Windows Management Instrumentation (WMI), 303, 329–35C#, 330–31, 332class, 333, 334data, 330deﬁned, 329leveraging, 333–35queries, 333query language analyzer application, 333remote process manager application, 335uses, 329VB.NET, 331–32, 333Windows multimedia API functions, 408Windows registryaccess functionality, 124data, drilling down, 123deﬁned, 122WinPCap, 354–66hardware ﬁlters, 365packet send/receive capability, 366with PacketX control, 360–66with rvPacket wrapper, 354–60Wireless application protocol (WAP), 128Wireless markup language (WML), 128HTML conversion to, 130minimal page in, 129sample page, 129Wireless transfer protocol (WTP), 128World Wide Web Consortium (W3C), 8, 9deﬁned, 9responsibilities, 9WSDL.EXE, 485WYSIWYG editor, 105–13C#, 108, 109, 110, 112deﬁned, 105Internet Explorer as, 105VB.NET, 108, 109, 110–11, 112X.25, 247X.509 certiﬁcates, 241–42, 449XML, 9remote object hosting, 511remoting conﬁguration, 509serialized object view, 40support, 493Web service response, 484XML schema deﬁnition (XSD), 43XMLSerializer class, 44YUV format, 302ZIP compression, 290, 291–96application, 295C#, 292, 293, 295decompression and, 296ﬁles, 293, 294, 295, 296Huffman compression, 291implementing, 291–96VB.NET, 292, 293–94, 295See also Lossless compression