# %listen() function

The %listen() function marks the specified
socket as accepting incoming connections and limits the number of
outstanding connections in the system queue.

## Syntax

```
code = %listen(fd, backlog)
```

## Parameter(s)

| fd | File descriptor of the local socket returned by a previous call to the FlashBASIC C function %socket(). |
| --- | --- |
| backlog | Maximum number of outstanding connections. The maximum value for this number is SOMAXCONN defined in the include file: dm,bp,unix.h socket.h. |

## Description

To compile successfully, the
statement cfunction socket.builtin must be
included in the source code. Upon successful completion, a value of
0 is returned in code. In case of error, a value of -1 is returned
and the 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.
char buffer[1024]
if %bind(fd, af$inet, inaddr$any, 1024)<0 then
 crt ’bind failed’; stop
end
* Wait for incoming connection and allow up to
* three more to be waiting.
%listen(fd, 3)
* Accept a connection until got them all
loop
 address=0; port=0
 socket=%accept(fd, &address, &port)
until socket<0 do
 crt ’Called by address ’:addr:’, port #’:port
* Read data from the data link
 %recv(socket, buffer, 1024)
 char buff[1024]
* Done with this connection, close it
 %closesocket(socket)
repeat
```

## See also

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

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