Khalil Beldi
Khalil Beldi

Reputation: 39

Why inet_ntoa always returns false IP adresse (socket programming inc )

i have simple client/server app with socket , so the client connect to the server with the server ip , i want to return ip of connected client with inet_ntoa but it always returns false ip of connected client. why ? and how to solve it .

i want the server to return true ip of machine connected to scan open ports later

Upvotes: 0

Views: 200

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

struct sockaddr_in addr_remote;
...
connfd = accept(sockfd, (SA*)&cli, &len); 
...
printf( " Welcome %s " , inet_ntoa(addr_remote.sin_addr));

You use inet_ntoa with addr_remote. addr_remote is never set in your code and therefore the contents is kind of unpredictable. You probably intended to use addr_remote inside accept but you've used cli there.

Upvotes: 2

Related Questions