Unix Network Programming Manual
Opening a Socket
From a programming point of view, if you want to perform interprocess
communication, you first need to create a socket for that purpose. You do this
with the the socket operating system call.
s = socket (af, type, protocol)
int s, af, type, protocol
s is the socket value, which is simply an integer value, that
the operating system uses to identify this structure.
This socket id is used in further operations to
identify the socket structure allocated by the operating system. If s
is less than zero, then an error has occurred. Note that the since each
call to socket creates a unique communication path, a program can have
many sockets open simultaneously to other programs.
af is the address family. There are a number of possible address
families,
depending on the type of communication you intend for the socket. The
integer values for the families are defined in the include file
Some of the families are:
- AF_UNIX - For communicating between processes executing on
the same Unix machine.
- AF_INET - For communicating between processes executing
on machines which support the Internet protocols.
- AF_X.25 - For communicating between processes executing
on machines which support the X.25 protocols.
- AF_DECnet - For communicating between processes executing
on machines which support the DECNET protocols.
A single machine may support more than one set of protocols.
Also, you should notice that all of the address families mentioned can support
communication between processes on the same or different machines, except
the AF_UNIX family, which is restricted to processes executing on the same
host machine. The only exception is a form of Unix which distributes
activity over multiple hosts as though they were a signle machine.
The type parameter specifies the type of the socket, which determines
how the socket may be used to communicate. Technically, there
could be lots of different socket types, but in reality, there are only a few.
The two most important are:
- SOCK_STREAM - which provides a sequenced, reliable,
full{--}duplex byte stream for communication.
- SOCK_DGRAM - which provides connectionless, unreliable
messages with a fixed maximum length (which is usually small (e.g. 512 bytes).
These are standard types on machines which support the TCP/IP protocols, and
they are commonly used for the AF_UNIX address family. If you want reliable
communication without a lot of bother, you choose SOCK_STREAM, and if
reliability is not very important, but speed is, you might choose SOCK_DGRAM.
Note that these two types correspond closely with connection-oriented and
connectionless services. You should be aware that depending on the machine
you are using and the Unix version, some address families will only support
certain types of socket types.
The protocol argument specifies which protocol you want. Normally,
for a given socket family and socket type, there is only one protocol,
but the socket call was designed to provide lots of flexibility for the
future. For example, if you choose SOCK_STREAM, you can usually only choose
the TCP protocol for address family AF\_INET, and if you choose
SOCK_DGRAM you can only choose the Udp protocol for the same address family.
You can usually send zero for protocol and be
reasonably safe. If you are schmoozing around in the wilderness of
unknown protocols, you might look at the section 5 man pages for services
and protocols, which provides all the information in a nearly undecipherable
format.
Some typical socket calls are:
s = socket (AF_UNIX, SOCK_STREAM, 0)\\
which opens a socket for connection{--}oriented communication
between Unix processes.
s = socket (AF_INET, SOCK_DGRAM, 0) \\
which opens a socket to be used for connectionless datagram
communication between processes which may or may not
be on the same physical machine, but they must be on machines
which support the Internet protocols. Since protocol was
set to zero, the protocol will be UDP (Universal Datagram Protocol).
The example program uses:
sock = socket (AF_INET, SOCK_STREAM, 0)
if (sock == -1)
{ perror("opening socket");
exit(-1);
}
This creates a stream communication socket that uses the Internet protocols.
Note that the value of sock is checked to insure that it is a legitimate
value before proceeding.