×
Namespaces

Variants
Actions

How to get service entry

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: polaka_sr (16 Jun 2008)
Last edited: hamishwillee (27 Jul 2012)

The getservent, getservbyname, and getservbyport functions each return a pointer to an object with the following structure containing the broken-out fields of a line in the network services database, c:/sys/data/services.

struct servent {
char *s_name; /* official name of service */
char **s_aliases; /* alias list */
int s_port; /* port service resides at */
char *s_proto; /* protocol to use */
};

The getservbyname and getservbyport functions sequentially search from the beginning of the file until a matching protocol name or port number (which must be specified in network byte order) is found, or until EOF is encountered. If a protocol name is also supplied (non- NULL ), searches must also match the protocol.

Following is the usage of getservbyname, getservbyport, getservent, retservent and endservent:

#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
Char *service=”http”;
Char *protocol=”tcp”:
Struct servent *p=0;
 
p = getservbyname(service,protocol);
if(p!=NULL)
printf(“service not supported:);
else
printf(“Service supported”);
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
struct servent *p;
char *protocol=”tcp”;
int port;
port=htons(80);
p=(port,protocol);
if(p)
{
Printf(“port is assigned”);
else
printf(“port is not assigned”);
}


#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
Int main()
{
struct servent *p;
p=getservent();
if(p)
printf(“getservent successful”);
else
printf(“getservent failed”);
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
int stayopen=1;
retservent(stayopen):
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
struct servent *p;
p=getservent();
if(p)
endservent();
return 0;
}


Null pointer (0) returned on EOF or error.


Wiki Links

Generic functions for system database functions

This page was last modified on 27 July 2012, at 07:17.
34 page views in the last 30 days.
×