Socket Functions
This section describes the Winsock functions used for creating, managing, and communicating with sockets.
Core Socket Operations
socket
Creates a socket that is bound to a specific transport service provider.
Syntax: SOCKET socket(int af, int type, int protocol);
bind
Associates a local address with a socket.
Syntax: int bind(SOCKET s, const struct sockaddr *name, int namelen);
connect
Establishes a connection to a specified remote host and port.
Syntax: int connect(SOCKET s, const struct sockaddr *name, int namelen);
listen
Places a socket in a state where it is listening for incoming connection requests.
Syntax: int listen(SOCKET s, int backlog);
accept
Permits an incoming connection on a socket.
Syntax: SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen);
send
Sends data on a connected socket.
Syntax: int send(SOCKET s, const char *buf, int len, int flags);
recv
Receives data from a connected socket.
Syntax: int recv(SOCKET s, char *buf, int len, int flags);
sendto
Sends data to a specific destination, whether or not the socket is connected.
Syntax: int sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen);
recvfrom
Receives a message from a socket and stores the originating address.
Syntax: int recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen);
closeclosesocket
Disables sends and receives on a socket.
Syntax: int closesocket(SOCKET s);
Socket Options
Functions for configuring socket behavior:
getsockopt
Retrieves the current value of a socket option.
Syntax: int getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen);
setsockopt
Sets the current value of a socket option.
Syntax: int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen);
Address Information
Functions for resolving network addresses:
gethostbyname
Retrieves host name and address information from a host name.
Syntax: struct hostent *gethostbyname(const char *name);
getaddrinfo
Provides protocol-independent translation from node and service names to socket addresses.
Syntax: int getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
freeaddrinfo
Frees address information structures returned by getaddrinfo.
Syntax: void freeaddrinfo(struct addrinfo *ai);
Additional Functions
select
Monitors multiple socket descriptors for readiness to read, write, or exceptional conditions.
Syntax: int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);