399

How to get the ip address of the accepted in-bound socket?

Question:

My question is:

Server will create a socket, bind to a given port and with address = INADDR_ANY.

listen() & accept() the new connection. Then, we can get the client's ip-address

from accept().

Now, I want to know the ip-address of the Server, since the host of the server has

multiple NIC on it.

How to know the ip-address of the network interface with which the accepted in-bound socket is from?

I tried getsockname, it gave me the port number, but the ip is all-zero.

Update: Here is the code:

<strong>Server.c (header files are removed)</strong>

int main(void) { struct sockaddr_in stSockAddr; int res, addr_len, SocketFD, ConnectFD; struct sockaddr_in addr; SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(-1 == SocketFD) { perror("can not create socket"); //exit(EXIT_FAILURE); return -1; } memset(&stSockAddr, 0, sizeof stSockAddr); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(49335); stSockAddr.sin_addr.s_addr = INADDR_ANY; if(-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof stSockAddr)) { perror("error bind failed"); close(SocketFD); return -1; } printf("going to listen!\n"); if(-1 == listen(SocketFD, 10)) { perror("error listen failed"); close(SocketFD); //exit(EXIT_FAILURE); return -1; } ConnectFD = accept(SocketFD, NULL, NULL); if(0 > ConnectFD) { perror("error accept failed"); close(SocketFD); //exit(EXIT_FAILURE); return -1; } addr.sin_family = AF_INET; res = getsockname (ConnectFD, (struct sockaddr *)&addr, &addr_len); // if you remove the following comment, that means, if you call // two times of getsockname, the result will be correct. //res = getsockname (ConnectFD, (struct sockaddr *)&addr, &addr_len); printf("addr:%x\n", addr.sin_addr.s_addr); while(1) { if (getchar() == 'q') break; } close(ConnectFD); close(SocketFD); return 0; }

<strong>Below is client.c:</strong>

int main(void) { struct sockaddr_in stSockAddr; int Res; int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (-1 == SocketFD) { perror("cannot create socket"); exit(EXIT_FAILURE); } memset(&stSockAddr, 0, sizeof stSockAddr); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(49335); Res = inet_pton(AF_INET, "192.168.1.102", &stSockAddr.sin_addr); if (0 > Res) { perror("error: first parameter is not a valid address family"); close(SocketFD); exit(EXIT_FAILURE); } else if (0 == Res) { perror("char string (second parameter does not contain valid ipaddress"); close(SocketFD); exit(EXIT_FAILURE); } if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof stSockAddr)) { perror("connect failed"); close(SocketFD); exit(EXIT_FAILURE); } /* perform read write operations ... */ printf("client sockfd is successful\n"); while(1) { if (getchar() == 'q') break; } shutdown(SocketFD, SHUT_RDWR); close(SocketFD); return 0; }

Answer1:

Use getsockname(2) on the socket returned from accept(2), <em>not</em> the socket returned from bind(2).

Answer2:

getsockname() gets name of passed socket. in this example, you pass the socket which is created by accept function in server. this socket is in server side, so it's name & address is related to server side.

if you want to know <strong>"who was connected to me"</strong> you must use getpeername() instead of getsockname.

good luck

Answer3:

<blockquote>

if you remove the following comment, that means, if you call two times of getsockname, the result will be correct. res = getsockname (ConnectFD, (struct sockaddr *)&addr, &addr_len);

</blockquote>

you must init addr_len.

addr_len = sizeof(addr);

Recommend

  • C++ server and java client chat [closed]
  • Find.Execute with confirmation dialog
  • Inject Javascript code into a web page
  • Two way binding not working in ng-bootstrap radio button in an angular reactive form
  • Get method parameters with specific annotation in aspect in scala using java reflection
  • Multiple custom authentication with spring security
  • Joining across databases with dbplyr
  • ASP.NET: replacing UpdatePanel with Jquery?
  • autotest on ubuntu does nothing
  • WSO2 Identity Server + Rest STS Client (without ESB)
  • MVC3 Extension for ValidatorMessage
  • playing mp3 from nsbundle
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • Add checkbox dynamically using angular 2
  • Java Collections.shuffle() weird behaviour [closed]
  • How to redirect into different page by user type in php and mysql
  • Getting the type of a “Type” in C# reflection
  • getting the class name of an HTML tag using XPATH
  • Can I read another applications memory?
  • Possible to set default CloudKit container not based on application name?
  • Django, uWSGI & nginx: Process dies for “no reason”
  • What Exception is thrown on timeout?
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • LinkedIn API: Access Denied when getting Access Token
  • Print Longitude and Latitude in textbox Android
  • Cloud Code: Creating a Parse.File from URL
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • Make checkout phone field optional for specific countries in WooCommerce
  • Sign a Pdf using custom digital signature in Java
  • Using Service Component Runtime
  • Spring Boot not autowiring @Repository
  • ReferenceError: TextEncoder is not defined
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}