Socket Function
int type,
int protocol
);
The Socket function creates a socket that is used to send or receive data. (Winsock)
Syntax
SOCKET Socket(
int af,
int type,
int protocol
);
Parameters
af
Address family. For the Transports that is used with the Windows Sockets API, this can be AF_INET or AF_INET6.
The Winsock catalog defines the available address families. For more information, see the Winsock Provider and Protocol Services topic.
type
Type of socket.
Supported values include the following:
SOCK_STREAM
(Provides sequenced, reliable, two-way, connection-based byte streams.)SOCK_DGRAM
(Supports datagrams, which are unreliable, connectionless datagrams of fixed maximum length.)SOCK_RAW
(Provides raw-network protocol access such that both incoming and outgoing packets can be handled.)
protocol
Protocol for the socket type. The commonly requested protocols for the SOCK_STREAM
and SOCK_DGRAM
types are IPPROTO_TCP and IPPROTO_UDP.
The value for this parameter can be a specific protocol for the given address family or zero if you want to use the default protocol for the specified address family and socket type. The Winsock catalog defines the available protocols.
Return Value
SOCKET
If no error occurs, Socket returns a descriptor for the newly created socket. This descriptor can be used in subsequent function calls to create client or server applications.
If an error occurs, Socket returns INVALID_SOCKET
. To get extended error information, call WSAGetLastError
.
Remarks
The Socket function creates a socket endpoint. The created socket is a communication endpoint and does not have any properties other than the address family, socket type, and protocol specified.
The af
parameter specifies the address family to be used. The Winsock API supports the AF_INET
(IPv4) and AF_INET6
(IPv6) address families.
The type
parameter specifies the socket type. The common socket types are SOCK_STREAM
for connection-oriented communication (like TCP) and SOCK_DGRAM
for connectionless communication (like UDP).
The protocol
parameter specifies the protocol to be used. If set to 0, the system will choose a default protocol appropriate for the specified address family and socket type.
For TCP/IP, specifying IPPROTO_TCP
for the protocol
parameter when using SOCK_STREAM
will establish a TCP socket. Specifying IPPROTO_UDP
for the protocol
parameter when using SOCK_DGRAM
will establish a UDP socket.
When an application is finished with the socket, it should close the socket using the closesocket function.
A socket descriptor is a handle to the Winsock service provider.
Winsock Catalog Entries
The Winsock catalog is an implementation-specific database that contains information about Winsock service providers and the protocols they support. Each Winsock catalog entry describes a combination of an address family, socket type, and protocol. The WSAEnumProtocols function can be used to enumerate these entries.
Ordering of Winsock Catalog Entries
The order of Winsock catalog entries can affect which protocol is chosen when the protocol
parameter is set to zero. The system searches the Winsock catalog for the first entry that matches the specified address family and socket type. If multiple entries match, the first one encountered is used.
Requirements
Client: Requires Windows Vista; Windows XP Professional x64 Edition, Windows XP, and Windows 2000 Professional. For older versions, see the Requirements section of the SDK documentation.
Server: Requires Windows Server 2008; Windows Server 2003; Windows 2000 Server. For older versions, see the Requirements section of the SDK documentation.
Header: Winsock2.h
Library: Ws2_32.lib
DLL: Ws2_32.dll
See Also
Example Usage
The following code snippet demonstrates how to create a TCP socket:
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// Create a socket for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket created successfully.\n");
// ... further operations ...
// Cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}