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
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
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
Stream sockets are supported by the internet (PF_INET) communication domain and local IPC (PF_OS2, PF_UNIX, or PF_LOCAL).
Datagram sockets are supported by the internet (PF_INET), local IPC (PF_OS2, PF_UNIX, or PF_LOCAL), and NetBIOS (PF_NETBIOS or PF_NB) communication domains.
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
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
accept()
accept_and_recv()
bind()
connect()
getsockname()
getsockopt()
ioctl()
listen()
os2_ioctl()
os2_select()
readv()
recv()
recvfrom()
recvmsg()
select()
send()
sendmsg()
sendto()
setsockopt()
shutdown()
sock_errno()
soclose()
writev()