Linux network Programming (ii)

Source: Internet
Author: User

information functions for service sets and clients

1. Byte conversion function

There are many types of machines on the network that represent different byte orders for data, such as the i386 chip is low-byte at the lower end of the memory address, the high byte is at the top, and the Alpha chip is the opposite. In order to unify, under Linux, there are special byte conversion functions.

unsigned long int htonl (unsigned long int hostlong)
unsigned short int htons (unisgned short int hostshort)
unsigned long int ntohl (unsigned long int netlong)
unsigned short int ntohs (unsigned short int netshort)

In these four conversion functions, H stands for host, and N for NETWORK.S represents the first function of long for the short L, which translates the Long data on the machine into a long on the network. Several other functions have the same meaning.

2. Conversion of IP and domain names
Marking a machine on the network can use IP or domain name. So how do we go about converting?

struct hostent *gethostbyname (const char *hostname)
struct hostent *gethostbyaddr (const char *addr,int len,int type)
Definition of struct hostent in
struct hostent{
Char *h_name; /* Official name of the host */
Char *h_aliases; /* Alias for Host */
int h_addrtype; /* Address type of host af_inet*/
int h_length; /* The address length of the host is 4 bytes 32 bits for IP4 */
Char **h_addr_list; /* Host's IP Address list */
}
#define H_ADDR H_addr_list[0]/* The first IP address of the host */

GetHostByName can convert a machine name (such as linux.yessun.com) to a struct pointer. In this structure, the information of the domain name is stored.
GETHOSTBYADDR can convert a 32-bit IP address (c0a80001) to a struct pointer.

When these two functions fail, they return null and set the H_errno error variable, and call H_strerror () to get a detailed error message

3, String IP and 32-bit IP conversion.
In the network above we use the IP is a digital dot (192.168.0.1) composition, and in the struct IN_ADDR structure with a 32-bit IP, we above that 32-bit IP (c0a80001) is 192.168.0.1 For the sake of conversion we can use the following two functions

int Inet_aton (const char *cp,struct in_addr *INP)
Char *inet_ntoa (struct in_addr in)

Inside the function a means that ASCII n represents the network. The first function represents the conversion of A.B.C.D IP to a 32-bit IP, stored in the INP pointer. The second is the format of converting 32-bit IP to a.b.c.d.

4. Service Information function
In the network program we sometimes need to know the port. IP and service information. At this point we can use the following several functions

int getsockname (int sockfd,struct sockaddr *localaddr,int *addrlen)
int getpeername (int sockfd,struct sockaddr *peeraddr, int *addrlen)
struct servent *getservbyname (const char *servname,const char *protoname)
struct servent *getservbyport (int port,const char *protoname)
struct Servent {
Char *s_name; /* Official service Name */
Char **s_aliases; /* Alias list */
int s_port; /* Port number */
Char *s_proto; /* Protocol to use */
}

In general, we seldom use these functions. Corresponding client, when we want to get the port number of the connection, use the port number that can be assigned to the system after the connect call succeeds. For the server, after we populate with Inaddr_any, in order to get the IP of the connection we can after the accept call succeeds Use to obtain an IP address.

There are many default ports and services on the network, such as Port 21 to FTP80 for www. To get the service of the specified port number, we can call the fourth function, instead of calling the third function in order to get a port number.

5. Sample Program

/******* ..... (hostname_ip.c) ************/
#include "stdio.h"
#include "stdlib.h"
#include "errno.h"
#include "sys/ Types.h "
#include" sys/socket.h "
#include" unistd.h "
#include" netinet/in.h "
#include" netdb.h "

int Main (int argc, char **argv)
{
        struct sockaddr_in addr;
        struct hostent *host;
        Char **alias;

if (argc<2) {
fprintf (stderr, "usage:%s Hostname|ip: A", argv[0]);
Exit (1);
}

argv++;
for (; *argv!=null;argv++) {
/* ....... ip*/
if (Inet_aton (*ARGV,&AMP;ADDR.SIN_ADDR)!=0) {
HOST=GETHOSTBYADDR ((char *) &addr.sin_addr,4,af_inet);
printf ("Address information of Ip%s", *ARGV);
}
else {
/* ..,.....? */
Host=gethostbyname (*ARGV);
printf ("Address Information of host%s", *ARGV);
}
if (host==null) {
/* ... ,.....*/
fprintf (stderr, "No address information of%s", *ARGV);
Continue
}
printf ("Official host name%s", host->h_name);
printf ("Name aliases:");
for (alias=host->h_aliases;*alias!=null;alias++)
printf ("%s,", *alias);
printf ("Ip address:");
for (alias=host->h_addr_list;*alias!=null;alias++)
printf ("%s,", Inet_ntoa (* (struct in_addr *) (*alias));
}
}

Related Article

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.