linux socket programming by example

557 414 1
linux socket programming by example

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Introduction About the Author Part 1—Basic Socket Concepts Chapter 1—Introducing Sockets A Brief Historical Introduction Understanding Sockets Defining a Socket Using Sockets Referencing Sockets Comparing Sockets to Pipes Creating Sockets Using socketpair(2) in an Example Running the Demonstration Program Performing I/O on Sockets Closing Sockets The shutdown(2) Function Shutting Down Writing to a Socket Dealing with Duplicated Sockets Shutting Down Reading from a Socket Knowing When Not to Use shutdown(2) Writing a Client/Server Example Chapter 2—Domains and Address Families Nameless Sockets Anonymous Calls Generating Addresses Understanding Domains Forming Socket Addresses Examining the Generic Socket Address Forming Local Addresses Forming Traditional Local Addresses Forming Abstract Local Addresses Forming Internet (IPv4) Socket Addresses Understanding Network Byte Order Performing Endian Conversions Initializing a Wild Internet Address Initializing a Specific Internet Address Specifying an X.25 Address Specifying Other Address Families The AF_UNSPEC Address Family What’s Next Chapter 3—Address Conversion Functions Internet IP Numbers Internet Address Classes Understanding Netmask Values Allocating IP Addresses Private IP Numbers Reserved IP Numbers Manipulating IP Numbers Using the inet_addr(3) Function The inet_aton(3) Function Using the inet_ntoa(3) Function Using inet_network(3) Using the inet_lnaof(3) Function Using the inet_netof(3) Function Using the inet_makeaddr(3) Function What’s Next Chapter 4—Socket Types and Protocols Specifying the Domain of a Socket Choosing PF_INET or AF_INET Using the PF_LOCAL and AF_LOCAL Macros Using the socket(2) Function Choosing a Socket Type Understanding the SOCK_STREAM Socket Type Understanding the SOCK_DGRAM Socket Type Understanding the SOCK_SEQPACKET Socket Type Choosing a Protocol Using PF_LOCAL and SOCK_STREAM Using PF_LOCAL and SOCK_DGRAM Using PF_INET and SOCK_STREAM Using PF_INET and SOCK_DGRAM Socket Domain and Type Summary Other Linux-Supported Protocols Researching Other Protocols Chapter 5—Binding Addresses to a Socket The Purpose of the bind(2) Function Using the bind(2) Function Obtaining the Socket Address Writing a sock_addr() Function Obtaining a Peer Socket Address Interfaces and Addressing Specifying an Interface Address Example Binding a Specific Interface Address Binding for Any Interface Chapter 6—Connectionless-Oriented Protocols The Methods of Communication Understanding the Advantages Understanding the Disadvantages of Connectionless Communications Performing Input/Output of Datagrams Introducing the sendto(2) Function Introducing the recvfrom(2) Function Writing a UDP Datagram Server Writing a UDP Datagram Client Testing the Datagram Client and Server Testing with No Server Testing with Other IP Numbers Leaving Out bind(2) in Client Programs Replying to a Wild Address Chapter 7—Connection-Oriented Protocols for Clients Reviewing the Methods of Communication TCP/IP Handles Lost Packets TCP/IP Handles Duplicated Packets TCP/IP Handles Sequencing TCP/IP Handles Flow Control Understanding the Advantages of TCP/IP Internet Services Examining the /etc/services File Using Function getservent(3) Using the setservent(3) Function Using the endservent(3) Function Looking Up a Service by Name and Protocol Looking Up a Service by Port and Protocol Consulting the /etc/protocols File Using the setprotoent(3) Function Using the endprotoent(3) Function Looking Up a Protocol by Name Looking Up a Protocol by Number Writing a TCP/IP Client Program Introducing the connect(2) Function Preparing to Write the Client Program The daytime Client Program Using connect(2) on SOCK_DGRAM Sockets Chapter 8—Connection-Oriented Protocols for Servers Understanding the Role of the Server The listen(2) Function Understanding the Connect Queue Specifying a Value for backlog The accept(2) Function Call Understanding the Role of accept(2) Writing a TCP/IP Server Running a Wild Server Modifying the Client Program Chapter 9—Hostname and Network Name Lookups Understanding the Need for Names Using the uname(2) Function Obtaining Hostnames and Domain Names Using Function gethostname(2) Using the getdomainname(2) Function Testing gethostname(2) and getdomainname(2) Resolving Remote Addresses Error Reporting Reporting an h_errno Error Using the gethostbyname(3) Function Applying the gethostbyname(3) Function The gethostbyaddr(3) Function Using the sethostent(3) Function Using the endhostent(3) Function Part 2—Advanced Socket Programming Chapter 10—Using Standard I/O on Sockets Understanding the Need for Standard I/O Associating a Socket with a Stream Using fdopen(3) to Associate a Socket with a Stream Closing a Socket Stream Using Separate Read and Write Streams Duplicating a Socket Closing the Dual Streams Winding Up Communications Shutting Down the Write Side Only Shutting Down the Read Side Only Shutting Down Both Read and Write Sides Handling Interrupts Handling EINTR for Other Functions Defining Buffer Operation Applying FILE Streams to Sockets Presenting the mkaddr() Function The RPN Calculator Engine Code Trying Out the RPN Server Chapter 11—Concurrent Client Servers Understanding the Multiple-Client Problem Overview of Server Functions Using fork(2) to Service Multiple Clients Understanding the Overall Server Process Understanding the Child Server Process Flow Understanding Process Termination Processing Designing Servers That Use select(2) Introducing the select(2) Function Manipulating File Descriptor Sets Applying select(2) to a Server Testing the select(2)-Based Server Limitations of the Example Chapter 12—Socket Options Getting Socket Options Applying getsockopt(2) Setting Socket Options Applying the setsockopt(2) Function Retrieving the Socket Type (SO_TYPE) Setting the SO_REUSEADDR Option Setting the SO_LINGER Option Setting the SO_KEEPALIVE Option Setting the SO_BROADCAST Option Setting the SO_OOBINLINE Option Options SO_PASSCRED and SO_PEERCRED Chapter 13—Broadcasting with UDP Understanding Broadcast Addresses Broadcasting on 255.255.255.255 Enhancing the mkaddr.c Subroutine Broadcasting from a Server Receiving Broadcasts Demonstrating the Broadcasts Broadcasting to a Network Starting Broadcasts Receiving Broadcasts Receiving Broadcasts from a Remote Host Troubleshooting Hints Chapter 14—Out-of-Band Data Defining Out-of-Band Understanding the Need for Out-of-Band Data Sockets and Out-of-Band Data Variations in Implementation Using Out-of-Band Data Writing Out-of-Band Data Reading Out-of-Band Data Understanding the Signal SIGURG Supporting Subprograms Receiving with the SIGURG Signal Sending Out-of-Band Data Testing the oobrecv and oobsend Programs Understanding the Urgent Pointer Understanding TCP Urgent Mode Urgent Mode When tcp_stdurg=1 Receiving Out-of-Band Data Inline Determining the Urgent Pointer Using Out-of-Band Data Inline Limitations of the Urgent Mode Pointer Processing Out-of-Band Data with select(2) Chapter 15—Using the inetd Daemon Steps Common to Most Servers Introducing inetd The /etc/inetd.conf Configuration File The Design Parameters of inetd Servers Implementing a Simple stream tcp Server Configuring /etc/inetd.conf to Invoke a New Server Disabling the New Service Datagram Servers with inetd Understanding wait and nowait Chapter 16—Network Security Programming Defining Security The Challenges of Security Identifying Friend or Foe Securing by Hostname or Domain Name Identifying by IP Number Securing inetd Servers Centralized Network Policy Understanding the TCP Wrapper Concept Determining Access Installing Wrapper and Server Programs Examining Server and Wrapper Logging Code Examining the Datagram Server Code Examining the Simple TCP Wrapper Program Introducing the Client Program Installing and Testing the Wrapper Monitoring the Log Files Starting Your inetd Daemon Testing the Wrapper Program Testing the Server Timeout Uninstalling the Demonstration Programs Datagram Vulnerability Chapter 17—Passing Credentials and File Descriptors Problem Statement Introducing Ancillary Data Introducing I/O Vectors The I/O Vector (struct iovec) The readv(2) and writev(2) Functions The sendmsg(2) and recvmsg(2) Functions The sendmsg(2) Function The recvmsg(2) Function Understanding struct msghdr Ancillary Data Structures and Macros Introducing the struct cmsghdr Structure Introducing the cmsg(3) Macros Iterating Through Ancillary Data Creating Ancillary Data Presenting an Ancillary Data Example The Common Header File common.h The misc.c Module The recvcred.c Module The Simple Web Server web80 The reqport() Function The recv_fd() Function The sockserv Server Program The send_fd() Function Testing the Socket Server Testing sockserv Chapter 18—A Practical Network Project Problem Statement Solving the Quote Service Problem Obtaining Stock Market Quotes Examining the Quote Server Program Fetching Quotations via get_tickinfo() Broadcasting Quotes via broadcast() Examining the Client Program Compiling and Running the Demonstration Starting the qserve Quotation Server Starting the mktwatch Client If the finance.yahoo.com Service Changes Appendix A Appendix B Appendix C Glossary Index Copyright © Macmillan Computer Publishing, Inc. Introduction There have been many books written on the topic of computer networking. While many of these are excellent resources for advanced programmers, they tend to be too deep for the beginner who just wants to know how to use it. Why require a potential driver to understand the theory behind his automobile? This book teaches the reader how to use socket programming, as if networking was an appliance that you can turn on and use. Consequently, a “by example” approach to socket programming is used here. Each chapter builds upon the previous, until all of the basic concepts are mastered in Part 1, “Basic Socket Concepts.” Part 2, “Advanced Socket Programming,” contains some more advanced topics that might present a challenge for some readers. The last chapter presents a practical application tying together many of the concepts you’ve learned. The by Example Series How does the by Example series make you a better programmer? The by Example series teaches programming using the best method possible—examples. The text acts as a mentor, looking over your shoulder, providing example programs, and showing you new ways to use the concepts covered in each chapter. While the material is still fresh, you will see example after example, demonstrating ways to use what you just learned. The philosophy of the by Example series is simple: The best way to teach computer programming is with multiple examples. Command descriptions, format syntax, and language references are not enough to teach a newcomer a programming language. Only by taking the components, immediately putting them into use, and running example programs can programming students get more than just a feel for the language. Newcomers who learn only a few basics using examples at every step of the way will automatically know how to write programs using those skills. Who Should Use This Book This book should be read by anyone wanting to know how to perform network programming on Linux or UNIX platforms. The example programs have been tailored specifically for Linux, in order to provide for the best educational experience to the reader. The best success with the example programs will occur with Red Hat 6.0 or comparable Linux distribution releases. Older releases of Linux might present some special challenges because the netstat(1) command has been through a lot of change in recent times. Older Linux distributions should also have the /proc file system enabled in order to take full advantage of some of the example programs. Conventions Used in This Book This book uses several common conventions to help teach Linux socket programming. Here is a summary of those typographical conventions. Examples are indicated by the icon shown at the left of this sentence. Code associated with this book will be available at the Que Web site for download (http://www.quecorp.com/series/by_example/). You’ll find the icon shown to the left of this sentence to indicate output associated with the code listings. Some of the typographical conventions used include the following: • Commands and computer output appear in a special monospaced computer font. • Words you type appear in a boldfaced computer font. • Any lines of code that are too long to fit on a single line will be broken into two lines, and a code continuation character, Ä, will appear on the second line. In addition to typographical conventions, the following special elements are included to set off different types of information to make them easily recognizable: NOTE: Special notes augment the material you read in each chapter. These notes clarify concepts and procedures. TIP: You’ll find numerous tips offering shortcuts and solutions to common problems. CAUTION: The cautions warn you about roadblocks that sometimes appear when programming for Linux. Reading the caution sections should save you time and trouble, not to mention a few headaches. [...]... Code Please visit the following by Example Web site for example code or additional material associated with this book: http://www.quecorp.com/series /by_ example/ What’s Next The socket API is not the only way that networking programs can be written It is, however, the most popular interface due to its elegant simplicity If you know a little about the C language and Linux programming and you have an appetite... Teach Yourself Linux Programming in 24 Hours Warren has been programming professionally since 1980, using many assembler languages, PL/I, C, and C++ He has been programming for UNIX since 1986 and started programming for Linux in 1994 Linux has allowed him to contribute software packages, such as the ftpbackup program and the rewrite of the popular wavplay program These and his other Linux packages... little-endian byte ordering Learn what an abstract local address is and how to form one Learn when socket addresses are not required This chapter is very important to you because many programmers struggle with this very aspect of socket programming A little extra effort spent here will reward you later Nameless Sockets Sockets do not always need to have an address The socketpair(2) function, for example, ... released by UCB that included a TCP/IP network implementation The network socket concepts and interfaces that you will learn about in this book are based upon the work done by UCB Linux draws upon this rich heritage, and so you’ll learn about the Linux specific implementation of the BSD socket interface in this book Figure 1.1 is provided as a time line overview of the history behind the socket interface... the remote socket, your program can establish a line of communication between your local socket and that remote endpoint Socket addresses are discussed in Chapter 2, “Domains and Address Families.” You can conclude then, that a socket is merely an endpoint in communication There are a number of Linux function calls that operate on sockets, and you learn about all of them in this book Using Sockets You... opened by calling open(2) 6 File unit 5 is returned to reference the newly opened file Notice how the Linux kernel makes no distinction between files and sockets when allocating unit numbers A file descriptor is used to refer to an opened file or a network socket This means that you, as a programmer, will use sockets as if they were open files Being able to reference files and sockets interchangeably by. .. function socketpair(2) synopsis is as follows: #include #include int socketpair(int domain, int type, int protocol, int sv[2]); The include file sys/types.h is required to define some C macro constants The include file sys /socket. h is necessary to define the socketpair(2) function prototype The socketpair(2) function takes four arguments They are • • • • The domain of the socket. .. Example of socketpair(2) function: */ #include #include #include #include #include #include #include int main(int argc,char **argv) { int z; /* Status return code */ int s[2]; /* Pair of sockets */ /* * Create a pair of local sockets: */ z = socketpair(AF_LOCAL,SOCK_STREAM,0,s); if ( z == -1 ) { fprintf(stderr, “%s: socketpair(AF_LOCAL,SOCK_STREAM,0)\n”,... int d; /* Existing socket */ /* Duplicated socket */ d = dup(s); /* duplicate this socket */ close(s); /* nothing happens yet */ close(d); /* last close, so shutdown socket */ In the example, the first close(2) call would have no effect It would make no difference which socket was closed first Closing either s or d first would still leave one outstanding file descriptor for the same socket Only when... surviving file descriptor for that socket would a close(2) call have any effect In the example, the close of the d file descriptor closes down the socket The shutdown(2) function avoids this difficulty Repeating the example code, the problem is solved using the shutdown(2) function: int s; int d; /* Existing socket */ /* Duplicated socket */ d = dup(s); /* duplicate this socket */ shutdown(s,SHUT_RDWR); . you’ve learned. The by Example Series How does the by Example series make you a better programmer? The by Example series teaches programming using the best method possible—examples. The text. Part 1—Basic Socket Concepts Chapter 1—Introducing Sockets A Brief Historical Introduction Understanding Sockets Defining a Socket Using Sockets Referencing Sockets Comparing Sockets to. brief history of how sockets were developed • The essence of sockets • How sockets are referenced by the Linux kernel and application programs • An introductory example of a socket C program A

Ngày đăng: 28/04/2014, 16:53

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan