網絡字節序和主機序相互轉換
#include<netinet/in.h>
uint16_t htons(uint16_t host16bitvalue);//主機序轉網絡字節序 16位
uint32_t htonl(uint32_t host16bitvalue);//主機序轉網絡字節序 32位
uint16_t ntohs(uint16_t host16bitvalue);//網絡字節序序轉主機 16位
uint32_t ntohl(uint32_t host32bitvalue);//網絡字節序序轉主機 32位
h代表host,n代表network,s代表short,l代表long
IPv4點分十進制和網絡字節序的轉換
#include <arpa/inet.h>
int inet_aton(const char *strptr, struct in_addr *addrptr);//點分十進制轉網絡字節序
char *inet_ntoa(struct in_addr inaddr);//網絡字節序轉點分十進制
IPv4或者IPv6點分十進制和網絡字節序的轉換
#include <arpa/inet.h>
int inet_pton(int family,const char *strptr, void *addrptr);//點分十進制轉網絡字節序
const char *inet_ntop(int family,const void *addrptr, char *strptr,size_t len);//網絡字節序轉點分十進制
#include <sys/socket.h>
int getsockname(int sockfd,struct sockaddr *localaddr,socklen_t *addrlen);
int getpeername(int sockfd,struct sockaddr *localaddr,socklen_t *addrlen);