简体   繁体   English

UDP套接字广播和ifaddrstruct

[英]UDP Socket broadcast and ifaddrstruct

I have a problem with the client side of an application I have to develop. 我必须开发的应用程序的客户端存在问题。

The first phase I have to implement is a "scan" of the network, searching for other users. 我必须实现的第一阶段是对网络进行“扫描”,搜索其他用户。 I decided to use UDP sockets, and they work properly. 我决定使用UDP套接字,它们可以正常工作。 If I use the broadcast ip address (getnamebyhost("192.168.1.255")) it works fine. 如果我使用广播IP地址(getnamebyhost(“ 192.168.1.255”)),则可以正常工作。 The problem is that the application have to work on different networks, and I don't know the ip address (192.168 or 10.0 or else). 问题是该应用程序必须在不同的网络上工作,并且我不知道IP地址(192.168或10.0或其他)。 I have to learn the address, so I found on the net to use getifaddr(). 我必须学习地址,所以我在网上发现要使用getifaddr()。 Here's the code: 这是代码:

#include <stdio.h>      
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h> 
#include <string.h> 
#include <arpa/inet.h>
#include <string.h>


int FindMyIp();
int IsConnected();

char *myIpAddr, *myMask, *myBroad;

int main (int argc, const char * argv[]) {

    int checkConnection=0;
    checkConnection = IsConnected();
    while (checkConnection == 0) {
        printf("Checking again..\n");
        checkConnection = IsConnected();
        sleep(2);
    }
    checkConnection=0;
    printf("\nConnected to a new network..\n");
    while (checkConnection == 0) {
        checkConnection = FindMyIp();
        if (checkConnection==0) { sleep(2); }
    }
    printf("My IP is %s\n", myIpAddr);
    printf("My Mask is %s\n", myMask);
    printf("The Broadcast IP is %s\n", myBroad);
    return 0;
}


int IsConnected()
{
    if (system("ping -c 1 www.google.com")){
        return 0;
    }
    else {  
        return 1;
    }
}

int FindMyIp()
{
    struct ifaddrs * ifAddrStruct=NULL;
    struct ifaddrs * ifa=NULL;
    void * tmpAddrPtr=NULL;
    printf("Acquiring interface information.. ");
    getifaddrs(&ifAddrStruct);
    printf("Checking for wanted interface..\n");
    int i=0, connected=0;
    ifa = ifAddrStruct;
    while (i!=1) {
        if ((ifa ->ifa_addr->sa_family==AF_INET)&&(ifa->ifa_name[0]=='e')&&(ifa->ifa_name[1]=='n')&&(ifa->ifa_name[2]=='1')) {
            i=1;
            connected=1;
        }
        else {
            if (ifa->ifa_next == NULL) {
                printf("error");
                return connected;
            }
            ifa = ifa->ifa_next;
        }
    }
    char addressBuffer1[INET_ADDRSTRLEN];
    char addressBuffer2[INET_ADDRSTRLEN];
    char addressBuffer3[INET_ADDRSTRLEN];
    tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer1, INET_ADDRSTRLEN);
    myIpAddr = addressBuffer1;
    tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer2, INET_ADDRSTRLEN);
    myMask = addressBuffer2;
    tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_broadaddr)->sin_addr;
    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer3, INET_ADDRSTRLEN);
    myBroad = addressBuffer3;
    return connected;
}

So in myBroad I should have saved the broadcast ip address, and it's, I can print it with printf, but when I pass it to the socket by: struct hostent *hptr = gethostbyname(myBroad); 因此,在myBroad中,我应该已经保存了广播ip地址,可以使用printf打印它,但是当我通过以下方式将其传递给套接字时:struct hostent * hptr = gethostbyname(myBroad); it won't work! 它行不通! Where's the error? 错误在哪里?

Thanks 谢谢

myIpAddr = addressBuffer1; myIpAddr = addressBuffer1;

Here you're setting a global pointer to point to a local buffer inside the FindMyIP function. 在这里,您要设置一个全局指针,以指向FindMyIP函数内部的本地缓冲区。 That array is gone when the function ends. 函数结束时该数组消失了。

Just make all your global char *myIpAddr, *myMask, *myBroad; 只需将所有全局char *myIpAddr, *myMask, *myBroad; an array, and place the strings directly into those, instead of the local addressBuffer1/2/3 一个数组,然后将字符串直接放入其中,而不是本地addressBuffer1 / 2/3

char *myBroad is just an address holder which looses its value after the end of function. char * myBroad只是一个地址持有者,在函数结束后会释放其值。 You actually need to create a string array and pass it to further functions of socket API. 实际上,您需要创建一个字符串数组,并将其传递给套接字API的其他功能。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM