Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

htons(3)

1장. htons(3)

차례
1.1절. 사용법
1.2절. 설명
1.3절. 반환값
1.4절. 예제

host byte order을 따르는 데이터를 network byte order로 변경한다.


1.1절. 사용법

#include <netinet/in.h>

unsigned short int htons(unsigned short int hostshort);
unsigned long int htonl(unsigned long int hostshort);
unsigned short int ntohs(unsigned short int hostshort);
unsigned long int ntohl(unsigned long int hostshort);
		


1.2절. 설명

데이터는 바이트 단위로 저장되지만 저장되는 방식에 있어서 CPU마다 차이가 발생하게 된다. 예를 들어 4바이트 크기의 int자료를 저장한다고 했을 때 어떤 CPU는 가장 낮은 바이트부터 저장을 하는가 하면, 어떤 CPU는 가장 높은 바이트 부터 데이터를 저장하기도 한다. 전자를 Little Endian방식, 후자를 Big Endian방식 이라고 한다.

이런 이유로 서로 다른 데이터 저장 방식을 사용하는 시스템끼리 통신을 하게 될경우 전혀 원하지 않는 값들을 서로 주고 받는 경우가 발생할 수 있다. 한쪽에서는 12345를 보냈는데, 다른 한쪽에서는 엉뚱하게 365779719로 받아 들이는 문제들이 발생한다.

이런 문제를 해결하기 위해서 데이터 통신을 할때는 명시적으로 네트워크 byte order을 따르도록 데이터의 byte order를 변경한다. 네트워크 byte order는 Big Endiasn을 따른다.

원격 호스트와 데이터 통신을 하길 원한다면 보낼 때 네트워크 byte order로, 받았을 때는 호스트 byteorder로 변경한다.

htonl()함수는 long intger(일반적으로 4byte)데이터를 네트워크 byte order로 변경한다.

htons()함수는 short intger(일반적으로 2byte)데이터를 네트워크 byte order로 변경한다.

ntohl()함수는 long intger 데이터를 호스트 byte order로 변경한다.

ntohs()함수는 short intger 데이터를 호스트 byte order로 변경한다.

Endian과 네트워크 byte order에 대한 자세한 내용은 Endian에 대해서를 참고하기 바란다.


1.3절. 반환값

네트워크 byte order된 2바이트 값을 넘겨준다.


1.4절. 예제

#include <sys/time.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    int client_sockfd;
    struct sockaddr_in clientaddr;
    int data = 123456789;
    int client_len;

    client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_addr.s_addr = inet_addr("192.168.100.190");
    clientaddr.sin_port = htons(atoi(argv[1]));

    client_len = sizeof(clientaddr);

    if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) < 0)
    {
        perror("Connect error : ");
        exit(0);
    }
    
    // 보낼 데이터 네트워크 byte order를 따르도록 변경한다. 
    data = htonl(data);
    write(client_sockfd, (void *)&data, sizeof(int));

    // 읽어들인 데이터는 호스트 byte order을 따르도록 변경한다.
    read(client_sockfd, (void *)&data, sizeof(int));
    data = ntohl(data);
    close(client_sockfd);
}