Name and address conversion getservbyname and Getservbyport functions

Source: Internet
Author: User

Name and address conversion getservbyname and Getservbyport functions

Services are also usually marked by name, and the Getservbyname function is used to find the appropriate service based on the given name.

#include
struct servent *getservbyname (const char *servname, const char *protoname);
Success: Returns the servent type non-null pointer;
Failure: null pointer;
This function returns a non-null pointer

struct servent
{
Char *s_name; It's the name of the service.
Char **s_aliases; Alias list
int s_port; Service port number
Char *s_proto; The protocol used
};

The service name parameter servname must be specified. If the protocol is specified at the same time (that is, the Protoname parameter is a non-null pointer), the specified service must have a matching protocol. Some Internet services are used both for TCP and for UDP delivery (such as DNS). If Protoname is not specified and servname specifies that the service supports multiple protocols, then returning that port number depends on the implementation. In general, services that support multiple protocols Wang Wang uses the same TCP port number and UDP port number.

The main concern in the servent structure is the port number.
Note: This port number is returned in the order of the network bytes, so it is absolutely not possible to call Htons when it is stored in a set of interface geologic structures.


The function getservbyport is used to find the appropriate service based on a given port number and optional protocol.
#include
struct servent *getservbyport (int port, const char *protoname);
Success: Returns the servent type non-null pointer;
Failure: null pointer;
Note: The value of port must be a network byte order, such as:
ptr = Getservbyport (htons (), "UDP"); DNS using UDP

Example code:

int main (int argc, char** argv)
{
int sockfd,n;
Char Recvline[maxline + 1], addrstr[128],str[inet_addrstrlen];;
Char **charpptr;
struct sockaddr_in servaddr;
struct IN_ADDR **pptr;
struct IN_ADDR *inetaddrp[2];
struct IN_ADDR inetaddr;
struct Hostent *hptr;
struct servent *sptr;

if (argc! = 3)
{
ReportError ("Usage:getservice");
Exit (1);
}
if ((Hptr = gethostbyname (argv[1]) = = = NULL)
{
if (Inet_aton (argv[1], &inetaddr) = = 0)
{
ReportError ("HostName error for%s:%s", Argv[1],hstrerror (H_errno));
Exit (1);
}
Else
{
Inetaddrp[0] = &inetAddr;
INETADDRP[1] = NULL;
Pptr = INETADDRP;
}
}
else//print host information first
{
printf ("Official hostname:%s\n", hptr->h_name);
for (charpptr=hptr->h_aliases; *charpptr!=null; ++charpptr)
printf ("\talias:%s\n", *charpptr);
Switch (hptr->h_addrtype)
{
Case AF_INET:
for (charpptr = hptr->h_addr_list; *charpptr!=null; ++charpptr)
printf ("\taddress:%s\n",
Inet_ntop (Hptr->h_addrtype, *charpptr, str, sizeof (str)));
Break
}

Pptr = (struct in_addr**) hptr->h_addr_list;
}

if ((Sptr = Getservbyname (argv[2], "tcp") = = = NULL)
{
ReportError ("Getservbyname error for%s", argv[2]);
Exit (1);
}
printf ("Official Service Name:%s\n", sptr->s_name);//Print Service information
for (charpptr=sptr->s_aliases; *charpptr!=null; ++charpptr)
printf ("\talias:%s\n", *charpptr);
printf ("\tport Number:%d\n", sptr->s_port);
printf ("\tprotocol:%s\n", Sptr->s_proto);

for (; *pptr! = NULL; ++pptr)
{
if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)
{
ReportError ("socket error");
Exit (1);
}
Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port = sptr->s_port;//Set the port number based on the service information obtained
memcpy (&servaddr.sin_addr, *pptr, sizeof (struct in_addr));//Set IP according to hostname information
Inet_ntop (af_inet, (struct sockaddr*) &servaddr, addrstr,sizeof (SERVADDR));
printf ("Trying%s:%d\n", Addrstr, Servaddr.sin_port);

if (Connect (SOCKFD, (struct sockaddr*) &servaddr, sizeof (servaddr)) = = 0)//connection succeeded
Break
ReportError ("Connect error");
Close (SOCKFD);
}
if (*pptr = = NULL)
{
ReportError ("Unable to connet");
Exit (1);
}
while ((N=read (SOCKFD, Recvline, MAXLINE) > 0)
{
Recvline[n] = 0;
Fputs (Recvline, stdout);
}
return 0;
}


Connection is rejected, do not know what reason, is the corresponding service did not open?

Use the Service--status-all command to view the currently running services and their status

[[Email protected] bin]# service--status-all
ABRTD (PID 1979) is running ...
Abrt-dump-oops (PID 1987) is running ...
Acpid (PID 1801) is running ...
ATD (PID 2006) is running ...
AUDITD (PID 1679) is running ...
Avahi-daemon (PID 1775) is running ...
Usage:/etc/init.d/bluetooth {start|stop}
Stopped
Cgred has stopped
Cpuspeed has stopped
Crond (PID 1995) is running ...
DNSMASQ has stopped
Usage:/etc/init.d/firstboot {start|stop}
Hald (PID 1810) is running ...
HTTPD has stopped
Table: Filter
Chain INPUT (Policy ACCEPT)
Num Target prot opt source destination
1 ACCEPT All::/0::/0 State related,established
2 ACCEPT ICMPv6::/0::/0
3 ACCEPT All::/0::/0
4 ACCEPT tcp::/0::/0 State NEW TCP dpt:22
5 REJECT All::/0::/0 Reject-with icmp6-adm-prohibited

Chain FORWARD (Policy ACCEPT)
Num Target prot opt source destination
1 REJECT All::/0::/0 Reject-with icmp6-adm-prohibited

Chain OUTPUT (Policy ACCEPT)
Num Target prot opt source destination

Table: Filter
Chain INPUT (Policy ACCEPT)
Num Target prot opt source destination
1 ACCEPT All – 0.0.0.0/0 0.0.0.0/0 State related,established
2 ACCEPT ICMP--0.0.0.0/0 0.0.0.0/0
3 ACCEPT All--0.0.0.0/0 0.0.0.0/0
4 ACCEPT TCP--0.0.0.0/0 0.0.0.0/0 state NEW TCP dpt:22
5 REJECT All--0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (Policy ACCEPT)
Num Target prot opt source destination
1 REJECT All--0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (Policy ACCEPT)
Num Target prot opt source destination

Irqbalance has stopped
Kdump is not operational
Mdmonitor (PID 1733) is running ...
Messagebus (PID 1752) is running ...
Module Netconsole not loaded
To configure the device:
Lo eth0
Current Active Device:
Lo eth0
NetworkManager (PID 1763) is running ...
NTPD has stopped
Master (PID 1955) is running ...
Process accounting is disabled.
QUOTA_NLD has stopped
Rdisc has stopped
Restorecond (PID 2781) is running ...
RNGD has stopped
RSYSLOGD (PID 1695) is running ...
Sandbox is stopped
SASLAUTHD has stopped
SMARTD has stopped
Openssh-daemon (PID 1879) is running ...
Wpa_supplicant (PID 1812) is running ...

Name and address conversion getservbyname and Getservbyport functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.