getservbyname

The Windows Sockets getservbyname function retrieves service information corresponding to a service name and protocol.

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

Parameters

name
[in] A pointer to a null-terminated service name.
proto
[in] An optional pointer to a null-terminated protocol name. If this pointer is NULL, getservbyname returns the first service entry where name matches the s_name member of the SERVENT structure or the s_aliases member of the SERVENT structure. Otherwise, getservbyname matches both the name and the proto.

Remarks

The getservbyname function returns a pointer to the SERVENT structure containing the name(s) and service number that match the string in the name parameter. All strings are null-terminated.

The pointer that is returned points to the SERVENT structure allocated by the Windows Sockets library. The application must never attempt to modify this structure or to free any of its components. Furthermore only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

Return Values

If no error occurs, getservbyname returns a pointer to the SERVENT structure. Otherwise, it returns a NULL pointer and a specific error number can be retrieved by calling WSAGetLastError.

Error Codes

WSANOTINITIALISED A successful WSAStartup must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAHOST_NOT_FOUND Authoritative Answer Service not found.
WSATRY_AGAIN Non-Authoritative Service not found, or server failure.
WSANO_RECOVERY Nonrecoverable errors, the services database is not accessible.
WSANO_DATA Valid name, no data record of requested type.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEINTR A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall.

QuickInfo

  Windows NT: Yes
  Windows: Yes
  Windows CE: Unsupported.
  Header: Declared in winsock2.h.
  Import Library: Link with ws2_32.lib.

See Also

getservbyport, WSAAsyncGetServByName