从 C 套接字中提取 IP 地址

发布于 2024-07-26 22:10:58 字数 2085 浏览 10 评论 0原文

好的,我对使用 C 套接字仍然很陌生,但我想知道是否有一种方法可以通过运行setsockopt 来提取 IP 地址? 如果您看一下下面的代码,我的多播套接字中的所有内容都已准备好发送,除了定义变量 mc_addr(它是我的 IP 地址)。

我是否做错了什么真正值得注意的事情? 如果是这样,请随时告诉我或提出您的建议。 不过现在,我主要关心的是变量 mc_addr 的填写。

我以前从未用 C 编程(只是 python、C++,从今年夏天开始,我开始使用 Objective-C),所以这就是为什么我不知道有关 C 语言的所有知识并正在寻求帮助。

文件.h

#define MYPORT 5673         /* port for our multicast socket */

int sock;                   /* socket descriptor */
char send_str[MAX_LEN];     /* string to send */
struct sockaddr_in mc_addr; /* socket address structure */
unsigned int send_len;      /* length of string to send */
char* mc_addr_str;          /* multicast IP address */
unsigned short mc_port;     /* multicast port */
unsigned char mc_ttl;       /* time to live (hop count) */

文件.c

mc_port = MYPORT;
/* create a socket for sending to the multicast address  */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    NSLog(@"ERROR: broadcastMessage - socket() failed");
    return 1;
}

/* set the TTL (time to live/hop count) for the send */
if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, 
        (void*) &mc_ttl, sizeof(mc_ttl))) < 0) {
    NSLog(@"ERROR: broadcastMessage - setsockopt() failed");
    return 1;
}

// define the IP address we will be using
mc_addr = // ???

/* construct a multicast address structure */
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family      = AF_INET;
mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
mc_addr.sin_port        = htons(mc_port);

/* clear send buffer */
memset(send_str, 0, sizeof(send_str));

while (fgets(send_str, MAX_LEN, stdin)) {
    send_len = strlen(send_str);

    /* send string to multicast address */
    if ((sendto(sock, send_str, send_len, 0, 
            (struct sockaddr *) &mc_addr, sizeof(mc_addr))) != send_len) {
        NSLog(@"ERROR: broadcastMessage - sendto() sent incorrect number of bytes");
        return 1;
    }

    /* clear send buffer */
    memset(send_str, 0, sizeof(send_str));
}

close(sock);
return 0;

Ok, I'm still new to using C sockets, but I was wondering if there is a way to extract the IP address adding running setsockopt? If you'll look at my code below, I have everything in my multicast sockets ready to send except for defining the variable mc_addr which is my IP address.

Am I doing anything wrong that's real noticeable? If so, feel free to let me know or offer your suggestions. Right now though, I'm mainly concerned about getting the variable mc_addr filled out.

I've never programmed in C before (just python, C++, and since the beginning of this summer, Objective-C) , so this is why I don't know EVERYTHING there is to know about the C language and am seeking help.

File.h

#define MYPORT 5673         /* port for our multicast socket */

int sock;                   /* socket descriptor */
char send_str[MAX_LEN];     /* string to send */
struct sockaddr_in mc_addr; /* socket address structure */
unsigned int send_len;      /* length of string to send */
char* mc_addr_str;          /* multicast IP address */
unsigned short mc_port;     /* multicast port */
unsigned char mc_ttl;       /* time to live (hop count) */

File.c

mc_port = MYPORT;
/* create a socket for sending to the multicast address  */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    NSLog(@"ERROR: broadcastMessage - socket() failed");
    return 1;
}

/* set the TTL (time to live/hop count) for the send */
if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, 
        (void*) &mc_ttl, sizeof(mc_ttl))) < 0) {
    NSLog(@"ERROR: broadcastMessage - setsockopt() failed");
    return 1;
}

// define the IP address we will be using
mc_addr = // ???

/* construct a multicast address structure */
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family      = AF_INET;
mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
mc_addr.sin_port        = htons(mc_port);

/* clear send buffer */
memset(send_str, 0, sizeof(send_str));

while (fgets(send_str, MAX_LEN, stdin)) {
    send_len = strlen(send_str);

    /* send string to multicast address */
    if ((sendto(sock, send_str, send_len, 0, 
            (struct sockaddr *) &mc_addr, sizeof(mc_addr))) != send_len) {
        NSLog(@"ERROR: broadcastMessage - sendto() sent incorrect number of bytes");
        return 1;
    }

    /* clear send buffer */
    memset(send_str, 0, sizeof(send_str));
}

close(sock);
return 0;

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

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

发布评论

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

评论(2

笑脸一如从前 2024-08-02 22:10:58

对我来说看起来是正确的 - 如果您有“定义我们将使用的 IP 地址”,您应该只设置 mc_addr_str (mc_addr 本身是根据 mc_addr_str 用您已有的代码填写的)。 类似于:

mc_addr_str = "239.20.30.40";

这实际上取决于您选择的多播地址 - 我刚刚从“管理范围”范围中选择的多播地址。 您的客户端和服务器只需事先达成一致(或以其他方式协商,例如客户端单播联系服务器以询问他们应该订阅的多播地址是什么)。

此网站可能会有所帮助。

Looks correct to me - where you have the "define the IP address we will be using" you should just set mc_addr_str (mc_addr itself is filled out with the code you already have there, based on mc_addr_str). Something like:

mc_addr_str = "239.20.30.40";

It's really up to you what multicast address you choose - the one I've put there I just picked out of the "Administratively scoped" range. Your clients and server just have to agree on it beforehand (or negotiate it some other way, like the clients contact the server unicast to ask what the multicast address they should subscribe to is).

This site may help.

夜吻♂芭芘 2024-08-02 22:10:58

这是一个 C 语言的多播客户端示例,另一个 此处
第一个示例中的相关内容是,

saddr.sin_addr.s_addr = inet_addr("226.0.0.1"); //your mcast IP address

我不确定在不了解 C 的情况下如何使用 C++ 进行编程。

Here is a Multicast client example in C and, another here.
The relevant line from the first example is

saddr.sin_addr.s_addr = inet_addr("226.0.0.1"); //your mcast IP address

I am not sure how you could have been programming in C++ without understanding C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文