1 Understanding the Internet and Network Programming 1 1.1 Introduction 1 1.2 Why network programming in .NET? 2 1.3 What can a network program do? 2 1.4 IP addresses 3 1.5 The network stack 6 1.6 Ports 7 1.7 Internet standards 7 1.8 What is .NET? 9 1.9 Getting started 11 1.10 Using Visual Studio .NET 12 1.11 Using the .NET SDK 16 1.11.1 Compiling with Visual Basic.NET 19 1.11.2 Compiling with C# 20 1.11.3 Testing the application 20 1.12 Conclusion 20 2 I/O in the .NET Framework 21 2.1 Introduction 21 2.2 Streams 21 2.2.1 Streams for files 22 2.2.2 Encoding data 28 2.2.3 Binary and text streams 29 2.2.4 Serialization 33 2.2.5 Writing a database to a stream 44 2.3 Conclusion 54 3 Working with Sockets 55 3.1 Introduction 55 3.2 What is a socket? 55 3.3 Creating a simple “hello world” application 56 3.3.1 Writing a simple UDP client 57 3.3.2 Writing a simple UDP server 58 3.4 Using TCP/IP to transfer files 62 3.4.1 Writing a simple TCP/IP client 62 3.4.2 Writing a simple TCP/IP server 65 3.5 Debugging network code 73 3.6 Socket-level networking in .NET 75 3.7 Conclusion 86 4 HTTP: Communicating with Web Servers 87 4.1 Introduction 87 4.1.1 Data mining 88 4.2 HTTP 88 4.2.1 The HTTP request 88 4.2.2 The HTTP response 91 4.2.3 MIME types 93 4.2.4 System.Web 93 4.2.5 Posting data 97 4.2.6 A note on cookies 104 4.2.7 A WYSIWYG editor 105 4.3 Web servers 113 4.3.1 Implementing a Web server 114 4.4 System.Net.HttpWebListener 124 4.5 Mobile Web browsers 128 4.5.1 Mobile Web SDK 130 4.6 Conclusion 130 5 SMTP and POP3: Communicating with email Servers 131 5.1 Introduction 131 5.2 Sending an email 131 5.3 SMTP 132 5.3.1 Implementing SMTP 133 5.4 Post office protocol 3 140 5.4.1 Implementing POP3 141 5.5 System.Web.Mail 148 5.5.1 Attachments 151 5.5.2 Images 153 5.6 Mail application programming interface 153 5.6.1 Accessing the address book 156 5.6.2 IMAP 158 5.6.3 Network news transfer protocol 159 5.7 Conclusion 161 6 FTP: Communicating with File Servers 163 6.1 Background 163 6.2 Microsoft file sharing 163 6.3 Netware file sharing 164 6.4 An overview of FTP 165 6.4.1 How FTP uses ports 167 6.4.2 The FTP handshake 168 6.4.3 Navigating folders 170 6.4.4 FTP command reference 171 6.4.5 Implementing FTP 172 6.4.6 Implementing FTP with the Internet Transfer Control 174 6.4.7 A more substantial implementation of FTP 178 6.4.8 FTP support in .NET 2.0 193 6.5 Conclusion 194 7 Securing a Network: Firewalls, Proxy Servers, and Routers 195 7.1 Introduction 195 7.1.1 Building a network from scratch 195 7.2 Building an enterprise network 199 7.2.1 Routers 199 7.2.2 Firewalls 200 7.3 Tunneling out of an enterprise network 203 7.4 Avoiding the networking pitfalls 205 7.4.1 Firewall tunneling 206 7.5 Conclusion 207 8 Protecting Data: Encryption 209 8.1 Introduction 209 8.2 Cryptanalysis 209 8.3 Terminology 212 8.4 Asymmetric encryption 212 8.5 Using RSA as asymmetric encryption 213 8.6 Symmetric encryption 218 8.6.1 Using 3DES as symmetric encryption 218 8.7 Piracy protection 224 8.8 Conclusion 225 9 Controlling User Access: Authentication and Authorization 227 9.1 Introduction 227 9.2 Authentication techniques 227 9.2.1 IIS authentication 228 9.3 Microsoft .NET Passport authentication 230 9.4 Hashing information 232 9.4.1 Hashing algorithms 234 9.4.2 Using SHA 234 9.5 SSL 236 9.6 Certificates 236 9.7 Server certificates 238 9.8 Client certificates 239 9.8.1 Microsoft Certificate Services 240 9.8.2 Reading certificates 241 9.9 Permissions in .NET 244 9.10 Financial network security 246 9.10.1 X.25 247 9.10.2 ISO 8730 247 9.10.3 SWIFT 248 9.10.4 Corporate transactions 248 9.11 Conclusion 249 10 Programming for Scalability 251 10.1 Introduction 251 10.2 Case study: The Google search engine 251 10.3 Replication and redundancy 253 10.4 Scalable network applications 254 10.5 Future proofing 255 10.6 Thread pooling 256 10.6.1 Implementing a thread pool 258 10.7 Avoiding deadlocks 261 10.8 Load balancing 262 10.9 Conclusion 272 11 Optimizing Bandwidth Utilization 275 11.1 Introduction 275 11.2 Tricks and tips to increase performance 275 11.2.1 Caching 276 11.2.2 Keep-alive connections 277 11.2.3 Progressive downloads 278 11.2.4 Tweaking settings 278 11.3 Multicast UDP 282 11.3.1 Multicast basics 282 11.3.2 Multicast routing 283 11.3.3 Implementing multicast 284 11.4 Data compression 289 11.5 Lossless compression 290 11.5.1 Implementing ZIP compression 291 11.6 Lossy compression 296 11.6.1 Audio compression 296 11.6.2 Image compression 298 11.6.3 Video compression 302 11.7 Conclusion 303 12 Ping, DNS, and WHOIS: Monitoring your Network 305 12.1 Introduction 305 12.2 DNS 305 12.2.1 Implementing DNS MX 306 12.3 Ping 314 12.4 WHOIS 321 12.4.1 Telnet 326 12.5 Other members of the TCP/IP suite 327 12.5.1 ARP 327 12.5.2 RIP 327 12.5.3 OSPF 328 12.5.4 BGP/EGP 328 12.5.5 SNMP 328 12.5.6 PPP 328 12.6 WMI 329 12.6.1 Reading WMI data 330 12.6.2 Leveraging WMI 333 12.7 Conclusion 336 13 Analyzing Network Packets 337 13.1 Introduction 337 13.2 IP-level network tapping 339 13.2.1 Interpreting raw network data 344 13.2.2 IP packets in detail 346 13.2.3 ICMP packets in detail 348 13.2.4 TCP/IP packets in detail 349 13.2.5 UDP packets in detail 351 13.2.6 DNS packets in detail 352 13.3 Layer 2 network tapping 354 13.3.1 Using rvPacket and WinPCap 354 13.3.2 Using PacketX and WinPCap 360 13.4 Physical network tapping 366 13.5 Conclusion 376 14 Adding Digital Telephony 379 14.1 Introduction 379 14.2 Basic telephony 380 14.3 Listening for incoming phone calls 382 14.4 DTMF tones 399 14.5 Audio playback 401 14.5.1 Audio playback over TAPI 413 14.6 Conclusion 417 15 Message Queues 419 15.1 Introduction 419 15.2 MSMQ 420 15.3 Implementing a message queue 420 15.3.1 Queuing complex objects 427 15.3.2 Transactions 435 15.3.3 Acknowledgments 437 15.4 Timeouts 439 15.5 Journal 441 15.6 Queued Components 443 15.7 Security 447 15.8 Scalability 449 15.9 Performance issues 451 15.10 Conclusion 452 16 IPv6: Programming for the Next-generation Internet 453 16.1 Introduction 453 16.2 What is IPv6? 453 16.3 The history of IPv6 454 16.4 So what changes? 455 16.5 IPv6 naming conventions 456 16.6 Installing IPv6 457 16.6.1 Auto configuration 457 16.7 Using IPv6 utilities 458 16.7.1 IPv6 458 16.7.2 NETSH 459 16.7.3 Ping6 459 16.7.4 Tracert6 460 16.7.5 IPSec6 461 16.7.6 Windows 2000 specific 463 16.8 IPv6 routing 464 16.8.1 Route determination process 465 16.8.2 Administering the IPv6 routing table 466 16.8.3 IPv6 routing advertisements 468 16.9 IPv6 coexistence 469 16.9.1 The 6to4 protocol 469 16.9.2 The ISATAP protocol 471 16.9.3 The 6over4 protocol 473 16.10 IPv6 in .NET 473 16.11 Conclusion 479 17 Web Services and Remoting 481 17.1 Introduction 481 17.2 Creating a Web service 481