Winsock Operations

Comprehensive guide to network programming operations using the Windows Sockets API.

Core Winsock Operations

The Windows Sockets API (Winsock) provides a set of functions for network communication. These operations form the foundation for building network-aware applications on Windows.

Socket Creation and Initialization

Understanding how to create, bind, and listen on sockets is the first step in establishing network connections. This involves functions like socket(), bind(), and listen().

Connection Establishment (TCP)

For connection-oriented communication using TCP, applications use connect() to initiate a connection to a remote host and accept() to handle incoming connection requests.

Data Transmission and Reception

The primary operations for sending and receiving data involve send(), recv(), sendto(), and recvfrom(), depending on the protocol used.

Socket Closure

Gracefully closing network connections is crucial. The closesocket() function is used to terminate socket connections and release associated resources.

Socket Options and Control

The getsockopt() and setsockopt() functions allow applications to query and modify various parameters associated with a socket, such as timeouts and buffer sizes.

Name Resolution (DNS)

Resolving hostnames to IP addresses and vice versa is handled by functions like gethostbyname() and getaddrinfo(), essential for locating network services.

Socket Creation: socket()

The socket() function creates a new socket descriptor, which is an integer handle representing a socket. It specifies the address family, socket type, and protocol.

int socket(
  int af,
  int type,
  int protocol
);

Binding a Socket: bind()

The bind() function associates a local address (IP address and port number) with a socket. This is typically done on the server side before listening for connections.

int bind(
  SOCKET         s,
  const sockaddr FAR *name,
  int            namelen
);

The name parameter is a pointer to a sockaddr structure containing the address information.

Establishing Connections (TCP)

Client Side: connect()

The connect() function is used by the client to establish a connection with a server.

int connect(
  SOCKET         s,
  const sockaddr FAR *name,
  int            namelen
);

Server Side: accept()

The accept() function, when called on a listening socket, blocks until a connection request arrives. It then creates a new socket for the connection.

SOCKET accept(
  SOCKET   s,
  sockaddr FAR *addr,
  int FAR  *addrlen
);

Sending and Receiving Data

Stream Sockets (TCP): send() and recv()

These functions are used for reliable, ordered data transfer over TCP.

int send(
  SOCKET s,
  const char FAR *buf,
  int            len,
  int            flags
);

int recv(
  SOCKET s,
  char FAR *buf,
  int            len,
  int            flags
);

Datagram Sockets (UDP): sendto() and recvfrom()

These functions are used for connectionless data transfer over UDP, allowing specification of the destination address for each packet.

int sendto(
  SOCKET   s,
  const char FAR *buf,
  int            len,
  int            flags,
  const sockaddr FAR *to,
  int            tolen
);

int recvfrom(
  SOCKET   s,
  char FAR *buf,
  int            len,
  int            flags,
  sockaddr FAR *from,
  int FAR  *fromlen
);

Closing a Socket: closesocket()

This function closes an existing socket. For TCP sockets, it initiates the connection termination process.

int closesocket(
  SOCKET s
);

Further Reading