Comprehensive documentation for networking functions in Windows.
This section provides detailed information about the functions available in the Windows Sockets API (Winsock) for network programming.
Prototype: SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen);
The accept
function retrieves from the queue of pending connections on a socket the first connection, creates a new socket with the same properties of the original socket, and allocates a new file descriptor for that socket.
s
addr
sockaddr
).addrlen
addr
argument. When accept
returns, this parameter is updated to indicate the actual size of the address returned.If no error occurs, accept
returns a descriptor for a newly created socket that is connected to s
by the same path. This returned descriptor is the same as a socket created with the socket
function, and it has the same properties as the socket pointed to by the s
parameter. If an error occurs, the value INVALID_SOCKET
is returned, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int bind(SOCKET s, const struct sockaddr *name, int namelen);
The bind
function associates a local address with a socket.
s
name
sockaddr
structure that specifies the local address to assign to the socket. The structure should contain the address family, IP address, and port number.namelen
sockaddr
structure pointed to by the name
parameter.If the bind operation completes successfully, bind
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int closesocket(SOCKET s);
The closesocket
function closes an existing socket.
s
If no error occurs, closesocket
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int connect(SOCKET s, const struct sockaddr *name, int namelen);
The connect
function establishes a connection to a remote socket.
s
name
sockaddr
structure that specifies the remote address to which to connect.namelen
sockaddr
structure pointed to by the name
parameter.If the connection is made successfully, connect
returns zero. If the socket is nonblocking and the connection cannot be made immediately, connect
returns SOCKET_ERROR
and WSAGetLastError
returns WSAEWOULDBLOCK
. The application can use select
to determine when the socket becomes writable, at which point the connection is established. If another error occurs, connect
returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: INT getaddrinfo(PCSTR pNodeName, PCSTR pServiceName, const ADDRINFOA *pHints, PADDRINFOA *ppResult);
The getaddrinfo
function provides protocol-independent translation from a host name and a service name to a set of socket address structures.
pNodeName
pServiceName
pHints
ADDRINFOA
structure that specifies constraints on the type of socket address to return. NULL
can be specified to indicate no constraints.ppResult
ADDRINFOA
structure. The getaddrinfo
function allocates and initializes a linked list of ADDRINFOA
structures for the caller. This parameter receives a pointer to the first ADDRINFOA
structure in the linked list.On success, getaddrinfo
returns zero. On failure, it returns an appropriate Winsock error code, such as EAI_FAMILY
, EAI_SOCKTYPE
, EAI_BADFLAGS
, EAI_BADHINTS
, EAI_FAIL
, EAI_AGAIN
, or EAI_MEMORY
.
Prototype: int gethostname(char *name, int namelen);
The gethostname
function retrieves the standard hostname for the current computer.
name
namelen
name
parameter.If no error occurs, gethostname
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int getpeername(SOCKET s, struct sockaddr *name, int *namelen);
The getpeername
function retrieves the name of the remote station that a socket is connected to.
s
name
namelen
name
parameter. When getpeername
returns, this parameter is updated to indicate the actual size of the returned address.If no error occurs, getpeername
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int getsockname(SOCKET s, struct sockaddr *name, int *namelen);
The getsockname
function retrieves the common names associated with a socket.
s
name
namelen
name
parameter. When getsockname
returns, this parameter is updated to indicate the actual size of the returned address.If no error occurs, getsockname
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: unsigned long htonl(unsigned long hostlong);
The htonl
function converts a 32-bit unsigned integer from host byte order to network byte order.
hostlong
The htonl
function returns the value of the 32-bit unsigned integer in network byte order.
Prototype: unsigned short htons(unsigned short hostshort);
The htons
function converts a 16-bit unsigned integer from host byte order to network byte order.
hostshort
The htons
function returns the value of the 16-bit unsigned integer in network byte order.
Prototype: in_addr inet_addr(const char *cp);
The inet_addr
function converts an IPv4 address from dotted-decimal string notation to a 32-bit unsigned integer (the Internet host address).
cp
If the input string is a valid IPv4 address, inet_addr
returns the corresponding 32-bit value. If the input string is not a valid IPv4 address, inet_addr
returns INADDR_NONE
.
Prototype: char *inet_ntoa(struct in_addr in);
The inet_ntoa
function converts an IPv4 address from network byte order to a string in dotted-decimal notation.
in
If no error occurs, inet_ntoa
returns a pointer to a static buffer containing the dotted-decimal string representation of the IPv4 address. If an error occurs, inet_ntoa
returns NULL
.
Prototype: int ioctlsocket(SOCKET s, long cmd, u_long *argp);
The ioctlsocket
function controls the I/O mode of a socket.
s
cmd
argp
If no error occurs, ioctlsocket
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int listen(SOCKET s, int backlog);
The listen
function puts a socket into a listening state. This means that the socket is ready to accept incoming connection requests.
s
backlog
If no error occurs, listen
returns zero. Otherwise, it returns SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: unsigned long ntohl(unsigned long netlong);
The ntohl
function converts a 32-bit unsigned integer from network byte order to host byte order.
netlong
The ntohl
function returns the value of the 32-bit unsigned integer in host byte order.
Prototype: unsigned short ntohs(unsigned short netshort);
The ntohs
function converts a 16-bit unsigned integer from network byte order to host byte order.
netshort
The ntohs
function returns the value of the 16-bit unsigned integer in host byte order.
Prototype: int recv(SOCKET s, char *buf, int len, int flags);
The recv
function receives data from a connected socket or a listening socket.
s
buf
len
buf
parameter.flags
If no error occurs, recv
returns the number of bytes received. If the connected socket is gracefully closed, the return value is zero. If the socket is in nonblocking mode and there is no data available to be received, recv
returns SOCKET_ERROR
and WSAGetLastError
returns WSAEWOULDBLOCK
. Otherwise, a value of SOCKET_ERROR
is returned, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int send(SOCKET s, const char *buf, int len, int flags);
The send
function sends data on a connected socket.
s
buf
len
buf
parameter.flags
If no error occurs, send
returns the number of bytes sent. If the socket is in nonblocking mode and the data cannot be sent immediately, send
returns SOCKET_ERROR
and WSAGetLastError
returns WSAEWOULDBLOCK
. Otherwise, a value of SOCKET_ERROR
is returned, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: SOCKET socket(int af, int type, int protocol);
The socket
function creates a socket that is bound to a specific transport-service provider.
af
type
SOCK_STREAM
(for connection-oriented protocols like TCP) and SOCK_DGRAM
(for connectionless protocols like UDP).protocol
If no error occurs, socket
returns a descriptor for the newly created socket. This descriptor is a small integer that is used to reference the socket in subsequent function calls. Otherwise, socket
returns INVALID_SOCKET
, and a specific error code can be retrieved by calling WSAGetLastError
.
Prototype: int WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData);
The WSAStartup
function initiates the use of the Winsock DLL. It is the first Winsock function that an application must call.
wVersionRequired
lpWSAData
WSADATA
structure that receives implementation-specific information about the Winsock DLL. The WSADATA
structure should not be initialized by the caller.If the call to WSAStartup
is successful, the return value is zero. If the call fails, the return value is one of the Winsock error codes listed in the Winsock error code table.
Prototype: int WSACleanup(void);
The WSACleanup
function terminates the use of the Winsock DLL. It is the last Winsock function that an application must call.
If the call to WSACleanup
is successful, the return value is zero. If the call fails, the return value is SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.