socket

Creates an endpoint for communication and returns a descriptor.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <sys/socket.h> 
   
  int  socket (
     int   domain,
     int   type,
     int   protocol);
  

Parameters

domain

(IN) Specifies the address family used in the communications domain. Use one of the following flags:

Flag

Value

Description

PF_INET

2

Internetwork protocol: UDP, TCP, etc.

PF_IPX

6

IPX protocols: IPX, SPX, etc.

PF_INET6

28

Internetwork protocol, Version 6

type

(IN) Specifies the type of communication socket to be used and implies a protocol. See Socket Types.

protocol

(IN) Specifies a particular protocol to be used with the socket. If you specify 0, socket uses a default protocol appropriate for the requested socket type.

Return Values

If successful, returns a socket handle. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

41

EPROTOTYPE

The specified protocol type does not match its actual type.

43

EPROTONOSUPPORT

The protocol type or the specified protocol is not supported within this domain.

55

ENOBUFS

The socket cannot be created until sufficient resources are freed.

Remarks

The socket function creates and initializes socket resources in the TCP NLMâ„¢ application. It also opens and associates a NetWare file handle with the allocated socket resources.

Associating internal socket resources with NetWare file handles grants sockets the same operating system services given to other devices, including the automatic closure of a device when a program terminates abnormally.

In NetWare, socket returns an integer that is actually the address of a file control block (unlike typical BSD interfaces where socket returns an integer that is an index into a per-process file table). This distinction is not important to a client program unless the program is dependent on the range or value of a socket handle. All 32 bits are significant, and a valid handle can be a positive or negative value (other than -1).

The socket function supports the PF_INET family (which includes TCP and UDP), the PF_INET6 (which includes TCP and UDP), and the PF_IPX family (which includes TCP). The AF_INET, AF_INET6, and AF_IPX address families are used for the addresses supplied in later operations on the socket.

Within PF_INET and PF_INET6, the SOCK_STREAM type uses TCP, and the SOCK_DGRAM type uses UDP, as follows:

  • TCP provides sequenced, reliable, two-way connection-based byte streams.

  • UDP supports datagrams (connectionless, unreliable messages shorter than 65,536 bytes).

The PF_INET, PF_INET6, and PF_IPX families supports one protocol per type so you can specify 0 for the protocol parameter. For completeness, the supported values are defined in NETINET/IN.H. The protocol values for TCP and UDP are as follows:

IPPROTO_TCP

TCP

IPPROTO_UDP

UDP

The protocol value for TCP is as follows:

IPXPROTO_TCP

TCP

A TCP socket must be in a connected state before it can receive or send any data. connect creates another socket. Out-of-band urgent data can also be transmitted (as described in send) and received (as described in recv).

TCP ensures that data is not lost or duplicated. If the peer socket does not acknowledge a transmission attempt within a reasonable length of time, the connection is considered broken and the function returns ETIMEDOUT in errno. TCP optionally keeps sockets active by forcing transmissions periodically in the absence of other activity.

SOCK_DGRAM sockets allow transmission of datagrams to correspondents named in sendto. The recvfrom function receives datagrams and returns the next datagram with its return address.

Socket-level options affect the operation of sockets and are defined in SYS/SOCKET.H. You can call setsockopt and getsockopt to set and get options.

See Also