문자열 IP 주소를 네트워크 바이트 순서(빅 엔디안)로 변환
inet_addr( ) vs inet_aton( ) 사용
sockaddr _in
| struct sockaddr_in |
| { |
| sa_family_t sin_family; |
| uint16_t sin_port; |
| struct in_addr sin_addr; |
| char sin_zero[8]; |
| } |
IP주소 211.214.107.99와 같은 ‘십진수 표현방식(Dotted Decimal Notation)'을 네트워크 바이트 순서의 IP가 변환한다.
방법 1) inet_addr( )
| #include <arpa/inet.h> |
| |
| in_addr_t inet_addr(const char * string); |
| |
| |
| #include <stdio.h> |
| #include <arpa/inet.h> |
| |
| int main(int argc, char *argv[]) |
| { |
| char *addr1="127.212.124.78"; |
| char *addr2="127.212.124.256"; |
| |
| unsigned long conv_addr=inet_addr(addr1); |
| if(conv_addr==INADDR_NONE) |
| printf("Error occured! \n"); |
| else |
| printf("Network ordered integer addr: %#lx \n", conv_addr); |
| |
| conv_addr=inet_addr(addr2); |
| if(conv_addr==INADDR_NONE) |
| printf("Error occureded \n"); |
| else |
| printf("Network ordered integer addr: %#lx \n\n", conv_addr); |
| return 0; |
| } |
방법 2) inet_aton( )
| #include <arpa/inet.h> |
| |
| int inet_aton(const char * string , struct in_addr *addr); |
| |
| |
| |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <arpa/inet.h> |
| void error_handling(char *message); |
| |
| int main(int argc, char *argv[]) |
| { |
| char *addr="127.232.124.79"; |
| struct sockaddr_in addr_inet; |
| |
| if(!inet_aton(addr, &addr_inet.sin_addr)) |
| error_handling("Conversion error"); |
| else |
| printf("Network ordered integer addr: %#x \n", addr_inet.sin_addr.s_addr); |
| return 0; |
| } |
| |
| void error_handling(char *message) |
| { |
| fputs(message, stderr); |
| fputc('\n', stderr); |
| exit(1); |
| } |
네트워크 바이트 순서(빅 엔디안)를 문자열 IP 주소로 변환
inet_ntoa( )
| #include <arpa/inet.h> |
| |
| char *inet_ntoa(struct in_addr adr); |
| |
| |
| #include <stdio.h> |
| #include <string.h> |
| #include <arpa/inet.h> |
| |
| int main(int argc, char *argv[]) |
| { |
| struct sockaddr_in addr1, addr2; |
| char *str_ptr; |
| char str_arr[20]; |
| |
| addr1.sin_addr.s_addr=htonl(0x1020304); |
| addr2.sin_addr.s_addr=htonl(0x1010101); |
| |
| str_ptr=inet_ntoa(addr1.sin_addr); |
| strcpy(str_arr, str_ptr); |
| printf("Dotted-Decimal notation1: %s \n", str_ptr); |
| |
| inet_ntoa(addr2.sin_addr); |
| printf("Dotted-Decimal notation2: %s \n", str_ptr); |
| printf("Dotted-Decimal notation3: %s \n", str_arr); |
| return 0; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| printf("Client %s:%d is connected..\n",inet_ntoa(clntAddr.sin_addr), ntohs(clntAddr.sin_port)); |
| printf("Client(%d) : %s \n", ntohs(clntAddr.sin_port),rBuff); |
| printf("Client(%d): is disconnected\n", ntohs(clntAddr.sin_port)); |
문자열 주소 초기화 방법
| struct sockaddr_in addr; |
| |
| char *serv_ip="211.217.168.13"; |
| char *serv_port="9190"; |
| |
| memset(&addr, 0, sizeof(addr)); |
| addr.sin_family=AF_INET; |
| addr.sin_addr.s_addr=inet_addr(serv_ip); |
| addr.sin_port=htons(atoi(serv_port)); |
서버의 sockaddr_in 구조체 변수에 IP 211. 217 .168 .13 , PORT 9190로 들어오는 요청을 받겠다.
INADDR ANY
| struct sockaddr_ in addr; |
| char *serv_port= "9190"; |
| |
| memset(&addr, 0, sizeof(addr)); |
| addr.sin_family=AF_INET; |
| addr.sin_addr.s_addr=htonl(INADDR_ANY); |
| addr.sin_port=htons(atoi(serv_port)); |
서버 프로그램의 구현에 많이 선호되는 방법으로 INADDR_ANY라는 이름의 상수를 통해서 IP주소를 자동 할당한다.
두 개 이상의 IP를 할당 받아서 사용하는 경우 PORT변호만 일치하면 수신할 수 있게 된다.
클라이언트는 서버의 기능을 포함하는 경우가 아니라면, 사용될 일이 별로 없다.
출처 & 참고
1. 윤성우 열혈 TCP IP 소켓 프로그래밍 Ch. 01
2. 네트워킹 강좌
댓글을 사용할 수 없습니다.