ebook img

An Introduction to Network Programming with Java PDF

433 Pages·2007·3.241 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview An Introduction to Network Programming with Java

An Introduction to Network Programming with Java Jan Graba An Introduction to Network Programming with Java Jan Graba, BA, PGCE, MSc Faculty of ACES Sheffield Hallam University UK British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library Library of Congress Control Number: 2006923894 ISBN-10: 1-84628-380-9 ISBN-13: 978-1-84628-380-2 Printed on acid-free paper © Jan Graba 2007 New and revised edition ofAn Introduction to Network Programming with Javapublished by Addison Wesley, 2003, ISBN 0321116143 Sun, Sun Microsystems, the Sun Logo, the Java programming language, J2SE 5.0, and JavaBeans are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Microsoft, Encarta, MSN, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publish- ers, or in the case of reprographic reproduction in accordance with the terms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. The use of registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Whilst we have made considerable efforts to contact all holders of copyright material contained in this book, we may have failed to locate some of them. Should holders wish to contact the Publisher, we will be happy to come to some arrangement with them. Printed in the United States of America (SB) 9 8 7 6 5 4 3 2 1 Springer Science+Business Media, LLC springer.com Preface The market in general-purpose Java texts is an exceptionally well populated one, as can be seen from just a cursory examination of the programming language section of any major bookshop. Surprisingly, the market in Java network programming texts is a much less exploited one, featuring very few texts. It is true that the better general- purpose Java texts provide some guidance on aspects of network programming, but this almost invariably takes the form of rather superficial coverage, often relegated to the end of the particular text and offering little more than an outline of the relevant concepts. Even those few texts that are devoted specifically to network programming in Java (and one or two are very good indeed) are rather thick tomes that are probably of greatest use as reference texts. The truth of this assertion appears to be reinforced by the absence of practical exercises from such texts. When I began work on the first edition of this work, my declared intention was to write a more 'streamlined' work that could serve equally as the core text on an undergraduate module and as the quick, clear, 'no-nonsense' guide required by a busy IT professional. Numerous examples and associated screenshots were provided, with the examples 'stripped down' to their bare essentials in order to avoid overwhelming the reader with too much detail. There is, of course, a level of detail below which it is impossible to go without omitting some of the essentials and this led to a few examples running over multiple pages. However, a conscious effort was made to keep this to a minimum and to provide adequate program comments where this did occur. It was gratifying to find that the first edition was well received, but the time has now come to replace it with an updated version. The changes in this second edition fall into three categories: • language changes, largely reflecting the significant changes introduced by J2SE 5.0 (but also including one or two changes brought in by earlier versions of Java); • the updating of support software, particularly that used for Web applications; • new material, some related to the changes introduced by J2SE 5.0 (otherwise known as Java 5) and some extending earlier coverage. A summary of the major elements of these changes is given below. Language Changes These mostly affect the example programs and the model solutions provided on the associated Web site. However, there are occasional, passing references to some of these new features in the main body of the text where it is considered appropriate. The main language changes introduced are listed below. • Replacement of the BufferedReader+InputStreamReader combination with the single Scanner class, with consequent erradication of the need to use the vi An Introduction to Network Programming with Java type 'wrapper' classes to convert String input into numeric values (a major improvement on the traditional method for obtaining input). • Associated with the above, replacement of the BufferedReader+FileReader combination with Scanner+File and that of the PrintWriter+FileWriter combination with PrintWriter+File for serial disc file I/O. • Replacement of the cumbersome addWindowListener(new WindowAdapter... method for closing down GUI applications with setDefaultCloseOperation(EXIT_ON_CLOSE). (This had been available since J2SE 1.3, but had deliberately not been included in the original text due to the earlier method being the one still used by most people at that time.) • The formatting of numeric output (particularly decimal output) via method printf. • The inclusion of generics in the declaration of Vectors, with the associated 'auto-boxing' and 'auto-unboxing' of elements. • Introduction of the 'enhanced for' loop where appropriate. Updating of Support Software • Replacement of the JSWDK Web server with Apache Tomcat. • Replacement of the JavaBean Development Kit (BDK) with the Bean Builder for the testing of JavaBeans. • Removal of the section on the now defunct HTMLConverter utility and updating of the example browsers to Internet Explorer 6 and Firefox 1.5. New Material • Coverage of non-blocking I/O (introduced in J2SE 1.4), but retaining coverage of the more traditional blocking I/O. • The use of JDBC with the DataSource interface (also introduced in J2SE 1.4), but maintaining coverage of the more traditional DriverManager class approach. The associated examples are no longer confined to the use of MS Access, but have been extended to include MySQL. The significant advantages to large-scale, commercial databases of using DataSource in preference to DriverManager are made clear to the reader. • As part of good practice, the above DataSource technique makes use of a DAO (Data Access Object) to encapsulate access to the database, so that data manipulation code is separated from business logic. On the CD-ROM accompanying this text may be found the executable file for installing J2SE 5.0 onto MS Windows platforms (available via free download from the Sun site, of course). In addition to this, the CD contains all example code and several media files (the latter for use with material in the final two chapters). Model solutions for end-of-chapter exercises are accessible by lecturers and other authorised individuals from the companion Web site (accessible via http://homepage.ntlworld.com/jan.graba/javanet.html). Finally, there is a document entitled Java Environment Installation that provides downloading and installation Preface vii instructions for those additional software elements for which permission for inclusion on the CD was not forthcoming. This document also contains installation instructions (and downloading instructions, which shouldn't be required) for J2SE 5.0 itself. I sincerely hope that your programming experiences while using this text give you some of the sense of satisfaction that I have derived from writing it. Of course, along with such satisfaction comes the occasional (?) infuriating sense of frustration when things just won't work, but you wouldn't want things to be too easy ... would you?? :-) Jan 22nd Feb 2006 Contents Chapter 1 Basic Concepts, Protocols and Terminology ......................... 1 1.1 Clients, Servers and Peers ................................................................. 1 1.2 Ports and Sockets .............................................................................. 2 1.3 The Internet and IP Addresses .......................................................... 3 1.4 Internet Services, URLs and DNS .................................................... 4 1.5 TCP ................................................................................................... 5 1.6 UDP .................................................................................................. 7 Chapter 2 Starting Network Programming in Java ............................... 9 2.1 The InetAddress Class ...................................................................... 9 2.2 Using Sockets ................................................................................... 12 2.2.1 TCP Sockets ..... .................................................................... 12 2.2.2 Datagram (UDP) Sockets ....................................................... 18 2.3 Network Programming with GUIs ................................................... 28 2.4 Downloading Web Pages ................................................................. 37 Exercises ................................................................................................... 41 Chapter 3 Multithreading and Multiplexing .......................................... 51 3.1 Thread Basics ................................................................................... 51 3.2 Using Threads in Java ..................................................................... 52 3.2.1 Extending the Thread Class ................................................... 53 3.2.2 Explicitly Implementing the Runnable Interface ...................... 57 3.3 Multithreaded Servers ..................................................................... 60 3.4 Locks and Deadlock ........................................................................ 65 3.5 Synchronising Threads .................................................................... 67 3.6 Non-Blocking Servers ..................................................................... 74 3.6.1 Overview ................................................................................. 74 3.6.2 Implementation ......................................................................... 76 3.6.3 Further Details .......................................................................... 86 Exercises .................................................................................................. 88 Chapter 4 File Handling .......................................................................... 91 4.1 Serial Access Files .......................................................................... 91 4.2 File Methods ................................................................................... 97 4.3 Redirection .................................................................................... 99 4.4 Command Line Parameters ............................................................ 101 4.5 Random Access Files ..................................................................... 102 4.6 Serialisation .................................................................................... 109 4.7 File I/O with GUIs ......................................................................... 113 4.8 Vectors ........................................................................................... 120 4.9 Vectors and Serialisation ............................................................... 123 Contents ix Exercises ................................................................................................. 132 Chapter 5 Remote Method Invocation (RMI) ...................................... 136 5.1 The Basic RMI Process .................................................................. 136 5.2 Implementation Details .................................................................. 137 5.3 Compilation and Execution ............................................................ 141 5.4 Using RMI Meaningfully ............................................................... 143 5.5 RMI Security .................................................................................. 153 Exercises ................................................................................................. 156 Chapter 6 CORBA ................................................................................... 158 6.1 Background and Basics ................................................................... 158 6.2 The Structure of a Java IDL Specification ...................................... 159 6.3 The Java IDL Process ...................................................................... 163 6.4 Using Factory Objects ..................................................................... 173 6.5 Object Persistence ........................................................................... 184 6.6 RMI-IIOP ........................................................................................ 184 Exercises .................................................................................................. 186 Chapter 7 Java Database Connectivity (JDBC) ................................... 188 7.1 The Vendor Variation Problem ....................................................... 188 7.2 SQL and Versions of JDBC ............................................................ 189 7.3 Creating an ODBC Data Source ..................................................... 190 7.4 Simple Database Access .................................................................. 191 7.5 Modifying the Database Contents ................................................... 199 7.6 Transactions .................................................................................... 203 7.7 Meta Data ........................................................................................ 204 7.8 Using a GUI to Access a Database ................................................. 207 7.9 Scrollable ResultSets in JDBC 2.0 .................................................. 210 7.10 Modifying Databases via Java Methods ......................................... 215 7.11 Using the DataSource Interface ...................................................... 220 7.11.1 Overview and Support Software ............................................ 220 7.11.2 Defining a JNDI Resource Reference ....................................... 222 7.11.3 Mapping the Resource Reference onto a Real Resource ........... 223 7.11.4 Obtaining the Data Source Connection ..................................... 225 7.11.5 Data Access Objects .................................................................. 226 Exercises .................................................................................................. 232 Chapter 8 Servlets .................................................................................... 234 8.1 Servlet Basics .................................................................................... 234 8.2 Setting up the Servlet API ............................................................... 235 8.3 Creating a Web Application ............................................................ 237 8.4 The Servlet URL and the Invoking Web Page ................................ 239 8.5 Servlet Structure ............................................................................... 240 8.6 Testing a Servlet .............................................................................. 242 8.7 Passing Data .................................................................................... 242 x An Introduction to Network Programming with Java 8.8 Sessions ........................................................................................... 249 8.9 Cookies ............................................................................................ 260 8.10 Accessing a Database Via a Servlet ................................................ 268 Exercises .................................................................................................. 275 Chapter 9 JavaServer Pages(JSPs) ........................................................ 278 9.1 The Rationale behind JSPs ............................................................... 278 9.2 Compilation and Execution ............................................................. 279 9.3 JSP Tags .......................................................................................... 280 9.4 Implicit JSP Objects ........................................................................ 283 9.5 Collaborating with Servlets ............................................................. 285 9.6 JSPs in Action ................................................................................. 285 9.7 Error Pages ...................................................................................... 291 9.8 Using JSPs to Access Remote Databases ....................................... 294 Exercises .................................................................................................. 295 Chapter 10 JavaBeans ................................................................................ 297 10.1 Introduction to the Bean Builder .................................................... 298 10.2 Creating a JavaBean ....................................................................... 301 10.3 Exposing a Bean's Properties ......................................................... 307 10.4 Making Beans Respond to Events ................................................. 311 10.5 Using JavaBeans within an Application ........................................ 315 10.6 Bound Properties ........................................................................... 317 10.7 Using JavaBeans in JSPs ............................................................... 324 10.7.1 The Basic Procedure ........................................................ 324 10.7.2 Calling a Bean's Methods Directly .................................. 326 10.7.3 Using HTML Tags to Manipulate a Bean's Properties .... 330 Exercises .................................................................................................. 342 Chapter 11 Introduction to Enterprise JavaBeans ................................. 345 11.1 Categories of EJB ........................................................................... 345 11.2 Basic Structure of an EJB ............................................................... 346 11.3 Packaging and Deployment ............................................................ 349 11.4 Client Programs .............................................................................. 351 11.5 Entity EJBs ..................................................................................... 353 Chapter 12 Multimedia ............................................................................. 359 12.1 Transferring and Displaying Images Easily ................................... 360 12.2 Transferring Media Files ................................................................ 365 12.3 Playing Sound Files ....................................................................... 370 12.4 The Java Media Framework ........................................................... 372 Exercises ................................................................................................. 379 Chapter 13 Applets ................................................................................... 380 13.1 Applets and JApplets ...................................................................... 380 Contents xi 13.2 Applet Basics and the Development Process ................................. 381 13.3 The Internal Operation of Applets .................................................. 385 13.4 Using Images in Applets ................................................................ 392 13.4.1 Using Class Image ........................................................... 392 13.4.2 Using Class ImageIcon .................................................... 397 13.5 Scaling Images ................................................................................ 400 13.6 Using Sound in Applets .................................................................. 401 Exercises .................................................................................................. 405 Appendix A Structured Query Language (SQL) ............................. 406 Appendix B Deployment Descriptors for EJBs ................................ 411 Appendix C Further Reading ............................................................. 414 Index ............................................................................................................. 417

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.