Linux Socket Programming By Example.pdf

(2819 KB) Pobierz
Linux Socket Programming By Example:Table of Contents
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
Zgłoś jeśli naruszono regulamin