Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function retrieves the name of the peer to which a socket is connected.

Syntax

int getpeername(
  SOCKET 
s,
  struct sockaddr FAR* 
name,
  int FAR* 
namelen
);

Parameters

s

[in] Descriptor identifying a connected socket.

name

[out] Structure that receives the name of the peer.

namelen

[in, out] Pointer to the size of the name structure.

Return Value

If no error occurs, this function returns zero. If an error occurs, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

The following table shows the possible error codes.

Error code Description

WSANOTINITIALISED

A successful WSAStartupcall must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAEFAULT

The nameor namelenparameter is not a valid part of the user address space, or the namelenparameter is too small.

WSAEINPROGRESS

A blocking Winsock call is in progress, or the service provider is still processing a callback function.

WSAENOTCONN

The socket is not connected.

WSAENOTSOCK

The descriptor is not a socket.

Remarks

This function retrieves the name of the peer connected to socket sand stores it in the sockaddrstructure identified by name. The function can be used only on a connected socket. For datagram sockets, only the name of a peer specified in a previous connect (Windows Sockets)call will be returned. Any name specified by a previous sendtocall will not be returned by getpeername.

On call, the namelenargument contains the size of the namebuffer, in bytes. On return, the namelenparameter contains the actual size in bytes of the namereturned.

Requirements

Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also