Note that there are some explanatory texts on larger screens.

plurals
  1. POUDP Socket broadcast and ifaddrstruct
    primarykey
    data
    text
    <p>I have a problem with the client side of an application I have to develop.</p> <p>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. If I use the broadcast ip address (getnamebyhost("192.168.1.255")) it works fine. 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). I have to learn the address, so I found on the net to use getifaddr(). Here's the code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/types.h&gt; #include &lt;ifaddrs.h&gt; #include &lt;netinet/in.h&gt; #include &lt;string.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;string.h&gt; 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(&amp;ifAddrStruct); printf("Checking for wanted interface..\n"); int i=0, connected=0; ifa = ifAddrStruct; while (i!=1) { if ((ifa -&gt;ifa_addr-&gt;sa_family==AF_INET)&amp;&amp;(ifa-&gt;ifa_name[0]=='e')&amp;&amp;(ifa-&gt;ifa_name[1]=='n')&amp;&amp;(ifa-&gt;ifa_name[2]=='1')) { i=1; connected=1; } else { if (ifa-&gt;ifa_next == NULL) { printf("error"); return connected; } ifa = ifa-&gt;ifa_next; } } char addressBuffer1[INET_ADDRSTRLEN]; char addressBuffer2[INET_ADDRSTRLEN]; char addressBuffer3[INET_ADDRSTRLEN]; tmpAddrPtr=&amp;((struct sockaddr_in *)ifa-&gt;ifa_addr)-&gt;sin_addr; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer1, INET_ADDRSTRLEN); myIpAddr = addressBuffer1; tmpAddrPtr=&amp;((struct sockaddr_in *)ifa-&gt;ifa_netmask)-&gt;sin_addr; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer2, INET_ADDRSTRLEN); myMask = addressBuffer2; tmpAddrPtr=&amp;((struct sockaddr_in *)ifa-&gt;ifa_broadaddr)-&gt;sin_addr; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer3, INET_ADDRSTRLEN); myBroad = addressBuffer3; return connected; } </code></pre> <p>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); it won't work! Where's the error?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload