Windows API Reference

Microsoft Learn

Protocol-Independent Functions

The following functions are protocol-independent and can be used with any Winsock protocol provider.

gethostname

Retrieves the standard host name for the local computer.

int gethostname(
  _Out_writes_bytes_to_(nameLength, (_String_length_(buffer) + 1)) char *nameBuffer,
  _In_ int nameLength
);

Parameters

Return Value

If no error occurs, gethostname returns zero. Otherwise, it returns a SOCKET_ERROR value, and a specific error code can be retrieved by calling WSAGetLastError.

gethostbyaddr

Retrieves the name of a host from its address.

struct hostent *gethostbyaddr(
  _In_reads_bytes_(len) const char *addr,
  _In_ int                      len,
  _In_ int                      type
);

Parameters

Return Value

If successful, gethostbyaddr returns a pointer to a hostent structure. Otherwise, it returns NULL. The returned structure is allocated by Winsock and may be overwritten by subsequent calls to the same function in the same thread. An application should not free this resource. If a temporary buffer is required, the application should allocate and manage its own buffer.

gethostbyname

Retrieves the address of a host from its name.

struct hostent *gethostbyname(
  _In_ const char *name
);

Parameters

Return Value

If successful, gethostbyname returns a pointer to a hostent structure. Otherwise, it returns NULL. The returned structure is allocated by Winsock and may be overwritten by subsequent calls to the same function in the same thread. An application should not free this resource. If a temporary buffer is required, the application should allocate and manage its own buffer.

getservbyname

Retrieves the port number for a specified service and protocol.

struct servent *getservbyname(
  _In_ const char *name,
  _In_ const char *proto
);

Parameters

Return Value

If successful, getservbyname returns a pointer to a servent structure. Otherwise, it returns NULL. The returned structure is allocated by Winsock and may be overwritten by subsequent calls to the same function in the same thread.

getservbyport

Retrieves the name of a service associated with a specified port and protocol.

struct servent *getservbyport(
  _In_ int    port,
  _In_ const char *proto
);

Parameters

Return Value

If successful, getservbyport returns a pointer to a servent structure. Otherwise, it returns NULL. The returned structure is allocated by Winsock and may be overwritten by subsequent calls to the same function in the same thread.