Network Programming in .NET Metzger, Debugging by Thinking, ISBN 1-55558-307-5, 600pp, 2003 Mosher, Microsoft Outlook Programming: Jump Start for Administrators, Developers, and Power Users, ISBN 1-55558-286-9, 624pp, 2002 Lawrence, Compaq Visual Fortran: A Guide to Creating Windows Applications, ISBN 1-55558-249-4, 468pp, 2002 Breakfield & Burkey, Managing Systems Migrations and Upgrades: Demystifying the Technology Puzzle, 320pp, ISBN 1-55558-256-7, 2002 For more information or to order these and other Digital Press titles, 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 about our 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 Press 200 Wheeler Road, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK Copyright © 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: [email protected]. 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/digitalpress 04 05 06 07 08 09 10 9 8 7 6 5 4 3 2 1 Printed in the United States of America To my parents, thank you for everything. This page intentionally left blank Contents Preface xv Who should read this book? xv What hardware and software do you need? xvi How this book is organized xvi Part I: Basic network applications xvi Part II: Network application design xvi Part III: Specialized networking topics xvii Conventions used in this book xvii Further information xviii Acknowledgments xix 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 vii viii Contents 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 Contents ix 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 Contents