如何更改地址?

发布于 2025-01-10 03:45:01 字数 1117 浏览 0 评论 0原文

我在将输入数据发送到本地服务器的代码中添加了一个简单的代码。 现在我不知道如何将地址更改为ngrok。

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int n,t;
char hello[10];
int main(int argc, char const *argv[]){
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    
    char buffer[1024] = {0};
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    
    // Convert IPv4 and IPv6 addresses from text to binary form
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }
    
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }
    send(sock , hello , strlen(hello) , 0 );
    
    return 0;
}

send函数将输入数据发送到127.0.0.1:8080 但不知道怎么改ip。

I added a simple code to the code that sends input data to the local server.
Now I do not know how to change the address to ngrok.

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int n,t;
char hello[10];
int main(int argc, char const *argv[]){
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    
    char buffer[1024] = {0};
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    
    // Convert IPv4 and IPv6 addresses from text to binary form
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }
    
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }
    send(sock , hello , strlen(hello) , 0 );
    
    return 0;
}

send function send input data to 127.0.0.1:8080
But I don't know how to change ip.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2025-01-17 03:45:01

Ngrok 在您的电脑上本地运行,因此他在 127.0.0.1 上运行,Ngrok 在您的电脑上不是 root,因此当您不是 root 时,他必须使用默认的 http 端口 8080。

我邀请您查看 ngrok 文档:
https://ngrok.com/docs

本教程:
https://ngrok.com/docs/guides/ how-to-set-up-a-custom-domain

那么你必须改变

// 将 IPv4 和 IPv6 地址从文本形式转换为二进制形式
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)

// 将 IPv4 和 IPv6 地址从文本形式转换为二进制形式
if(inet_pton(AF_INET, "new_Ngrok_IP_adresse", &ser​​v_addr.sin_addr)<=0)"

Ngrok is running in local on your pc so his running on 127.0.0.1, Ngrok is not root on your pc so he has to use the port 8080 default for http when your not root.

I invite you to check the ngrok documentation:
https://ngrok.com/docs

And this tutorial:
https://ngrok.com/docs/guides/how-to-set-up-a-custom-domain

Then you have to change

// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)

to

// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "new_Ngrok_IP_adresse", &serv_addr.sin_addr)<=0)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文