---
title: "sock_recv()"
description: "The sock_recv() function is used to receive a message from a socket. It is similar to the recv function in POSIX, but it also supports reading the data into multiple buffers in the manner of readv."
url: "https://wasix.org/docs/api-reference/wasi/sock_recv/"
markdown: "https://wasix.org/docs/api-reference/wasi/sock_recv.md"
---

# `sock_recv()`

Receive a message from a socket.

## Description

The `sock_recv()` function is used to receive a message from a socket. It is similar to the `recv` function in POSIX, but it also supports reading the data into multiple buffers in the manner of `readv`.

The function receives data from the socket and stores it in the provided scatter/gather vectors. The number of bytes stored in `ri_data` and the message flags are returned.

## Syntax

```ebnf
  ;;; the data into multiple buffers in the manner of `readv`.
  ;;; Note: This is similar to `recv` in POSIX, though it also supports reading
  ;;; Receive a message from a socket.
  (@interface func (export "sock_recv")
    (param $fd $fd)
    ;;; List of scatter/gather vectors to which to store data.
    (param $ri_data $iovec_array)
    ;;; Message flags.
    (param $ri_flags $riflags)
    ;;; Number of bytes stored in ri_data and message flags.
    (result $error (expected (tuple $size $roflags) (error $errno)))
  )
```

## Parameters

- `ctx`: A mutable reference to the function environment.
- `sock`: The file descriptor of the socket to receive from.
- `ri_data`: A list of scatter/gather vectors to which the received data will be stored.
- `ri_data_len`: The length of the scatter/gather vector list.
- `ri_flags`: Message flags.
- `ro_data_len`: A pointer to store the number of bytes read.
- `ro_flags`: A pointer to store the message flags.

## Return Value

The function returns a `Result` containing an `Errno` value. If the operation is successful, `Errno::Success` is returned. Otherwise, an appropriate `Errno` value indicating the error is returned.

## Notes

- The `sock_recv()` function receives a message from a socket and stores it in the provided scatter/gather vectors.
- The function supports reading the data into multiple buffers.
- The `ri_data` parameter represents a list of scatter/gather vectors, where each vector contains a buffer and its length.
- The `ri_data_len` parameter specifies the length of the scatter/gather vector list.
- The received data is stored in the buffers specified by the scatter/gather vectors.
- The number of bytes read from the socket is stored in `ro_data_len`.
- The message flags are stored in `ro_flags`.
- The specific behavior of the `sock_recv()` function may vary depending on the runtime environment and underlying networking implementation.

---

[Documentation index](https://wasix.org/llms.txt) · [HTML version](https://wasix.org/docs/api-reference/wasi/sock_recv/)
