tonykang22 / study

0 stars 0 forks source link

[리눅스 개발환경] 16. 소켓 개요 #87

Open tonykang22 opened 2 years ago

tonykang22 commented 2 years ago

[리눅스 개발환경] 16. 소켓 개요

TCP/IP



소켓



소켓 생성

#include <sys/types.h>
#include <sys/socket.h>

int socket(int domain, int type, int protocol);







바이트 순서


#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong); 
uint16_t htons(uint16_t hostshort); 
uint32_t ntohl(uint32_t netlong); 
uint16_t ntohs(uint16_t netshort);



예제

#include <stdio.h> 
#include <arpa/inet.h>

union longbyte { 
    long l;
    unsigned char c[4]; 
};

int main() 
{
    union longbyte lb;

    lb.l = 0x1234;
    printf("[Host Order] %02x, %02x, %02x, %02x\n", lb.c[0], lb.c[1], lb.c[2], lb.c[3]);

    lb.l = htonl(lb.l);
    printf("[Network Order] %02x, %02x, %02x, %02x\n", lb.c[0], lb.c[1], lb.c[2], lb.c[3]);

    return 0; 
}