Sockets
(Part I)
LESSON 21
(November 15, 1995)
Daniel Z. Tabor Jr.
New Jersey Institute of Technology
Outline:
Sockets (Part I)
The Socket Interface:
- The Berkeley socket interface provides generalized functions that support network communication using many
possible protocols.
- Socket calls refer to all TCP/IP protocols as a single protocol family (protocol suite).
- The calls allow a programmer to specify the type of service required, rather than the name of a specific protocol.
- The socket interface was created since an API (application program interface) for network connections is
not standardized, it’s design lies outside the scope of a protocol suite.
Windows Socket Interface
(WinSock):
- The WinSock specification was defined to provide a Berkeley Sockets (BSD) 4.3 API for TCP/IP on Microsoft
Windows.
- It, like the true BSD socket interface, provides APIs to resolve address and service differences.
- The WinSock specification also provides a set of asynchronous extensions that allow applications to send
and receive message-based notices (arrival of new data).
- Currently it is in version 2.0 and provides communication connections (multiple protocol stacks) for many
hardware and software technologies.
- Some hardware and software technologies include:
- IPX/SPX
- OSI protocols (X.500)
- DNS
- ATM
- ISDN
- Wireless connections (CDPD)
Basic I/O Functions in UNIX:
- UNIX and other operating systems provide a basic set of system functions used for I/O operations on files and other
devices.
- Most operating systems provide similar variations to the five standard I/O operations that BSD UNIX uses.
- I/O Functions:
- Open - prepare for input / output.
- Close - terminate the use of a device.
- Write - transfer data from memory to an output device.
- Read - transfer data from an input device to memory.
- Lseek - position the head of a disk drive to a specific place on the disk.
File and Socket Descriptors:
- A socket is a generalized UNIX file access mechanism that provides an endpoint for communication.
- Descriptors (maintained in the descriptor tables) are kept per process by the operating system to point to
internal data structures for files and sockets.
- Descriptors are small integer values.
- File Descriptor:
- Bound to a file when open is called.
- Socket Descriptor:
- Created using open, but does not bind it to a destination.
- Unbounded - UDP specifies destination every time.
- Bounded - TCP specifies destination during an open system call.
- After a socket has been created (using open), additional system calls are required to specify the details of it’s use.
- Passive Socket - used by a server to wait for calls.
- Active Socket - used by a client to initiate a connection.
Communication Endpoints:
- TCP/IP protocols define communication endpoints which consist of:
- An IP address
- A protocol port number
.
- Sockets support all possible endpoint types and address formats used by families of protocols.
- The socket abstraction defines an address family for each type address (allowing many for each protocol family).
- PF_INET (protocol family) - TCP/IP
- AF_INET (address family) - TCP/IP
- AF_PUP - Xerox Corp. PUP
- AF_APPLETALK - Apple Corp.
- AF_UNIX - General UNIX file system.
Generic Address Structure:
- The generalized format that all endpoints use is:
(address family, endpoint address in that family)
- Address families - can be one of the previously mentioned AF types, or others.
- Endpoint addresses - are stored in predefined data structures defined by the address family.
- Sockaddr structure (sa)
(2-byte sa_family, 14-byte address):
- Should only be used as an overlay with code only referencing the sa_family field.
TCP/IP Sockaddr_in Structure:
- When representing a TCP/IP communication endpoint, an application uses the sockaddr_in structure.
- Based directly on the sockaddr structure, it contains:
- An Address Family (8-bits)
- An IP Address (32-bits)
- A protocol port number (8-bits).
Socket System Calls:
Socket
- Socket (af, type, protocol):
- Creates a socket on demand (placing it in an unconnected state), returns an integer identifying the socket
(descriptor), and specifies:
- Address Family (af) - particular address of the family.
- Type - Type of communication socket:
- SOCK_STREAM - connection-oriented
- SOCK_DGRAM - connection-less
- SOCK_RAW - access to low-level protocols or network interfaces.
- Protocol - Accommodates multiple protocols within a family.
Socket System Calls:
Socketpair
- Socketpair (af, type, protocol, sarray):
- Not meaningful within TCP/IP, it is designed to implement an UNIX pipe.
- The calling process creates both endpoints for the communication simultaneously using the sockets
specified in the socket array parameter (sarray).
Socket System Calls:
Close
- Close (socket descriptor):
- When a client or server finishes with a socket, calls close to deallocate it’s resources.
- The connection immediately terminates unless several processes share the same socket.
- It then decrements the reference count (closing it completely when reference count = 0).
Socket System Calls:
Bind
- Bind (socket, localaddr, addrlen):
- Socket is created without any association to local or destination addresses, so a program uses bind to
establish a local address for it.
- Socket - integer descriptor of the socket.
- Localaddr - structure that specifies the local address to be bound.
- Addrlen - integer length of the address (in bytes).
Socket System Calls:
Connect
- Connect (socket, destaddr, addrlen):
- Binds a permanent destination to a socket placing it in a connected state.
- Sockets using connection-less service do not have to use connect (specify the address in every datagram), but
may.
- Socket - socket descriptor.
- Destaddr - socket_addr structure (also includes protocol port number) specifying the destination address.
- Addrlen - length of destination address (in bytes).
Socket System Calls:
Shutdown
- Shutdown (socket, direction):
- The shutdown function applies to full-duplex sockets (connected using a TCP socket) and is used to
partially close the connection.
- Socket - socket descriptor of a connected socket.
- Direction - direction in which shutdown is desired
- 0 = terminate further input.
- 1 = terminate further output.
- 2 = terminate input / output (close).
Output Socket Calls:
Write
- Write (socket, buffer, length):
- Uses a connected socket to transmit data over the connection. Write blocks until data is transferred, then
send an error code notifying the application if the write succeeded.
- Socket - descriptor, can also be a file descriptor.
- Buffer - address of the data to be sent.
- Length - number of bytes to be sent.
Output Socket Calls:
Requiring a Connected Socket
- Writev (socket, iovector, vectorlen):
- Performs a ‘gather-write’ using the iovector structure.
- Send (socket, message, length, flags):
- Uses flags for out-of-band signaling, debugging, etc.
Output Socket Calls:
Not Requiring a Socket Connection
- Sendto (socket, message, length, flags, destaddr, addrlen):
- Functions the same as an unconnected Send.
- Sendmsg (socket, messagestruct, flags):
- Places parameters in the message-structure to be sent.
Input Socket Calls:
Read
- Read (descriptor, buffer, length):
- Used to receive (read) data from an established connection (connected socket).
- Descriptor - IP of socket or file from which to read.
- Buffer - address in memory of where to store read data.
- Length - maximum number of bytes to read.
Input Socket Calls:
Requiring a Connected Socket
- Readv (descriptor, iovector, vectorlen):
- Performs a ‘gather-read’ of non-contiguous locations, using the iovector structure.
- Recv (socket, message, length, flags):
- Flags allow for look-ahead reading from sockets, etc.
Input Socket Calls:
Not Requiring a Socket Connection
- Recvfrom (socket, buffer, length, flags, fromaddr, addrlen):
- Unconnected read, same as Recv.
- Recvmsg (socket, messagestruct, flags):
- Message parameters are received through the message-structure.
Server-Specific Socket Calls:
Listen
- Listen (socket, qlength):
- Server creates a socket, binds it to a well-known port, and waits for requests.
- To avoid rejecting service requests that cannot be handled, a server queue is created using Listen.
- It provides a mechanism to create the queue and then listen for incoming connections (passive mode).
- Listen only works with sockets using a reliable stream service.
- Socket - Integer descriptor.
- Qlength - length of the request queue for that socket (max. = 5)
Server-Specific Socket Calls:
Accept
- Accept (socket, addr, addrlen):
- Bind associates a socket with port, but that socket is not connected to a foreign destination.
- When a request comes in, Accept establishes the full connection.
- It blocks until a connection request arrives.
- Addr - pointer to the sockaddr structure.
- Addrlen - pointer to integer size of address.
Server-Specific Socket Calls:
Fork
- Fork is not directly related to communications sockets, but it is essential because servers use it to
create and handle concurrent connections for the same service.
- Fork ()
- It does not take any arguments, but if successful,
returns the new child process identifier to the parent (server) process.
Order of Socket System Calls:
Client Side
- Client Side (depends on connection type):
- Socket
- Connect
- Write (may be repeated)
- Read (may be repeated)
- Close
Order of Socket System Calls:
Server Side
- Server Side (depends on connection type):
- Socket
- Bind
- Listen
- Accept
- Read (may be repeated)
- Write (may be repeated)
- Close (go back to Accept)
Last Modification: (Sunday, August 25, 1996)
All work was written, produced, and is copyrighted by Daniel Z. Tabor Jr.
Page created by Daniel Z. Tabor Jr.
Copyright ©1996 Illusion Industries Inc.
