Operating System - HP-UX
1826454 Members
3208 Online
109692 Solutions
New Discussion

Re: getpeername() return 0 but with IP address "0.0.0.0"

 
Fei Rao
Advisor

getpeername() return 0 but with IP address "0.0.0.0"

Hi all,

The server is listening on a specified port. When a connection request comes, the server accepts the request and then fork() a child. Then the server call getpeername() and getsockname() to get the client's and server's addresses. Unfortunately both two calls succeed and return addresses "0.0.0.0" and ports "0".

I looked up the manual and search through google, but didn't found any solutions. Do you have any ideas about this problem?

HP-UX B.11.23 9000/800 on PA-RISC

Fei
Life is easy. Life is hard.
5 REPLIES 5
Arunvijai_4
Honored Contributor

Re: getpeername() return 0 but with IP address "0.0.0.0"

Hi Fei,

Check for IPv6 is enabled on your server ?

# ifconfig will show it.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Fei Rao
Advisor

Re: getpeername() return 0 but with IP address "0.0.0.0"

No. Just IPv4.

Here's the source code fragment:

struct sockaddr_in peerAddr, servAddr;
int addrlen = sizeof (sockaddr_in);

if (-1 == (getpeername(sockFd, (struct sockaddr *)&peerAddr, &addrlen)))
exitError("getpeername()");

addrlen = sizeof (sockaddr_in);
if (-1 == (getsockname(sockFd, (struct sockaddr *)&servAddr, &addrlen)))
exitError("getsockname()");

cout << "Peer IP: " << inet_ntoa(peerAddr.sin_addr) << " PORT: "<< ntohs(peerAddr.sin_port) << endl;

cout << "Serv IP: " << inet_ntoa(servAddr.sin_addr) << " PORT: " << ntohs(servAddr.sin_port) << endl;

Here's the output:
Peer IP: 0.0.0.0 PORT: 0
Serv IP: 0.0.0.0 PORT: 0
Life is easy. Life is hard.
Torsten.
Acclaimed Contributor

Re: getpeername() return 0 but with IP address "0.0.0.0"

Many many years ago for me, but I guess to remember I used inet_ntoa(peerAddr.sin_addr.s_addr).
I'm not sure, because I didn't write any C stuff for years.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Fei Rao
Advisor

Re: getpeername() return 0 but with IP address "0.0.0.0"

Actually, the whole structure peerAddr and servAddr are filled with '\0' as if I use memset() to set all bits to 0.
Life is easy. Life is hard.
rick jones
Honored Contributor

Re: getpeername() return 0 but with IP address "0.0.0.0"

A couple things - accept() will also return the remote addressing information, so you can eliminate the per-connection getpeername() call :)

You might consider doing an memset to something other than "0" on the sockaddr so you can see if the calls are setting them to zero or simply not setting them at all.

Also, a tusc trace of the calls might be a good idea. Make it a verbose one.
there is no rest for the wicked yet the virtuous have no pillows