Chapter Contents

Previous

Next
The BSD UNIX Socket Library

Socket Functions

The following scenario depicts a typical sequence of events in a network application using stream sockets:

  1. A server application issues a socket call, which returns an integer that is the socket descriptor, allocated to the AF_INET addressing family.

  2. The server uses the bind function to bind a name to the socket and allow the network access to the socket.

  3. The server issues the listen call to signal the network that it is ready to accept connections from other applications.

  4. The server issues an accept call to accept the connection request.

  5. A client application issues a connect call to the server.

  6. The client and server conduct read/write operations.

  7. The socket is disconnected with a close call.

Datagram sockets do not have to be bound or connected, but they normally need to acquire additional address information in order to communicate with each other.

The following sections describe the socket functions that gather information, convert data, and accompany each step in the process of communicating between applications over a network. Refer to Socket Function Reference for a complete description of each socket function.


Addresses and Network Information

The complexity of network communications frequently requires that an application have some means of determining the location of sockets or other applications and of acquiring information about the network.

Socket Address

A process may need to discover the address to which the socket is connected. The process may also need to discover its socket's local address. The following function calls determine socket addresses:


Host Name

The following function calls return the host's assigned name and the host's Internet address:


Address Manipulation

The following socket functions perform a translation between 32-bit IP addresses and the standard network dotted decimal notation, or they divide or reassemble the network and host sections of 32-bit IP addresses:


Host Information

An application can obtain information about a host by submitting either the host's name or its address. The following functions return the host name and IP address:


Network Information

An application can also obtain information about a network. The following function calls return network names and addresses:


Protocol Information

An application can also obtain information about protocols. The following function calls return protocol names and numbers:


Network Services

An application can obtain information about network services and their protocol ports. The following functions return network service names and ports:


Database Services

There are three sets of library routines that access a database to return information such as the names of machines and network services, and protocol port numbers. Generally speaking, these routines are of use only to applications, such as network management programs, that are intended to read the entire database.

With each set, an application can connect to a database, return a database entry, and disconnect. The pattern for the names of these routines is as follows:

X is the name of the database.

The following function calls perform database services:


Resolver Routines

These routines make, send, and interpret data packets for use with the Internet Domain Name Service. The resolver consists of the following socket functions:

The following macros and functions place long and short integers in a buffer in the format expected by the Internet name server:


Data Conversion

Address or network information may need to be translated, and character sets received through a socket may need to be converted.

Byte Order Conversion

When an integer is copied from a network packet (unit of data) to a local machine or from a local machine to a network packet, the byte order must be converted between local machine byte order and network standard byte order. The following socket functions take a value as an argument and return that value with the bytes rearranged:


ASCII-EBCDIC Translation

The following functions translate between the ASCII character set generally used for text on the Internet, and the OS/390 or CMS EBCDIC character set:


Choosing a Socket Implementation

The SAS/C Socket Library supports both integrated and non-integrated sockets. Integrated sockets are available with USS and provide a higher degree of UNIX compatiblity. Non-integrated sockets rely on a run-time library implemented interface to TCP/IP software. The socket implementation is specified by the following function:

Note:    The socket implementation can also be specified at run time by setting the _SASC_SOCKIMP environment variable.  [cautionend]


Creating a Socket

In order to create a socket, the application issues a socket call, specifying the domain and family to which the socket belongs. The program that creates the socket can also control factors such as length of timeout, type of data transmitted, and size of the communication buffer. Finally, the program can set operating characteristics of the socket such as whether an application can continue processing without becoming blocked during a recv call.

The following socket functions create a socket or control its operating characteristics:


Binding a Socket

A socket is created without associating it to any address. A local address can be bound to the socket with the following socket function:


Listening for a Connection

When an application in the role of server has opened a stream socket, it binds the socket to a local address and then waits for a connection request from another application. To avoid confusion in message delivery, the application can set up a queue for the incoming connection requests. The following socket function prepares the server to await incoming connections:


Connecting and Passing a Socket

Connecting a socket binds it to a permanent destination. If an application seeks to transmit data to a stream socket, it must first establish a connection to that socket. A server program can also pass sockets between two client programs. The following socket functions establish or pass connections between applications:


Accepting a Connection

After a server has established a socket, it waits for a connection. The following socket function causes the application to wait for a connect request:

The following socket function accepts the next connection on a socket and also receives the first block of data:


Sending and Receiving Data Through a Socket

After a socket has been established, an application can transmit or receive data through the socket. The following socket functions are used in the transmission or receipt of data between sockets:


Closing the Connection

An application closes a connection when it has no more data to send. An application can also shut down transmissions in one or both directions. The following functions close the connection or shut down transmissions:


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.