Data Transfer

This section details the core functions and mechanisms used for sending and receiving data over a Winsock socket. Efficient and reliable data transfer is fundamental to network communication.

send

Sends data on a connected socket or for unconnected sockets, specifying the destination address.

Syntax
int send(
  SOCKET s,
  const char *buf,
  int len,
  int flags
);
Parameters
  • s: A descriptor identifying a socket.
  • buf: A pointer to a buffer containing the data to be transmitted.
  • len: The length, in bytes, of the data in the buffer pointed to by the buf parameter.
  • flags: Flags that control the transmission.
Return Value
If no error occurs, send returns the total number of bytes sent, which may be less than the number requested with the len parameter. If the socket is nonblocking and cannot send data immediately, SOCKET_ERROR is returned, and WSAGetLastError indicates WSAEWOULDBLOCK.
For connection-oriented sockets (like SOCK_STREAM), send attempts to send as much data as the network buffer allows. For datagram sockets (like SOCK_DGRAM), send sends the entire datagram.

recv

Receives data from a connected socket or for unconnected sockets.

Syntax
int recv(
  SOCKET s,
  char *buf,
  int len,
  int flags
);
Parameters
  • s: A descriptor identifying a socket.
  • buf: A pointer to the buffer into which the received data will be copied.
  • len: The maximum number of bytes that can be stored in buf.
  • flags: Flags that control the reception.
Return Value
If no error occurs, recv returns the number of bytes received, which may be less than the number requested with the len parameter. If the connected host has performed an orderly shutdown for sends by sending a FIN, 0 bytes will be returned. If the socket is nonblocking and cannot receive data immediately, SOCKET_ERROR is returned, and WSAGetLastError indicates WSAEWOULDBLOCK.
It is important to check the return value of recv. A return value of 0 indicates that the connection has been gracefully closed by the peer.

sendto

Sends data on a socket. If the socket is unconnected, the destination address is specified.

Syntax
int sendto(
  SOCKET s,
  const char *buf,
  int len,
  int flags,
  const struct sockaddr *to,
  int tolen
);
Parameters
  • s: A descriptor identifying a socket.
  • buf: A pointer to a buffer containing the data to be transmitted.
  • len: The length, in bytes, of the data in the buffer pointed to by the buf parameter.
  • flags: Flags that control the transmission.
  • to: A pointer to a sockaddr structure that specifies the destination address.
  • tolen: The length, in bytes, of the sockaddr structure pointed to by the to parameter.
Return Value
If no error occurs, sendto returns the total number of bytes sent. Otherwise, SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
This function is typically used with connectionless sockets (like SOCK_DGRAM) where each send operation may be directed to a different destination.

recvfrom

Receives data from a socket and stores the source address.

Syntax
int recvfrom(
  SOCKET s,
  char *buf,
  int len,
  int flags,
  struct sockaddr *from,
  int *fromlen
);
Parameters
  • s: A descriptor identifying a socket.
  • buf: A pointer to the buffer into which the received data will be copied.
  • len: The maximum number of bytes that can be stored in buf.
  • flags: Flags that control the reception.
  • from: (Optional) A pointer to a sockaddr structure that will receive the source address of the datagram.
  • fromlen: (Optional) A pointer to a int that specifies the size of the buffer pointed to by the from parameter, and on return, contains the actual size of the address returned.
Return Value
If no error occurs, recvfrom returns the number of bytes received. If the socket is nonblocking and cannot receive data immediately, SOCKET_ERROR is returned, and WSAGetLastError indicates WSAEWOULDBLOCK.
When using recvfrom with connectionless sockets, the from and fromlen parameters are crucial for identifying the sender.