socket()

The socket call creates an endpoint for communication and returns a socket descriptor representing the endpoint.

Syntax

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

Parameters

domain

type protocol

Description

This call creates an endpoint for communication and returns a socket descriptor representing the endpoint. Each socket type provides a different communication service.

Sockets are deallocated with the soclose() call.

The domain parameter specifies a communications domain where communication is to take place. This parameter specifies the protocol family which is used.

Protocol Family

PF _ OS2orPF _ UNIX PF _ INET PF _ NETBIOSorPF _ NB PF _ ROUTE

The type parameter specifies the type of socket created. These socket type constants are defined in the <SYS\SOCKET.H> header file. See Socket Types for additional details. The types supported are:

Type

SOCK _ STREAM SOCK_DGRAM SOCK_RAW SOCK_SEQPACKET

The protocol parameter specifies a particular protocol to be used with the socket. If the protocol field is set to 0 (default), the system selects the default protocol number for the domain and socket type requested. Default and valid protocol number-protocol family combinations are in the section Socket Protocol Families. The getprotobyname() call can be used to get the protocol number for a protocol with a well-known name.

Return Values

A non-negative socket descriptor return value indicates success. The return value -1 indicates an error. You can get the specific error code by calling sock_errno() or psock_errno().

sock_errno() Value

SOCEMFILE SOCEPROTONOSUPPORT SOCEPFNOSUPPORT SOCESOCKTNOSUPPORT

Examples

Following are examples of the socket() call.

int s;struct protoent *p;
struct protoent *getprotobyname(char *name);
int socket(int domain, int type, int protocol); /* extracted from sys/socket.h */
...
/* Get stream socket in internet domain with default protocol */
s = socket(PF_INET, SOCK_STREAM, 0);
...
/* Get raw socket in internet domain for ICMP protocol */
p = getprotobyname("icmp");
s = socket(PF_INET, SOCK_RAW, p->p_proto);

Related Calls


[Back: sock_errno()]
[Next: soclose()]