TCP/IP Sockets in Java
Second Edition
The Morgan Kaufmann Practical Guides Series
Series Editor: Michael J. Donahoo
TCP/IP Sockets in Java: Practical Guide for Programmers, Second Edition
Kenneth L. Calvert and Michael J. Donahoo
SQL: Practical Guide for Developers
Michael J. Donahoo and Gregory Speegle
C# 2.0: Practical Guide for Programmers
Michel de Champlain and Brian Patrick
Multi-Tier Application Programming with PHP: Practical Guide for Architects and Programmers
David Wall
TCP/IP Sockets in C#: Practical Guide for Programmers
David Makofske, Michael J. Donahoo, and Kenneth L. Calvert
Java Cryptography Extensions: Practical Guide for Programmers
Jason Weiss
JSP: Practical Guide for Programmers
Robert Brunner
JSTL: Practical Guide for JSP Programmers
Sue Spielman
Java: Practical Guide for Programmers
Michael Sikora
Multicast Sockets: Practical Guide for Programmers
David Makofske and Kevin Almeroth
The Struts Framework: Practical Guide for Java Programmers
Sue Spielman
TCP/IP Sockets in C: Practical Guide for Programmers
Kenneth L. Calvert and Michael J. Donahoo
JDBC: Practical Guide for Java Programmers
Gregory Speegle
For further information on these books and for a list of forthcoming titles,
please visit our Web site at http://www.mkp.com.
TCP/IP Sockets in Java
Practical Guide for Programmers
Second Edition
Kenneth L. Calvert
University of Kentucky
Michael J. Donahoo
Baylor University
AMSTERDAM • BOSTON • HEIDELBERG • LONDON
NEW YORK • OXFORD • PARIS • SAN DIEGO
SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
Morgan Kaufmann Publishers is an imprint of Elsevier
Joanne Tracy
Publishing Director
Publisher Denise E. M. Penrose
Acquisitions Editor Rick Adams
Publishing Services Manager George Morrison
Senior Production Editor Dawnmarie Simpson
Assistant Editor Michele Cronin
Production Assistant Lianne Hong
Cover Design Alisa Andreola
Cover Images
Composition diacriTech
Technical Illustration diacriTech
Copyeditor
Proofreader
Indexer
Interior printer
Cover printer Phoenix Color, Inc
JC Publishing
Janet Cocker
Sheridan Books, Inc
istock
Joan Green
Morgan Kaufmann Publishers is an imprint of Elsevier.
30 Corporate Drive, Suite 400, Burlington, MA 01803, USA
This book is printed on acid-free paper.
© 2008 by Elsevier Inc. All rights reserved. Reproduced with permission from TCP/IP.
Designations used by companies to distinguish their products are often claimed as trademarks or registered
trademarks. In all instances in which Morgan Kaufmann Publishers is aware of a claim, the product names
appear in initial capital or all capital letters. Readers, however, should contact the appropriate companies for
more complete information regarding trademarks and registration.
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, scanning, or otherwise—without 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. You may also
complete your request online via the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact”
then “Copyright and Permission” and then “Obtaining Permissions.”
Library of Congress Cataloging-in-Publication Data
Calvert, Kenneth L.
TCP/IP sockets in Java : practical guide for programmers / Kenneth L. Calvert, Michael J.
Donahoo. – 2nd ed.
p. cm.
Includes bibliographical references and index.
ISBN 978-0-12-374255-1 (pbk. : alk. paper) 1. Internet programming. 2. TCP/IP (Computer network
protocol) 3. Java (Computer program language) I. Donahoo, Michael J. II. Title.
QA76.625.C35 2008
005.13
3–dc22
ISBN: 978-0-12-374255-1
For information on all Morgan Kaufmann publications,
visit our Web site at www.mkp.com or www.books.elsevier.com
Printed in the United States
08 09 10 11 12
5 4 3 2 1
2007039444
To Tricia and Lisa
This page intentionally left blank
Contents
1
2
Preface xi
Introduction 1
1.1 Networks, Packets, and Protocols 1
1.2 About Addresses 4
1.3 About Names 6
1.4 Clients and Servers 6
1.5 What Is a Socket? 7
1.6
Exercises 8
Basic Sockets 9
2.1
2.2 TCP Sockets 15
Socket Addresses 9
2.2.1 TCP Client 16
2.2.2 TCP Server 21
2.2.3 Input and Output Streams 25
2.3 UDP Sockets 26
2.3.1 DatagramPacket 27
2.3.2 UDP Client 29
2.3.3 UDP Server 34
2.3.4 Sending and Receiving with UDP Sockets 36
Exercises 38
2.4
3
Sending and Receiving Data 39
3.1
Encoding Information 40
3.1.1 Primitive Integers 40
vii