NetSock_Open

Create a datagram (i.e., UDP) or stream (i.e., TCP) type socket.

Files

net_sock.h/net_sock.c
net_bsd.h/net_bsd.c

Prototypes

NET_SOCK_ID NetSock_Open(NET_SOCK_PROTOCOL_FAMILY  protocol_family,
                         NET_SOCK_TYPE             sock_type,
                         NET_SOCK_PROTOCOL         protocol,
                         NET_ERR                  *p_err);

int         socket      (int                       protocol_family,
                                                   int sock_type,
                                                   int protocol);

Arguments

protocol_family

This field establishes the socket protocol family domain. Always use NET_SOCK_FAMILY_IP_V4/PF_INET or NET_SOCK_FAMILY_IP_V6/PF_INET6 for TCP/IP sockets.

sock_type

Socket type:

NET_SOCK_TYPE_DATAGRAM/PF_DGRAM for datagram sockets (i.e., UDP)

NET_SOCK_TYPE_STREAM/PF_STREAM for stream sockets (i.e., TCP)

NET_SOCK_TYPE_DATAGRAM sockets preserve message boundaries. Applications that exchange single request and response messages are examples of datagram communication.

NET_SOCK_TYPE_STREAM sockets provides a reliable byte-stream connection, where bytes are received from the remote application in the same order as they were sent. File transfer and terminal emulation are examples of applications that require this type of protocol.

protocol

Socket protocol:

NET_SOCK_PROTOCOL_UDP/IPPROTO_UDP for UDP

NET_SOCK_PROTOCOL_TCP/IPPROTO_TCP for TCP

0 for default-protocol:

UDP for NET_SOCK_TYPE_DATAGRAM/PF_DGRAM

TCP for NET_SOCK_TYPE_STREAM/PF_STREAM

p_err

Pointer to variable that will receive the return error code from this function:

NET_SOCK_ERR_NONE
NET_SOCK_ERR_NONE_AVAIL
NET_SOCK_ERR_INVALID_FAMILY
NET_SOCK_ERR_INVALID_PROTOCOL
NET_SOCK_ERR_INVALID_TYPE
NET_INIT_ERR_NOT_COMPLETED
NET_ERR_FAULT_LOCK_ACQUIRE

The table below shows you the different ways you can specify the three arguments.

TCP/IP Protocol

Arguments


protocol_family

sock_type

protocol

UDP

NET_SOCK_FAMILY_IP_V4/NET_SOCK_FAMILY_IP_V6

NET_SOCK_TYPE_DATAGRAM

NET_SOCK_PROTOCOL_UDP

UDP

NET_SOCK_FAMILY_IP_V4/NET_SOCK_FAMILY_IP_V6

NET_SOCK_TYPE_DATAGRAM

0

TCP

NET_SOCK_FAMILY_IP_V4/NET_SOCK_FAMILY_IP_V6

NET_SOCK_TYPE_STREAM

NET_SOCK_PROTOCOL_TCP

TCP

NET_SOCK_FAMILY_IP_V4/NET_SOCK_FAMILY_IP_V6

NET_SOCK_TYPE_STREAM

0

Returned Value

Returns a non-negative socket descriptor ID for the new socket connection, if successful;

NET_SOCK_BSD_ERR_OPEN/-1 otherwise.

Required Configuration

None.

Notes / Warnings

The family, type, and protocol of a socket is fixed once a socket is created. In other words, you cannot change a TCP stream socket to a UDP datagram socket (or vice versa) at run-time.

To connect two sockets, both sockets must share the same socket family, type, and protocol.