Sockets-C.pdf

(12234 KB) Pobierz
711346682 UNPDF
711346682.001.png
30 Corporate Drive, Suite 400, Burlington, MA 01803, USA
This book is printed on acid-free paper.
Copyright © 2009 by Elsevier Inc. All rights reserved.
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. All trademarks that appear or are otherwise
referred to in this work belong to their respective owners. Neither Morgan Kaufmann Publishers nor the
authors and other contributors of this work have any relationship or a liation with such trademark
owners nor do such trademark owners confirm, endorse or approve the contents of this work. Readers,
however, should contact the appropriate companies for more information regarding trademarks
and any related registrations.
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
Application Submitted
ISBN: 978-0-12-374540-8
For information on all Morgan Kaufmann publications,
visit our Web site at www.mkp.com or www.elsevierdirect.com
Printed in The United States of America
09101112131415 16 54321
711346682.002.png
Preface to the Second Edition
When we wrote the first edition of this book, it was not very common for college courses on
networking to include programming components. That seems dicult to believe now, when
the Internet has become so important to our world, and the pedagogical benefits of hands-on
programming and real-world protocol examples are so widely accepted. Although there are now
other languages that provide access to the Internet, interest in the original C-based Berkeley
Sockets remains high. The Sockets API (application programming interface) for networking
was developed at UC Berkeley in the 1980s for the BSD flavor of UNIX—one of the very first
examples of what would now be called an open-source project.
The Sockets API and the Internet both grew up in a world of many competing protocol
families—IPX, Appletalk, DECNet, OSI, and SNA in addition to Transmission Control Proto-
col/Internet Protocal (TCP/IP)—and Sockets was designed to support them all. Fewer protocol
families were in common use by the time we wrote the first edition of this book, and the num-
ber today is even smaller. Nevertheless, as we predicted in the first edition, the Sockets API
remains important for those who want to design and build distributed applications that use
the Internet—that is, that use TCP/IP. And the interface has proven robust enough to support
the new version of the Internet Protocol (IPv6), which is now supported on virtually all common
computing platforms.
Two main considerations motivated this second edition. First, based on our own experi-
ence and feedback from others, we found that some topics needed to be presented in more
depth and that others needed to be expanded. The second consideration is the increasing
acceptance and use of IP version 6, which is now supported by essentially all current end sys-
tem platforms. At this writing, it is not possible to use IPv6 to exchange messages with a large
fraction of hosts on the Internet, but it is possible to assign an IPv6 address to many of them.
Although it is still too early to tell whether IPv6 will take over the world, it is not too early to
start writing applications to be prepared.
ix
x
Preface
Changes from the First Edition
We have updated and considerably expanded most of the material, having added two chapters.
Major changes from the first edition include:
IP version 6 coverage. We now include three kinds of code: IPv4-specific, IPv6-specific, and
generic. The code in the later chapters is designed to work with either protocol version
on dual-stack machines.
An additional chapter on socket programming in C++ (contributed by David B. Sturgill).
The PracticalSocket library provides wrappers for basic socket functionality. These allow
an instructor to teach socket programming to students without C programming back-
ground by giving them a library and then gradually peeling back the layers. Students
can start developing immediately after understanding addresses/ports and client/server.
Later they can be shown the details of socket programming by peeking inside the wrapper
code. Those teaching a subject that uses networking (e.g., OS) can use the library and only
selectively peel back the cover.
Enhanced coverage of data representation issues and strategies for organizing code that
sends and receives messages. In our instructional experience, we find that students have
less and less understanding of how data is actually stored in memory, 1 so we have
attempted to compensate with more discussion of this important issue. At the same
time, internationalization will only increase in importance, and thus we have included
basic coverage of wide characters and encodings.
Omission of the reference section. The descriptions of most of the functions that make
up the Sockets API have been collected into the early chapters. However, with so many
online sources of reference information—including “man pages”—available, we chose to
leave out the complete listing of the API in favor of more code illustrations.
Highlighting important but subtle facts and caveats. Typographical devices call out
important concepts and information that might otherwise be missed on first reading.
Although the scope of the book has expanded, we have not included everything that
we might have (or even that we were asked to include); examples of topics left for more
comprehensive texts (or the next edition) are raw sockets and programming with WinSock.
Intended Audience
We originally wrote this book so that we would have something to hand our students when we
wanted them to learn socket programming, so we would not have to take up valuable class time
1 We speculate that this is due to the widespread use of C++ and Java, which hide such details from the
programmer, in undergraduate curricula.
711346682.003.png
 
Preface xi
teaching it. In the years since the first edition, we have learned a good deal about the topics
that students need lots of help on, and those where they do not need as much handholding.
We also found that our book was appreciated at least as much by practitioners who were
looking for a gentle introduction to the subject. Therefore, this book is aimed simultaneously
at two general audiences: students in introductory courses in computer networks (graduate or
undergraduate) with a programming component, and practitioners who want to write their own
programs that communicate over the Internet. For students, it is intended as a supplement, not
as a primary text about networks. Although this second edition is significantly bigger in size
and scope than the first, we hope the book will still be considered a good value in that role.
For practitioners who just want to write some useful code, it should serve as a standalone
introduction —but readers in that category should be warned that this book will not make
them experts. Our philosophy of learning by doing has not changed, nor has our approach of
providing a concise tutorial sucient to get one started learning on one’s own, and leaving the
comprehensive details to other authors. For both audiences, our goal is to take you far enough
so that you can start experimenting and learning on your own.
Assumed Background
We assume basic programming skills and experience with C and UNIX. You are expected to be
conversant with C concepts such as pointers and type casting, and you should have a basic
understanding of the binary representation of data. Some of our examples are factored into
files that should be compiled separately; we assume that you can deal with that.
Here is a little test: If you can puzzle out what the following code fragment does, you
should have no problem with the code in this book:
typedef struct {
int a;
short s[2];
} MSG;
MSG *mp, m = {4, 1, 0};
char *fp, *tp;
mp = (MSG *) malloc(sizeof(MSG));
for (fp = (char *)m.s, tp = (char *)mp->s; tp < (char *)(mp+1);)
*tp++ = *fp++;
If you do not understand this fragment, do not despair (there is nothing quite so convo-
luted in our code), but you might want to refer to your favorite C programming book to find
out what is going on here.
You should also be familiar with the UNIX notions of process/address space, command-
line arguments, program termination, and regular file input and output. The material in
Chapters 4 and 6 assumes a somewhat more advanced grasp of UNIX. Some prior exposure to
networking concepts such as protocols, addresses, clients, and servers will be helpful.
 
Zgłoś jeśli naruszono regulamin