Bonanza
Email
Enterprise Service
menu
Email
Enterprise Service
Submit
Basic information
Waiting for a reply
Your form has been submitted. We'll contact you in 24 hours.
Close
Home/ Blog/ How to use SOCKS5 proxy IP in C language

How to use SOCKS5 proxy IP in C language

Author:PYPROXY
2024-10-24 15:54:26

How to use SOCKS5 proxy IP in C language


In modern network environment, using proxy IP is a common technical means, especially when you need to access restricted resources or protect user privacy. SOCKS5 proxy is widely popular because of its flexibility and powerful functions. This article will introduce in detail how to use SOCKS5 proxy IP for network programming in C language to help developers effectively implement this function.


First, we need to understand the basic concept of SOCKS5 proxy. SOCKS (Socket Secure) is a network protocol that allows clients to communicate with target servers through proxy servers. SOCKS5 is the latest version of the protocol and supports multiple authentication methods and UDP forwarding. This makes SOCKS5 proxy very suitable for application scenarios that require high security and flexibility.


To use SOCKS5 proxy IP in C language, we usually need the following steps:

1. Establish a connection with SOCKS5 proxy: First, we need to create a TCP connection with the SOCKS5 proxy server.

2. Authorize: If the proxy server requires authentication, we need to provide the corresponding credentials.

3. Send a connection request: Send a connection request to the target server through the proxy server.

4. Perform data transfer: Once the connection is established, we can perform data transfer through the proxy server.

5. Close the connection: After completing the data transfer, close the connection with the proxy and the target server.


Next, we will implement these steps step by step.

1. Establish a connection with the SOCKS5 proxy

In C language, we can use the standard socket library to create a TCP connection. Here is a simple example code showing how to connect to a SOCKS5 proxy server:

```c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

int connect_to_socks5_proxy(const char proxy_ip, int proxy_port) {

int sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0) {

perror("Socket creation failed");

return -1;

}

struct sockaddr_in proxy_addr;

memset(&proxy_addr, 0, sizeof(proxy_addr));

proxy_addr.sin_family = AF_INET;

proxy_addr.sin_port = htons(proxy_port);

inet_pton(AF_INET, proxy_ip, &proxy_addr.sin_addr);

if (connect(sockfd, (struct sockaddr )&proxy_addr, sizeof(proxy_addr)) < 0) {

perror("Connection to proxy failed");

close(sockfd);

return -1;

}

return sockfd;

}

```


2. Perform authentication

SOCKS5 supports multiple authentication methods, including no authentication, username/password authentication, etc. Here, we will implement a connection without authentication. For situations where authentication is required, we can handle it accordingly according to the protocol specification.


3. Send a connection request

Once a connection is established with the SOCKS5 proxy, we need to send a connection request. The format of the connection request is as follows:

- Version number (1 byte, value 0x05)

- Number of authentication methods (1 byte)

- Authentication method list (N bytes, indicating supported authentication methods)

The following is a sample code for sending a connection request:

```c

void send_socks5_connect_request(int sockfd, const char target_ip, int target_port) {

unsigned char request[6];

request[0] = 0x05; // SOCKS version

request[1] = 0x01; // Number of authentication methods

request[2] = 0x00; // No authentication required

request[3] = 0x01; // Address type: IPv4

inet_pton(AF_INET, target_ip, &request[4]); // Target IP

request[4] = target_port >> 8; // Target port high byte

request[5] = target_port & 0xFF; // Target port low byte

send(sockfd, request, sizeof(request), 0);

}

```


4. Receive connection response

After sending the connection request, we need to receive the response from the proxy server to confirm whether the connection is successful. The format of the response is as follows:

- Version number (1 byte)

- Status code (1 byte, 0x00 indicates success)

- Reserved field (1 byte)

- Address type (1 byte)

- Target address (N bytes)

- Target port (2 bytes)

The following is a sample code to receive the response:

```c

void receive_socks5_connect_response(int sockfd) {

unsigned char response[10];

recv(sockfd, response, sizeof(response), 0);

if (response[1] == 0x00) {

printf("Connected successfully through SOCKS5 proxy.\n");

} else {

printf("Failed to connect through SOCKS5 proxy. Error code: %d\n", response[1]);

}

}

```


5. Data transfer

Once the connection is established, you can transfer data with the target server through the proxy server. Here is a simple example of sending and receiving data:

```c

void send_data(int sockfd, const char data) {

send(sockfd, data, strlen(data), 0);

}

void receive_data(int sockfd) {

char buffer[1024];

int bytes_received = recv(sockfd, buffer, sizeof(buffer) - 1, 0);

if (bytes_received > 0) {

buffer[bytes_received] = '\0'; // Null-terminate the buffer

printf("Received: %s\n", buffer);

}

}

```


6. Close the connection

After completing the data transfer, it is a good habit to close the connection with the proxy and the target server:

```c

void close_connection(int sockfd) {

close(sockfd);

}

```


Complete Example

Combining all the above steps, we can implement a complete SOCKS5 proxy client. The following is a simple example program: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> int connect_to_socks5_proxy(const char proxy_ip, int proxy_port); void send_socks5_connect_request(int sockfd, const char target_ip, int target _port); void receive_socks5_connect_response(int sockfd); void send_data(int sockfd, const char data); void receive_data(int sockfd); void close_connection(int sockfd); int main() { const char proxy_ip = "127.0.0.1"; // SOCKS5 proxy IP int proxy_port = 1080; // SOCKS5 proxy port

const char target_ip = "93.184.216.34"; // Target IP (e.g. example.com)

int target_port = 80; // Target port

int sockfd = connect_to_socks5_proxy(proxy_ip, proxy_port);

if (sockfd < 0) {

return 1;

}

send_socks5_connect_request(sockfd, target_ip, target_port);

receive_socks5_connect_response(sockfd);

// Send HTTP request

const char http_request = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n";

send_data(sockfd, http_request);

receive_data(sockfd);

close_connection(sockfd);

return 0;

}

```


Using SOCKS5 proxy IP for network programming in C language is not complicated. You just need to follow the protocol specifications and implement the corresponding functions. By establishing a connection with the proxy, sending connection requests, receiving responses, and performing data transmission, developers can take full advantage of the SOCKS5 proxy. I hope this article can provide you with valuable guidance and help you successfully implement the use of SOCKS5 proxy in actual projects.