Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function creates a socket that is bound to a specific service provider.

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 table shows the two type specifications supported for Winsock 1.1 . 

Type Description

SOCK_STREAM

Provides sequenced, reliable, two-way, connection-based byte streams with an out of band (OOB) 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.

SOCK_RAW

Supports raw sockets. Raw sockets allow an application to send and receive packets with customized headers.

In Winsock 2.2, many new socket types will be introduced and no longer need to be specified because an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocolsfunction. Socket type definitions appear in Winsock2.h, which will be periodically updated as new socket types, address families, and protocols are defined.

protocol

[in] Protocol to be used with the socket that is specific to the indicated address family. The following list shows the possible values.

IPPROTO_IP

IPPROTO_IPV6

IPPROTO_TCP

IPPROTO_UDP

SOL_SOCKET

IPPROTO_RAW

For more information about these values, see Socket Options.

Return Value

If no error occurs, this function returns a descriptor referencing the new socket. If an error occurs, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError. The following table shows a list of possible error codes.

Error code Description

WSANOTINITIALISED

A successful WSAStartupcall must occur before using this function.

WSAENETDOWN

The network subsystem or the associated service provider has failed.

WSAEAFNOSUPPORT

The specified address family is not supported.

WSAEINPROGRESS

A blocking Winsock call is in progress, or the service provider is still processing a callback function.

WSAEMFILE

No more socket descriptors are available.

WSAENOBUFS

No buffer space is available. The socket cannot be created.

WSAEPROTONOSUPPORT

The specified protocol is not supported.

WSAEPROTOTYPE

The specified protocol is the wrong type for this socket.

WSAESOCKTNOSUPPORT

The specified socket type is not supported in this address family.

Remarks

This function causes a socket descriptor and any related resources to be allocated and bound to a specific transport-service provider. Windows Sockets will use the first available service provider that supports the requested combination of address family, socket type, and protocol parameters. The socket that is created will have the overlapped attribute as a default.

Sockets with the overlapped attribute can be created by using WSASocket. All functions that allow overlapped operation ( WSASend, WSARecv, WSASendTo, WSARecvFrom, and WSAIoctl) also support nonoverlapped usage on an overlapped socket if the values for parameters related to overlapped operation are NULL.

When selecting a protocol and its supporting service provider, this procedure will only choose a base protocol or a provider chain, not a protocol layer by itself. Unchained protocol layers are not considered to have partial matches on type or afeither. That is, they do not lead to an error code of WSAEAFNOSUPPORT or WSAEPROTONOSUPPORT if no suitable protocol is found.

Important:
The manifest constant AF_UNSPEC continues to be defined in the header file, but its use is strongly discouraged because this can cause ambiguity in interpreting the value of the protocol parameter.

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 (Windows Sockets)call. Once connected, data can be transferred using sendand recvcalls. When a session has been completed, a closesocketcall 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 code set to WSAETIMEDOUT.

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

Support for the sockets with type RAWis not required, but service providers are encouraged to support raw sockets as practicable.

Notes for IrDA Sockets

  • The Af_irda.h header file must be explicitly included.

  • Only SOCK_STREAMis supported; the SOCK_DGRAMtype is not supported by IrDA.

  • The protocol parameter is always set to zero for IrDA.

Note:
On Windows NT and Windows 2000, raw socket support requires administrative privileges.

For more inforamtion about IrDA support in Windows Embedded CE, see Infrared Communications.

Notes for Bluetooth Sockets

  • The ws2bth.h header file must be explicitly included.

  • Only SOCK_STREAM is supported; the SOCK_DGRAMtype is not supported by Bluetooth.

  • The protocol parameter is always set to BTHPROTO_RFCOMM for Bluetooth.

Requirements

Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also