I am working on a server client project on Qt. The server is running in a machine with more than one network interface. The design is such that the client will discover the server automatically. ie the client will broadcast its IP to a network the server get that message and sends back the server's IP. The problem now is that when I try to get the IP in the server, There are more than 1 IP. How to get the IP of the interface through which server have received the message?
Asked
Active
Viewed 1,514 times
3
-
1Why do you need to know the server interface's IP? If you get the broadcast from the client on multiple intefaces, just respond to all of them. The client will need to filter out multiple responses and just pick one. – unwind Feb 21 '14 at 11:08
-
1You almost never need to send your own IP address over the net. Every IP packet already contains a usable source address. The other side only needs to getpeername or recvfrom. – n. m. could be an AI Feb 21 '14 at 11:09
-
@unwind The server is running in a machine which have multiple network. Client needs the a valid IP(ip in clients network) to communicate for further data. – jsaji Feb 21 '14 at 11:16
-
1@Griffin Yes, I got that. But if the server sees the broadcast from the client on multiple of its interfaces, then that proves that there is connectivity on each of those interfaces using the recieved IP(s) so the server can just respond to those, then. I still don't get what I'm missing. :/ Perhaps you can edit your question to clarify it. – unwind Feb 21 '14 at 11:37
1 Answers
0
This might be a solution for you
IPAddress FindLocalIPAddressOfIncomingPacket( senderAddr )
{
foreach( adapter in EnumAllNetworkAdapters() )
{
adapterSubnet = adapter.subnetmask & adapter.ipaddress;
senderSubnet = adapter.subnetmask & senderAddr;
if( adapterSubnet == senderSubnet )
{
return adapter.ipaddress;
}
}
}
How to get your own (local) IP-Address from an udp-socket (C/C++)
In order to get the incoming peer IP address you can use following solution in C
socklen_t len;
struct sockaddr_storage addr;
char ipstr[INET6_ADDRSTRLEN];
int port;
len = sizeof addr;
getpeername(s, (struct sockaddr*)&addr, &len);
// deal with both IPv4 and IPv6:
if (addr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
port = ntohs(s->sin_port);
inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
} else { // AF_INET6
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
port = ntohs(s->sin6_port);
inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr);
}
printf("Peer IP address: %s\n", ipstr);
-
This works (when it works) only for machines on the same local network, which is kinda restrictive. – n. m. could be an AI Feb 21 '14 at 11:20
-
1@Griffin Yes. Do not send your own IPs over the network. It is a wrong and broken way to communicate. Let the receiver find out the sender's address. – n. m. could be an AI Feb 21 '14 at 11:29
-
@n.m. The client application is based on JAVA(Android). Do you have any idea how to get the IP of the sender from UDP Packet?? – jsaji Feb 21 '14 at 11:33
-
http://stackoverflow.com/questions/11370024/java-display-clients-ip-and-port-numbers – n. m. could be an AI Feb 21 '14 at 11:45
-
Exactly the provided code snippet in answer depends on the network structure. – deimus Feb 21 '14 at 11:58
-
@Griffin check the updated answer for finding out the IP address of incoming socket connection – deimus Feb 21 '14 at 12:05
-
@deimus I want to get the IP of server in server application, not of client. Since there are more than network in the server. Sever needs to send a valid IP(Valid for that client who have send the message). ie the IP of the sever interface through which server app received the message – jsaji Feb 21 '14 at 12:22
-
@Griffin lets discuss in order 1. The local `IP` address on server side you can figure out like in first code example in my answer, e.g. iterating over the available network interfaces and doing the magic as described. 2. For doing it you need to know the `IP` address of your client, i.e. the `IP` address of incoming socket connection which you can do like in the second code snippet 3. The solution depends on the structure of your network, so be sure that your not using `NAT` or routing. – deimus Feb 21 '14 at 12:26
-
@deimus I got it now... can you give me an idea how to implement the first code?? i didn't see functions in linux – jsaji Feb 21 '14 at 14:37
-
@Griffin I'm pretty sure there are plenty of ways to do it. At least if the googling does not return enough results, you can try to check inside the source codes of `ifconfig` or `route` tools, which are publicly available. – deimus Feb 21 '14 at 23:30