Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by Wilson Edwards
at 2026-06-18 01:15:59
Point:500 Replies:2 POST_ID:829424USER_ID:12108
Topic:
C;socket;SSL
we used socket for http site then set int socket variable
and find memory address of socket from &socket.
now we switched to use openssl socket for SSL https, and define socket
by SSL * socket.
we tried to find memory address of SSL socket for https
using operator of & , but got wrong address
printf("======%d",&socket)
How to get correct memory address of socket for SSL https ?
Accepted Solution
Author: Wilson Edwards replied at 2026-06-18 01:22:45
500 points Excellent
directly use socket without &
printf("======%d",socket)
The reason is pointer socket from SSL, u can study more these area in google search
Expert: rcschroeder replied at 2026-06-18 01:18:21
When using OpenSSL with sockets, the `SSL` struct does not have a direct equivalent to the `int` type in C. The `SSL` struct is more complex and contains multiple fields, including pointers, integers, and other data types.
To get the memory address of an SSL socket, you cannot use the unary '&' operator directly on the `SSL` pointer. Instead, you can try using the following methods:
1. **Use the `%p` format specifier**:
Instead of using `printf("======%d", &socket)`, try using:
```c
printf("======%p", (void*)&socket);
```
The `%p` format specifier is used to print the memory address of a variable.
2. **Cast the `SSL` pointer to a `void*`**:
Similarly, you can cast the `SSL` pointer to a `void*` and then pass it to `printf()`:
```c
printf("======%p", (void*)&socket);
```
Both of these methods should give you the correct memory address of the SSL socket.
Here's an example code snippet:
```c
#include <openssl/ssl.h>
#include <stdio.h>
// Create an SSL context and a new SSL connection
SSL_CTX* ctx;
SSL* socket;
int main() {
// Initialize OpenSSL
OpenSSL_init_ex();
// Load the SSL/TLS parameters (e.g., certificate and key)
// ...
// Create a new SSL context
ctx = SSL_CTX_new(TLS_client_method());
// Create a new SSL connection
socket = SSL_connect(ctx);
// Print the memory address of the SSL socket
printf("======%p", (void*)&socket);
return 0;
}
```
In this example, we create an SSL context and a new SSL connection using `SSL_connect()`. We then print the memory address of the SSL socket using `printf()` with the `%p` format specifier.
To get the memory address of an SSL socket, you cannot use the unary '&' operator directly on the `SSL` pointer. Instead, you can try using the following methods:
1. **Use the `%p` format specifier**:
Instead of using `printf("======%d", &socket)`, try using:
```c
printf("======%p", (void*)&socket);
```
The `%p` format specifier is used to print the memory address of a variable.
2. **Cast the `SSL` pointer to a `void*`**:
Similarly, you can cast the `SSL` pointer to a `void*` and then pass it to `printf()`:
```c
printf("======%p", (void*)&socket);
```
Both of these methods should give you the correct memory address of the SSL socket.
Here's an example code snippet:
```c
#include <openssl/ssl.h>
#include <stdio.h>
// Create an SSL context and a new SSL connection
SSL_CTX* ctx;
SSL* socket;
int main() {
// Initialize OpenSSL
OpenSSL_init_ex();
// Load the SSL/TLS parameters (e.g., certificate and key)
// ...
// Create a new SSL context
ctx = SSL_CTX_new(TLS_client_method());
// Create a new SSL connection
socket = SSL_connect(ctx);
// Print the memory address of the SSL socket
printf("======%p", (void*)&socket);
return 0;
}
```
In this example, we create an SSL context and a new SSL connection using `SSL_connect()`. We then print the memory address of the SSL socket using `printf()` with the `%p` format specifier.