# %bind() function

The %bind() function binds a socket to a named resource. In case of a
 network, the resource would be an access point into the system.

## Syntax

```
code = %bind(fd, addr.family, address, port)
```

## Parameter(s)

| fd | File descriptor of the local socket returned by a previous call to the FlashBASIC C function %socket(). |
| --- | --- |
| addr.family | Specifies the addressing scheme used by the protocol. This field must match the address family used when creating the socket. Valid values are defined in the include file: dm,bp,unix.h socket.h |
| address | Legal values are defined in the include file: dm,bp,unix.h socket.h. |
| port | Port number on the local host. The legal value for this field depends on the protocol. On TCP/IP, for example, valid port numbers are from 1024 to 32767. This field might not be applicable for all protocols. |

## Description

To compile successfully, the
statement cfunction socket.builtin must be included
in the source code.

On successful completion, a value of 0 is returned in code.

 In case of an error, -1 is returned and the FlashBASIC system(0)
 function is set to the value of errno.

## Example(s)

```
cfunction socket.builtin
include dm,bp,includes sysid.inc
include dm,bp,unix.h socket.h
* Create a socket
fd=%socket(af$inet, sock$stream, 0)
* Bind the socket to a local Ethernet port.
* Use default address.
if %bind(fd, af$inet, inaddr$any, 1024)<0 then
 crt ’bind failed’; stop
end
* Wait for incoming connection
%listen(fd, 1)
* Accept a connection
address=0; port=0
rd=%accept(fd, &address, &port)
* Read data from the data link
char buffer[128]
%recv(rd, buffer, 1024)
```

## See also

- [%accept() function](https://d3codex.com/pickbasic-flashbasic/percent-accept-function/)
- [%gethostid() function](https://d3codex.com/pickbasic-flashbasic/percent-gethostid-function/)
- [%listen() function](https://d3codex.com/pickbasic-flashbasic/percent-listen-function/)
- [%read() function](https://d3codex.com/pickbasic-flashbasic/percent-read-function/)
- [%socket() function](https://d3codex.com/pickbasic-flashbasic/percent-socket-function/)

---
Source: https://d3codex.com/pickbasic-flashbasic/percent-bind-function/ - part of the D3Codex reference.
