socket

This function creates a socket.

At a Glance

Header file: Winsock.h
Windows CE versions: 1.0 and later

Syntax

SOCKET socket (int af, int type, int protocol);

Parameters

af

[in] Address family specification.

type

[in] Type specification for the new socket.

The following are the only two type specifications supported for Windows Sockets 1.1:

Value Description
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams with an out-of-band data transmission mechanism. Uses TCP for the Internet address family.
SOCK_DGRAM Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family.

protocol

[in] Particular protocol to be used with the socket that is specific to the indicated address family.

Return Values

A descriptor referencing the new socket indicates that no error occurred. INVALID_SOCKET indicates failure. To get a specific error value, call WSAGetLastError.

Remarks

For Infrared Sockets (IrSock), this function has the following additional requirements:

The PF_INET and AF_IRDA ARPA Internet address formats are supported.

The socket function causes a socket descriptor and any related resources to be allocated and bound to a specific transport service provider. Windows Sockets will utilize the first available service provider that supports the requested combination of address family, socket type and protocol parameters.

Connection-oriented sockets such as SOCK_STREAM provide full-duplex connections, and must be in a connected state before any data can be sent or received on it. A connection to another socket is created with a connect call. Once connected, data can be transferred using send and recv calls. When a session has been completed, a closesocket must be performed.

The communications protocols used to implement a reliable, connection-oriented socket ensure that data is not lost or duplicated. If data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, the connection is considered broken and subsequent calls will fail with the error value set to WSAETIMEDOUT.

Connectionless, message-oriented sockets allow sending and receiving of datagrams to and from arbitrary peers using sendto and recvfrom. If such a socket is connected to a specific peer, datagrams can be sent to that peer using send and can be received only from this peer using recv.

See Also

accept, bind, closesocket, connect, getsockname, getsockopt, ioctlsocket, recvfrom, select, send, sendto, setsockopt, shutdown, WSAStartup