开发者

linux system call getpeername c++

In my c++ application I'm using getpeername system call and it return 0.0.0.0 in the var sa. there is no error in errno, and the return code is 0.

here is the code:

int GetSock(int sock)
{
    struct sockaddr_storage ss;
    socklen_t salen = sizeof(ss);
    struct sockaddr *sa;
    struct addrinfo hints, *paddr, *paddrp;

  开发者_StackOverflow  sa = (struct sockaddr *)&ss;

    if (getpeername(sock, sa, &salen) != 0) {
        error = errno;
        return -1;
    }
}

note: I'm compiling this code with GCC in eclipse. any help?

thanks!


        error = errno;
        return -1;
    }
    /* Did you mean to return something right here? */
}


Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and errno set to indicate the error.

EDIT: Check what is the value of sock is. getpeername only extracts and stores the peer address of the socket, in this case sock, and stores it in sa. If your socket isn't created or your socket isn't bound to a named socket, what sa points to is unspecified and this may be your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜