# 5.10. getpeername()－你是谁？

这个函数很简单。\
\
它太简单了，我几乎不想给它一个自己的章节，虽然还是给了。\
\
getpeername() 函数会告诉你另一端连接的 stream socket 是谁，函数原型如下：<br>

```c
#include <sys/socket.h>
int getpeername(int sockfd, struct sockaddr *addr, int *addrlen);
```

*sockfd* 是连接的 stream socket 之 descriptor，*addr* 是指向 struct sockaddr［或 struct sockaddr\_in］的指针，这个数据结构储存了连线另一端的资料，而 *addrlen* 则是指向 int 的指针，应该将它初始化为 sizeof \*addr 或 sizeof(struct sockaddr)。\
\
函数在错误时返回 -1，并设置相对的 errno。\
\
一旦你取得了它们的地址，你就可以用 inet\_ntop()丶getnameinfo() 或 gethostbyaddr() 印出或取得更多的资料。不过你无法取得它们的登录帐号。\
\
［好好好，如果另一台电脑运行的是 ident daemon 就可以。］然而，这个已经超出本教程的范围，更多资料请参考 RFC 1413 \[19]。\
\[19] <http://tools.ietf.org/html/rfc1413>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://beej-zhcn.netdpi.net/system_call/getpeernameff0d_ni_shi_shui_ff1f.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
