Socket Creation

The basis for communication between processes centers on the socket mechanism. A socket is comparable to an OS/2 file handle. Application programs request the operating system to create a socket through the use of the socket() call. When an application program requests the creation of a new socket, the operating system returns an integer that the application program uses to reference the newly created socket.

To create a socket with the socket() call, the application program must include a protocol family and a socket type. It can also include a specific communication protocol within the specified protocol family.

An example of an application using the socket() call is:
An Application Using the socket() Call

int s;
...
s = socket(PF_INET, SOCK_STREAM, 0);

In this example, the socket() call allocates a socket descriptor s in the internet protocol family (PF_INET). The type parameter is a constant that specifies the type of socket. For the internet communication domain, this can be SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW. The protocol parameter is a constant that specifies which protocol to use. Passing 0 chooses the default protocol for the specified socket type. Supported Protocol Families includes information on default protocols. If successful, socket() returns a non-negative integer socket descriptor.

See Socket Connections for more about creating sockets.


[Back: Guidelines for Using Socket Types]
[Next: Binding Names to Sockets]