The demo program is to retrieve my public IP address, but this can be used for any TCP request over SSL
- Note: this has only been tested on Mac M2
make all
./client
1) Project structure (made with https://marketplace.visualstudio.com/items?itemName=jmkrivocapich.drawfolderstructure)
└── 📁cpp-tcp-client
└── 📁include
├── http_client.h
├── tcp_socket.h
├── tls_client.h
└── 📁src
├── http_client.cpp
├── main.cpp
├── tcp_socket.cpp
├── tls_client.cpp
├── Makefile
2) How it works
- Resolve host: get IP from hostname (
gethostbyname
) - Open TCP socket:
socket()
+connect()
to server:443 - Start TLS: wrap socket with OpenSSL (
SSL_connect
) - Send HTTP request:
SSL_write("GET ...")
- Receive response:
SSL_read()
until done - Parse body: extract the data
- Cleanup: shutdown TLS, free SSL, close socket
- https://man7.org/linux/man-pages/man3/gethostbyname.3.html
- https://www.geeksforgeeks.org/cpp/socket-programming-in-cpp/
- https://wiki.openssl.org/index.php/SSL/TLS_Client
- https://www.ibm.com/docs/en/ztpf/1.1.2024?topic=apis-tls-client-method
- https://www.ibm.com/docs/en/ztpf/1.1.2024?topic=layer-ssl-examples