第四节、网络字节序与主机字节序的转换

/*
字节序转换
unsigned short htons(unsigned short);
unsigned short ntohs(unsigned short);
unsigned long htonl(unsigned long);
unsigned long ntohl(unsigned long);

htons中的h代表主机(host)字节序
htons中的n代表网络(network)字节序
*/



#include 
#include 

int main(int argc, char const *argv[])
{
     
	unsigned short host_port = 0x1234;
	unsigned short net_port = htons(host_port);

	unsigned int host_addr = 0x12345678;
	unsigned int net_addr = htonl(host_addr);

	// char ip_msg[] = inet_ntoa

	printf("Host ordered port: %#x \n",host_port );
	printf("Network ordered port: %#x \n",net_port );

	printf("Host ordered address: %#lx\n",host_addr );
	printf("Network ordered address: %#lx\n",net_addr );


	return 0;
}

你可能感兴趣的:(网络编程TCP/IP)