Linux Network Programming - Server and Client Information Functions

  

Computer shop news

1 byte conversion function There are many types of machines on the network, these machines are different in the byte order of the data, such as the i386 chip is the low byte in the memory address The low end, the high byte is at the high end, and the alpha chip is the opposite. To unify, there is a special byte conversion function under Linux. 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, n stands for network.s stands for short l stands for long, the meaning of the first function is Convert the long data on this machine into a long on the network. The meaning of several other functions is similar. 2 IP and domain name conversion On the network, a machine can use IP or domain name. So how do we convert? Struct hostent *gethostbyname(const char *hostname) struct hostent *gethostbyaddr(const char *addr,int len,int type) struct hostent in Construct struct hostent{ char *h_name; /* The official name of the host */char *h_aliases; /* host alias */int h_addrtype; /* host address type AF_INET*/int h_length; /* host address length for IP4 Is 4 bytes 32 bits */char **h_addr_list; /* Host IP address list */} #define h_addr h_addr_list[0] /* The host's first IP address */gethostbyname can be the machine name (such as linux. Yessun.com) is converted to a structure pointer. In this structure, the domain name information gethostbyaddr can convert a 32-bit IP address (C0A80001) into a structure pointer. When these two functions fail, NULL is returned and the h_errno error variable is set. Call h_strerror() to get detailed error information. 3 String IP and 32-bit IP conversion. The IP used in the network is composed of numbers plus points (192.168.0.1), and the struct in_addr structure is used. 32-bit IP, the 32-bit IP above us (C0A80001) is 192.168.0.1. In order to convert we can use the following two Int inet_aton(const char *cp,struct in_addr *inp) char *inet_ntoa(struct in_addr in) The function a represents ascii n represents network. The first function represents converting the abcd IP to a 32-bit IP, stored in inp Inside the pointer. The second is to convert the 32-bit IP to abcd format. 4 Service Information Functions In the network program, we sometimes need to know the port.IP and service information. At this time we can use the following 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 used */} Generally we rarely use these functions. Corresponding customers When we want to get the port number of the connection, we can get the port number assigned by the system after the connect call succeeds. For the server, we fill it with INADDR_ANY. In order to get the connected IP, we can use IP after the accept call succeeds. Address. There are many default ports and services on the network, for example, port 21 corresponds to WWW for ftp80. In order to get the service of the specified port number, we can call the fourth function. Instead, we can call the third function in order to get the port number. 5 An example

CODE:

#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <arpa/Inet.h> int main(int argc ,char **argv) { struct sockaddr_in addr; struct hostent *host; char **alias; if(argc<2) { fprintf(stderr,"Usage:%s hostname

Copyright © Windows knowledge All Rights Reserved