---
title: "sock_open()"
description: "The sock_open() function creates an endpoint for communication and returns a file descriptor that refers to that endpoint. The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process."
url: "https://wasix.org/docs/api-reference/wasix/sock_open/"
markdown: "https://wasix.org/docs/api-reference/wasix/sock_open.md"
---

# `sock_open()`

Create an endpoint for communication.

## Description

The `sock_open()` function creates an endpoint for communication and returns a file descriptor that refers to that endpoint. The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.

## Syntax

```ebnf
  ;;; Note: This is similar to `socket` in POSIX using PF_INET
  ;;;
  ;;; for the process.
  ;;; call will be the lowest-numbered file descriptor not currently open
  ;;; tor that refers to that endpoint. The file descriptor returned by a successful
  ;;; creates an endpoint for communication and returns a file descriptor
  ;;;
  ;;; Create an endpoint for communication.
  (@interface func (export "sock_open")
    ;;; Address family
    (param $af $address_family)
    ;;; Socket type, either datagram or stream
    (param $socktype $sock_type)
    ;;; Socket protocol
    (param $sock_proto $sock_proto)
    ;;; The file descriptor of the socket that has been opened.
    (result $error (expected $fd (error $errno)))
  )
```

## Parameters

- `ctx`: A mutable reference to the function environment.
- `af`: The address family of the socket.
- `ty`: The socket type, either datagram or stream.
- `pt`: The socket protocol.
- `ro_sock`: A WebAssembly pointer to a memory location where the file descriptor of the opened socket will be stored.

## Return Value

The function returns an `Errno` value indicating the outcome of the operation. If the operation is successful, `Errno::Success` is returned. If an error occurs, an appropriate `Errno` value is returned.

## Notes

- The `sock_open()` function creates an endpoint for communication.
- The endpoint is created using the specified address family (`af`), socket type (`ty`), and socket protocol (`pt`).
- The file descriptor returned by the function refers to the opened socket endpoint.
- The file descriptor will be the lowest-numbered file descriptor not currently open for the process.
- The function `sock_open()` is similar to the `socket` function in POSIX, using the `PF_INET` address family.
- The behavior and limitations of the `sock_open()` function may vary depending on the specific runtime environment and underlying networking implementation. It is important to consult the documentation or specifications of the specific environment to understand its behavior in that context.

### Address Family Values

|Type|Value|Description|
|-|-|-|
|`AF_UNSPEC`|0|Unspecified placeholder|
|`AF_INET`|1|Represents a IPv4 address family, and will require a [`sockaddr_in`](https://wasix.org/docs/api-reference/wasix/sock_bind.md#ipv4) when binding|
|`AF_INET6`|2|Represents a IPv6 address family, and will require a [`sockaddr_in6`](https://wasix.org/docs/api-reference/wasix/sock_bind.md#ipv6) when binding|
|`AF_LOCAL`|3|Internal socket used for communicating within the same system for IPC calls, this will require a [`sockaddr_un`](https://wasix.org/docs/api-reference/wasix/sock_bind.md#ipc) when binding|

Please refer to [this file ](https://github.com/wasix-org/wasix-libc/blob/main/expected/wasm32-wasi/predefined-macros.txt) in the case the value you are looking for isn’t here

### Socket Type Values

|Type|Value|Description|
|-|-|-|
|`SOCK_STREAM`|1|TCP Socket|
|`SOCK_DGRAM`|2|UDP Socket|
|`SOCK_CLOEXEC`|`0x00002000`|This is a flag that can be combined with other socket types using the bitwise OR operator `\|`|

Please refer to [this file ](https://github.com/wasix-org/wasix-libc/blob/main/expected/wasm32-wasi/predefined-macros.txt) in the case the value you are looking for isn’t here

---

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