複製鏈接
請複製以下鏈接發送給好友

getpeername()

鎖定
getpeername()是一個函數。獲取與套接口相連的端地址。
中文名
getpeername()
返回值
若無錯誤發生返回零
標    識
已連接套接口的描述字
獲    取
套接口相連的端地址

目錄

getpeername()簡述

獲取與套接口相連的端地址。
#include <winsock.h>
int PASCAL FAR getpeername(SOCKET s, struct sockaddr FAR* name,
int FAR* namelen);
s:標識一已連接套接口的描述字。
name:接收端地址的名字結構。
namelen:返回名字結構的長度。

getpeername()註釋

getpeername()函數用於從端口s中獲取與它捆綁的端口名,並把它存放在sockaddr類型的name結構中。它適用於數據報或流類套接口。
若無錯誤發生,getpeername()返回零。否則的話,返回SOCKET_ERROR,應用程序可通過WSAGetLastError()來獲取相應的錯誤代碼
錯誤代碼:
WSANOTINITIALISED:在使用此API之前應首先成功地調用WSAStartup()。
WSAENETDOWN:WINDOWS套接口實現檢測到網絡子系統失效。
WSAEFAULT:namelen參數不夠大。
WSAEINPROGRESS:一個阻塞的WINDOWS套接口調用正在運行中。
WSAENOTCONN 套接口未連接。
WSAENOTSOCK:描述字不是一個套接口。
參見:
bind(), socket(), getsockname().
Linux system
Name
getpeername - get name of connected peer socket
Synopsis
#include <sys/socket.h>
int getpeername(int s, struct sockaddr *name, socklen_t *namelen);
Description
getpeername() returns the name of the peer connected to socket s. The namelen parameter should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes). The name is truncated if the buffer provided is too small.
Return Value
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
Errors
EBADF
The argument s is not a valid descriptor.
EFAULT
The name parameter points to memory not in a valid part of the process address space.
EINVAL
namelen is invalid (e.g., is negative).
ENOBUFS
Insufficient resources were available in the system to perform the operation.
ENOTCONN
The socket is not connected.
ENOTSOCK
The argument s is a file, not a socket.
Conforming to
SVr4, 4.4BSD (the getpeername() function call first appeared in 4.2BSD), POSIX.1-2001.
Note
The third argument of getpeername() is in reality an int * (and this is what 4.x BSD and libc4 and libc5 have). Some POSIX confusion resulted in the present socklen_t, also used by glibc. See also accept(2).