0

I have a client server program in C++ console application. I run the server first then run the client. The server should display the ip address from the client that's connected but it says 0.0.0.0

I'm guessing its this line that's causing the problem

getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
printf("Server: Receiving IP(s) from client: %s\n", inet_ntoa(ServerAddr.sin_addr)); 

in the client i set the ip address to 127.0.0.1

 ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
Karen123456
  • 327
  • 1
  • 3
  • 10
  • What does [`getsockname`](http://msdn.microsoft.com/en-gb/library/windows/desktop/ms738543(v=vs.85).aspx) return? – Peter Wood Mar 11 '13 at 09:40
  • For best results: Test the client on a different machine to the server. – johnsyweb Mar 11 '13 at 10:06
  • i am trying to run it on another computer but i have a problem with that. see here http://stackoverflow.com/questions/15297270/problems-with-running-exe-file-built-with-visual-studio-on-another-computer – Karen123456 Mar 11 '13 at 15:09

1 Answers1

1

You get the clients address in the sockaddr provided to accept. If you later want a connected clients socket address you should use getpeername (not getsockname).

You also should pass a valid and initialized variable as the length to getpeername (or getsockname):

int size = sizeof(SOCKADDR_IN);
getpeername(connectedsocket, (SOCKADDR*) &address, &size);

What you are doing now is attempting to get the local address of the server socket, but call it wrongly.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621