본문 바로가기

TCP/IP

inet_addr()함수, (=inet_aton) dotted decimal -> 32비트 IP주소 할 때, inet_addr()함수 사용(=inet_aton) #include #include int main() { char* addr1 = "1.2.3.4"; char* addr2 = "1.2.3.255"; unsigned long conv_addr; conv_addr = inet_addr(addr1); if(conv_addr == INADDR_NONE) { printf("error occur : %d\n", conv_addr); } else { printf("unsigned long addr(network ordered) : %x\n", conv_addr); } conv_addr = inet_addr(addr2); if(conv_addr == INAD.. 더보기
호스트pc(리틀엔디안) <-> 네트워크(빅엔디안) 네트워크 상에서는 빅엔디안을 사용 htons()함수 : 호스트에서 port번호(2바이트)를 네트워크로 바꿀 때 사용 ntohs()함수 : 네트워크에서 port번호(2바이트)를 호스트로 바꿀 때 사용 htonl()함수 : 호스트에서 IP주소(4바이트)를 네트워크로 바꿀 때 사용 ntohl()함수 : 네트워크에서 IP주소(4바이트)를 호스트로 바꿀 때 사용 h는 host, n은 network, s는 short(2byte), l은 long(4byte) P C 네트워크 리틀엔디안 빅엔디안 2byte(port번호) ---------------------> htons()함수 사용 htonl()함수 사용 더보기
gethostbyname() 함수 /* 도메인 네임 -> 32비트 IP주소 할 때, gethostbyname() 함수를 사용한 프로그램*/ #include #include #include #include int main(int argc, char *argv[]) { struct hostent *myhost; struct in_addr myinaddr; //IP 주소를 저장할 구조체 int i; if(argc h_name); //호스트이름 출력 i = 0; while(myhost->h_aliases[i] != NULL) { printf("aliases name : \t\t\t%s\n", myhost->h_aliases[i]); //호스트별명 출력 i++; } printf("host address type : \t\t%d\n", myhost.. 더보기
inet_ntoa() 함수 /*인터넷 주소를 표시할 수 있는 3가지 방식중, 32비트 IP주소 -> dotted decimal 할 때, inet_ntoa()함수를 사용한 프로그램*/ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *haddr; struct in_addr host_ip; if(argc dotted decimal*/ printf("dotted decimal Address : %s\n", inet_ntoa(host_ip)); return 0; } 더보기
소켓의 생성과 해지 TCP와 UDP를 이용해 통신하려면 프로그램이 운영체제에 소켓 인스턴스의 생성을 요청해야한다. 이러한 과정을 수행하는 함수가 socket()이다. int socket(int domain, int type, int protocol) 첫번째 인자인 domain은 프로토콜 패밀리, 소켓의 통신영역을 결정 PF_INET : 인터넷 체계방식 PF_INET6 : IPv6 인터넷 체계방식 PF_UNIX : 유닉스 체계방식 PF는 protocol family 약자 두번째 인자인 type는 소켓의 형식을 결정 SOCK_STREAM : 스트림형 소켓(TCP) SOCK_DRDGRAM : 데이터그램형 소켓(UDP) 세번째 인자는 사용될 종단 간 프로토콜을 구체적으로 명시 IPPROTO_TCP를 사용하면 스트림 소켓을 위한 T.. 더보기