Winsock Functions
This section provides detailed information on the Windows Sockets API (Winsock) functions. Winsock is a Microsoft implementation of the Berkeley sockets API that provides application developers with a familiar programming interface for network communication.
Overview
Winsock provides a set of functions for establishing network connections, sending and receiving data, and managing network protocols. These functions are essential for developing network-aware applications on Windows.
The Winsock API supports both connection-oriented (like TCP) and connectionless (like UDP) communication protocols.
API Reference
accept
Accepts a connection on a socket.
bind
Associates a local address with a socket.
closesocket
Closes an existing socket.
connect
Establishes a connection to a remote socket.
getaddrinfo
Resolves a network address and service.
gethostname
Retrieves the standard host name for the local computer.
getpeername
Retrieves the address of the peer connected to a socket.
getsockname
Retrieves the local name for a socket.
htonl
Converts a u_long
from host order to network order.
htons
Converts a u_short
from host order to network order.
ioctlsocket
Sets or retrieves the non-blocking mode of a socket.
listen
Places a socket in a state where it listens for incoming connection requests.
ntohl
Converts a u_long
from network order to host order.
ntohs
Converts a u_short
from network order to host order.
recv
Receives data from a connected socket.
recvfrom
Receives a datagram and stores the address of the sender.
select
Monitors multiple socket handles for activity.
send
Sends data on a connected socket.
sendto
Sends data to a specific remote address.
setsockopt
Sets socket options.
socket
Creates a socket that is bound to a specific transport service provider.
WSAStartup
Initializes the Winsock DLL.
WSACleanup
Shuts down the Winsock DLL.
accept
Syntax: SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen);
Description: The accept
function extracts the first connection available on the queue of pending connections for the listening socket, s
, creates a new socket with the same properties of s
but with its own connection, and allocates a new socket descriptor for this connection. The newly created socket descriptor can be used to send and receive data on the connection. The original socket s
continues to listen for additional connections.
Parameters:
s
: A descriptor identifying a socket that is placed in a listening state bylisten
.addr
: (Optional) A pointer to a buffer that will receive the address of the connecting entity, as known to the communications service. The structure of theaddr
parameter is determined by the address family established when the socket was created.addrlen
: (Optional) A pointer to an integer that specifies the size of the buffer pointed to byaddr
. On return, this parameter is populated with the actual length of the address returned.
Return Value: If no error occurs, accept
returns a descriptor for a newly created socket that is connected to wp
. This descriptor cannot be used to accept more connections. If an error occurs, a value of INVALID_SOCKET
is returned, and a specific error code can be retrieved by calling WSAGetLastError
.
bind
Syntax: int bind(SOCKET s, const struct sockaddr *name, int namelen);
Description: The bind
function associates a local address with a socket. For a connectionless socket, this is the address the socket will use for datagrams. For a connection-oriented socket, this is the address that will be assigned to the socket. For TCP/IP, the name
parameter is a pointer to a sockaddr_in
structure that specifies the local IP address and port number.
Parameters:
s
: A descriptor identifying an unbound socket.name
: A pointer to asockaddr
structure that specifies the address to associate with the socket.namelen
: The length, in bytes, of the structure specified by thename
parameter.
Return Value: If no error occurs, bind
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.