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 test test
at 2026-06-17 10:06:08
Point:500 Replies:4 POST_ID:829423USER_ID:12446
Topic:
C;SSL;https
In c programming, raw socket for http is worked well without using curl, but we will target
https site that will return http code 302.
How to send message through socket to https ?
Please advise
Author: test test replied at 2026-06-18 13:57:32
Be Reminded to set:
gcc -o basic basic.c -g -lpthread -ldl //to follows
gcc -o basic basic.c -g -lpthread -ldl -lssl -lcrypto
Expert: Wilson Edwards replied at 2026-06-18 02:36:56
sudo dnf install openssl-devel
sudo dnf install -y libcurl-devel gcc
Accepted Solution
Author: test test replied at 2026-06-17 10:16:09
500 points Excellent
You need install openssl
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
SSL * ssl;
then read the following socket function and call it
SSL ssl *sockinit('computer28.com');
char *headers = "GET / HTTP/1.1\r\n"
"Host: computer28.com\r\n"
"User-Agent: C-Socket-Client\r\n"
"Connection: close\r\n\r\n"; // Double CRLF ends the header block
n=SSL_write(ssl, headers, strlen(headers));//n>0
char response[4096];
int bytes = SSL_read(ssl, response, sizeof(response) - 1);
if (bytes > 0) {
response[bytes] = '\0';
//printf("==0=9=========response\n");
printf("Received:\n%s\n", response);
}
-----------------------------------------------------
SSL* sockinit(char *host){
//char *hostname = "computer28.com";
struct hostent *host2 = gethostbyname(hostname);
if (host2 == NULL) {herror("gethostbyname");return 1;}
// Extract the first resolved IP address
struct in_addr **addr_list = (struct in_addr **)host2->h_addr_list;
char *ip_str = inet_ntoa(*addr_list[0]);
// printf("Resolved IP: %s\n", ip_str);
//exit(0);
//https://www.scottklement.com/rpg/socktut/nonblocking.html
// http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html
//http://mff.devnull.cz/pvu/src/tcp/non-blocking-connect.c
//https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzab6/xnonblock.htm
//https://stackoverflow.com/questions/27014955/socket-connect-vs-bind
//http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch16lev1sec4.html
// http://www.cs.odu.edu/~cs779/spring09/lectures/nonblockingIO.html
//winsocks https://cboard.cprogramming.com/networking-device-communication/123660-nonblocking-sockets-c.html
//non-blocking and block definition: http://www.wangafu.net/~nickm/libevent-book/01_intro.html
//https://stackoverflow.com/questions/2004307/cant-create-socket-on-windows
//winsock http://www.binarytides.com/winsock-socket-programming-tutorial/
//set timeout https://stackoverflow.com/questions/2917881/how-to-implement-a-timeout-in-read-function-call
SSL *ssl;int n; int sockfd; struct sockaddr_in serv_addr;struct hostent *server;
// 1. Initialize OpenSSL
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
// 2. Create TCP Socket
const SSL_METHOD *method = TLS_client_method();
SSL_CTX *ctx = SSL_CTX_new(method);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server = gethostbyname(host);
if (server == NULL) {fprintf(stderr,"ERROR, no such host\n");exit(0);}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(443);
//serv_addr.sin_addr.s_addr = inet_addr(); // example.com
n=connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
//it may be different for n 's defination in 80 and 443 port
// 3. Attach TLS to Socket
ssl = SSL_new(ctx);
SSL_set_fd(ssl, sockfd);
SSL_connect(ssl); // Perform the TLS handshake
return ssl;
//return sockfd;
}
Expert: rcschroeder replied at 2026-06-17 10:08:41
To set up a raw socket in C to communicate with an HTTPS server and receive a 302 redirect response, you'll need to follow these steps:
**Step 1: Create a raw socket**
You can use the `socket()` function from the Linux kernel's network programming API (syscalls) to create a raw socket. The socket type is set using the `AF_INET` address family and the `SOCK_RAW` socket type.
```c
#include <sys/socket.h>
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
```
**Step 2: Set up the socket options**
To receive HTTPS traffic, you need to set up the socket options to intercept incoming HTTP requests. You'll use the `SETsockopt()` function with the `IPCV4` option to enable IPv4 and the `SO_RCVBUF` option to set the receive buffer size.
```c
int optval = 1;
size_t optlen = sizeof(optval);
setsockopt(sock, IPV4, SO_RCVBUF, &optval, optlen);
```
**Step 3: Set up the SSL/TLS layer**
To establish a secure connection to an HTTPS server, you'll need to set up an SSL/TLS layer. You can use the OpenSSL library to create a TLS client context.
```c
#include <openssl/ssl.h>
#include <openssl/err.h>
SSL_CTX *ctx;
```
**Step 4: Connect to the HTTPS server**
Using the established socket and SSL/TLS context, you'll connect to the HTTPS server. The `connect()` function takes the hostname or IP address of the server as an argument.
```c
int err = SSL_connect(ctx);
if (err != 1) {
// Handle connection error
}
```
**Step 5: Receive and process the response**
Once connected, you can receive data from the HTTPS server using the `recv()` function. Since the server returns a 302 redirect response, you'll need to parse the HTTP headers to extract the new URL.
```c
char buffer[1024];
int bytesReceived = recv(sock, buffer, 1024, 0);
if (bytesReceived < 0) {
// Handle receive error
}
// Parse HTTP headers to extract new URL
```
**Example Code**
Here's a simplified example code that demonstrates the above steps:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
int main() {
// Create raw socket
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
// Set up socket options
int optval = 1;
size_t optlen = sizeof(optval);
setsockopt(sock, IPV4, SO_RCVBUF, &optval, optlen);
// Create SSL/TLS context
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// Connect to HTTPS server
char hostname[] = "example.com";
int err = SSL_connect(ctx);
if (err != 1) {
printf("Connection error: %s", SSL_get_error(ctx, err));
return -1;
}
// Receive response from server
char buffer[1024];
int bytesReceived = recv(sock, buffer, 1024, 0);
if (bytesReceived < 0) {
printf("Receive error: %s", strerror(errno));
return -1;
}
// Parse HTTP headers to extract new URL
char* LocationHeader = strstr(buffer, "Location:");
if (LocationHeader != NULL) {
printf("New URL: %s", LocationHeader + 9);
} else {
printf("No Location header found");
}
return 0;
}
```
Note that this example assumes a simplified scenario where the server returns a 302 redirect response with a `Location` header. You'll need to modify the code to handle more complex scenarios.
Also, keep in mind that raw sockets and SSL/TLS interactions are complex topics, and there are many security considerations and potential pitfalls to be aware of when working with them.
**Step 1: Create a raw socket**
You can use the `socket()` function from the Linux kernel's network programming API (syscalls) to create a raw socket. The socket type is set using the `AF_INET` address family and the `SOCK_RAW` socket type.
```c
#include <sys/socket.h>
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
```
**Step 2: Set up the socket options**
To receive HTTPS traffic, you need to set up the socket options to intercept incoming HTTP requests. You'll use the `SETsockopt()` function with the `IPCV4` option to enable IPv4 and the `SO_RCVBUF` option to set the receive buffer size.
```c
int optval = 1;
size_t optlen = sizeof(optval);
setsockopt(sock, IPV4, SO_RCVBUF, &optval, optlen);
```
**Step 3: Set up the SSL/TLS layer**
To establish a secure connection to an HTTPS server, you'll need to set up an SSL/TLS layer. You can use the OpenSSL library to create a TLS client context.
```c
#include <openssl/ssl.h>
#include <openssl/err.h>
SSL_CTX *ctx;
```
**Step 4: Connect to the HTTPS server**
Using the established socket and SSL/TLS context, you'll connect to the HTTPS server. The `connect()` function takes the hostname or IP address of the server as an argument.
```c
int err = SSL_connect(ctx);
if (err != 1) {
// Handle connection error
}
```
**Step 5: Receive and process the response**
Once connected, you can receive data from the HTTPS server using the `recv()` function. Since the server returns a 302 redirect response, you'll need to parse the HTTP headers to extract the new URL.
```c
char buffer[1024];
int bytesReceived = recv(sock, buffer, 1024, 0);
if (bytesReceived < 0) {
// Handle receive error
}
// Parse HTTP headers to extract new URL
```
**Example Code**
Here's a simplified example code that demonstrates the above steps:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
int main() {
// Create raw socket
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
// Set up socket options
int optval = 1;
size_t optlen = sizeof(optval);
setsockopt(sock, IPV4, SO_RCVBUF, &optval, optlen);
// Create SSL/TLS context
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// Connect to HTTPS server
char hostname[] = "example.com";
int err = SSL_connect(ctx);
if (err != 1) {
printf("Connection error: %s", SSL_get_error(ctx, err));
return -1;
}
// Receive response from server
char buffer[1024];
int bytesReceived = recv(sock, buffer, 1024, 0);
if (bytesReceived < 0) {
printf("Receive error: %s", strerror(errno));
return -1;
}
// Parse HTTP headers to extract new URL
char* LocationHeader = strstr(buffer, "Location:");
if (LocationHeader != NULL) {
printf("New URL: %s", LocationHeader + 9);
} else {
printf("No Location header found");
}
return 0;
}
```
Note that this example assumes a simplified scenario where the server returns a 302 redirect response with a `Location` header. You'll need to modify the code to handle more complex scenarios.
Also, keep in mind that raw sockets and SSL/TLS interactions are complex topics, and there are many security considerations and potential pitfalls to be aware of when working with them.