socket()

Creates a socket, an endpoint for communication, and returns a socket descriptor for the socket. This must be the first BSD IPC call used in the process. Both server and client processes need to create a socket with the socket() call.

Syntax

socket = socket(af, type, protocol)

int socket, af, type protocol;

Parameters

socket

Socket descriptor for the newlycreated socket. It is an integer

 

with a valid range of 0 to 30.

 

This socket descriptor is used in subsequent BSD IPC calls to

 

reference this socket.

 

If the call fails, a -1is returned in socket and the global

 

variable errno contains the error code.

af

Address family for the socket being created. It must be set to

 

AF_INET, for Internet address family.

 

The address family defines the address format used in socket

 

operations. The AF_INET address family uses an address

 

structure (sockaddr_in) of 16 bytes. Refer to •Address

 

Family Type" in Appendix D of the BSD IPC Reference Manual

 

for NSARPA/1000 and ARPA/1000 for more information.

type

Type of socket being created. It must be set to SOCK_STREAM.

 

The socket type specifies the semantics of communication for

 

the socket. A SOCK_STREAM type provides sequenced,

 

reliable, twoway, connectionbased bytes streams. Refer to

 

•Socket Type" in Appendix D of the BSD IPC Reference Manual

 

for NSARPA/1000 and ARPA/1000 for more information.

protocol

Underlying protocol to be used. 0 causes the system to choose

 

a protocol type to use. The default is tcp.

420 Berkeley Software Distribution Interprocess Communication