# %connect_ssl() function

The %connect_ssl() function requests
a secure connection between two sockets.

Note: To use this function, the OpenSSL libraries must be
installed.

The %connect_ssl() function
requests a secure connection between two sockets.

## Syntax

```
code = %connect_ssl(fd, addr.family, host, port, &ssl_fd);
```

## 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. |
| host | Destination host name. The string must be known to the local network manager. Internally, this calls getaddrinfo to resolve the remote host. |
| port | Port number on the distant host. Legal value for this field depends on the protocol. On TCP/IP, for example, valid port numbers are from 1024 to 32767. |
| ssl_fd | File descriptor of the Secure connection. |

## 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, and the ssl_fd FlashBASIC variable is updated.

In the case of an error, the
return code is a negative value. The table below lists all of the
error return codes:

| -1 | Socket error, system(0) function is set to the value of errno. |
| --- | --- |
| -2 | OpenSSL is not installed. |
| -6 | Unable to create a SSL. |
| -7 | Unable to establish the TLS/SSL handshake with the server, the FlashBASIC system(0) function is set to the value of SSL_get_error. |
The connection is closed when the socket is closed.

## 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)
* Connect to the distant host
ssl_fd = 0
code=%connect_ssl(fd, af$inet, "prod", 1024, &ssl_fd)
if code < 0 then print "failed to connect"; stop

* Write data to it thru the secure connection
msg="Hello World"
msglen=len(msg)
code = %write_ssl(ssl_fd, msg,msglen)
if code < 0 then
 code = %close_ssl(fd, &ssl_fd)
 stop
end
* read from host
Char buffer[100]
code = %read_ssl(ssl_fd, buffer, 100)
if code < 0 then
 code = %close_ssl(fd, &ssl_fd)
 stop
end
print "READ: ":buffer
code = %close_ssl(fd, &ssl_fd)
```

## See also

- [%connect() function](https://d3codex.com/pickbasic-flashbasic/percent-connect-function/)
- [%accept_ssl() function](https://d3codex.com/pickbasic-flashbasic/percent-accept-ssl-function/)
- [%close_ssl() function](https://d3codex.com/pickbasic-flashbasic/percent-close-ssl-function/)
- [%read_ssl() function](https://d3codex.com/pickbasic-flashbasic/percent-read-ssl-function/)
- [%write_ssl() function](https://d3codex.com/pickbasic-flashbasic/percent-write-ssl-function/)

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