The bind function associates a local protocol information structure with a socket.
bind Function (Winsock)The bind function associates a local protocol information structure with a socket.
int bind(
_In_ SOCKET s,
_In_reads_bytes_(namelen) const struct sockaddr *name,
_In_ int namelen
);
| Parameter | Description |
|---|---|
s |
A descriptor that identifies an unconnected socket. |
name |
A pointer to a sockaddr structure that specifies the address and port to assign to the bound socket. For IPv4, this is a sockaddr_in structure. For IPv6, this is a sockaddr_in6 structure. |
namelen |
The size, in bytes, of the structure pointed to by the name parameter. |
If no error occurs, bind returns zero. Otherwise, a Winsock error code is returned, and a specific error code can be retrieved by calling WSAGetLastError.
The bind function is used to assign a local address and port to a socket. This is typically done for a server application, which needs a well-known address and port so that clients can connect to it. A client application can also use bind to associate a specific local port with a socket, although this is less common.
When binding a socket, you must specify the address family (e.g., AF_INET for IPv4, AF_INET6 for IPv6) in the sockaddr structure.
If you are binding a server socket to listen for incoming connections, it is common to bind to the wildcard address (e.g., INADDR_ANY for IPv4) to accept connections on any available network interface. The port number should be a specific, non-privileged port (typically above 1024).
For TCP sockets, binding to a specific port prepares the socket to accept incoming connection requests on that port using the listen and accept functions.
For UDP sockets, binding associates the socket with a specific local port for sending and receiving datagrams.
A socket can only be bound once. Attempting to bind a socket that has already been bound will result in a WSAEINVAL error.
If namelen is zero or name is null, bind will return WSAEFAULT.
When binding to an ephemeral port (port number 0), the system assigns a unique, ephemeral port number. This is typically used by client applications.
The following code snippet demonstrates how to bind a TCP socket to a specific local address and port for a server application.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET listenSocket;
struct sockaddr_in serverAddr;
int port = 8080; // Example port
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return 1;
}
// Create a socket
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
printf("socket failed: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Set up the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Bind to localhost
serverAddr.sin_port = htons(port);
// Bind the socket
if (bind(listenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("bind failed: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Socket bound successfully to 127.0.0.1:%d\n", port);
// ... Further socket operations (listen, accept, etc.) ...
closesocket(listenSocket);
WSACleanup();
return 0;
}
| Minimum supported client | Windows 2000 Professional [desktop apps only] |
| Minimum supported server | Windows 2000 Server [desktop apps only] |
| Header | Winsock2.h (include Winsock2.h and Ws2tcpip.h) |
| Library | Ws2_32.lib |
| DLL | Ws2_32.dll |
Tip: For a robust server implementation, consider binding to INADDR_ANY to listen on all network interfaces if your application is intended to be accessible from other machines.
Important: Ensure that the port number you choose is not already in use by another application. Ports below 1024 are typically reserved for system services and require administrative privileges.