블로그 이미지
랜달프

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

Notice

    'RFC868'에 해당되는 글 2

    1. 2009.03.18 타임서버에서 시간 가져오기
    2. 2009.03.18 RFC 868 (Time Protocol)
    2009. 3. 18. 19:20 Programming/C programming

    우리가 흔히 리눅스에서

    rdate -s time.bora.net

    이라는 명령어로 시간을 맞춘다

    즉,     time.bora.net   이라는 타임 서버에서 프로토콜에 맞춰 값을 가져온 후 시스템에 적용 하는 것이다.
    다음은 RFC 868 문서를 보고 만든 것인데 time.bora.net 서버에서 시간을 가져와 출력하는 코드이다.
    #ifdef _WIN32
    #include 
    #else
    #include 
    #include 
    #include 
    #include 
    #endif
    
    #include 
    #include 
    
    
    std::time_t getTime()
    {
    #ifdef _WIN32
    	WSADATA wsaData;
    #endif
    
    	int hSocket;
    	sockaddr_in servAddr;
    	std::time_t	sBuffer;
    	struct hostent* s_HostEntry;    
    	int strLen;
    	
    #ifdef _WIN32
    	if( WSAStartup(MAKEWORD(1,1),&wsaData) != 0 )
    	{
    		std::cout << "Startup Error" << std::endl;
    		return -1;
    	}
    #endif
    
    	hSocket = socket(PF_INET, SOCK_STREAM, 0);
    	
    	if( -1 == hSocket )
    	{
    		std::cout << "Socket Create Error" << std::endl;
    		return -1;
    	}
    
    	memset(&servAddr, 0, sizeof(servAddr));
    	servAddr.sin_family = AF_INET;
    	servAddr.sin_port = htons(37);
    
    	s_HostEntry = gethostbyname("time.bora.net");
    	memcpy((void *)(&servAddr.sin_addr), (void *)(s_HostEntry->h_addr), sizeof(servAddr.sin_addr));
    
    	if ( -1 == connect(hSocket, (sockaddr*)&servAddr, sizeof(servAddr)) )
    	{
    		return -1;
    	}
    
    	strLen = recv(hSocket, (char*)&sBuffer, sizeof(sBuffer), 0);
    	
    
    	if( -1 == strLen )
    	{
    		return -1;
    	}
    
    	sBuffer = ntohl(sBuffer) - 2208988800l;
    
    #ifdef _WIN32
    	closesocket(hSocket);
    	WSACleanup();
    #else
    	close(hSocket);
    #endif
    
    	return sBuffer;
    }
    
    int main()
    {
    	std::time_t currentTime;
    	std::tm* ts;
    	char str_time[80];
    
    	currentTime = getTime();
    	ts = localtime(¤tTime);      // 시스템에 적용된 시간대로 tm 구조체에 저장  (대한민국 표준시 GMT +09:00 )
    	//ts = gmtime(¤tTime);     // GMT  
    
    	strftime(str_time, 80, "%Y년 %m월 %d일 %H:%M:%S  %Z", ts);  // tm 구조체를 문자열로 저장
    
    	std::cout << str_time << std::endl;
    	
    	return 0;
    }
    
    struct tm 구조체
    #include 
    struct tm {
    
    	int tm_sec;   /* 초 */
    	int tm_min;   /* 분 */
    	int tm_hour;  /* 시 (0--23) */
    	int tm_mday;  /* 일 (1--31) */
    	int tm_mon;   /* 월 (0--11) */
    	int tm_year;  /* 년 (+ 1900) */
    	int tm_wday;  /* 요일 (0--6; 일요일 = 0) */
    	int tm_yday;  /* 올해 몇번째 날 (0--365) */
    	int tm_isdst; /* 서머타임 여부 */
    
    };
    

    실행 화면

    posted by 랜달프
    2009. 3. 18. 19:09 Programming/C programming


    RFC 868

    Network Working Group
    Request for Comments: 868

    J. Postel - ISI
    K. Harrenstien - SRI
    May 1983


    Time Protocol

    This RFC specifies a standard for the ARPA Internet community. Hosts on the ARPA Internet that choose to implement a Time Protocol are expected to adopt and implement this standard.
    This protocol provides a site-independent, machine readable date and time. The Time service sends back to the originating source the time in seconds since midnight on January first 1900.
    One motivation arises from the fact that not all systems have a date/time clock, and all are subject to occasional human or machine error. The use of time-servers makes it possible to quickly confirm or correct a system's idea of the time, by making a brief poll of several independent sites on the network.
    This protocol may be used either above the Transmission Control Protocol (TCP) or above the User Datagram Protocol (UDP).
    When used via TCP the time service works as follows:


    S: Listen on port 37 (45 octal).
    U: Connect to port 37.
    S: Send the time as a 32 bit binary number.
    U: Receive the time.
    U: Close the connection.
    S: Close the connection.


    The server listens for a connection on port 37. When the connection is established, the server returns a 32-bit time value and closes the connection. If the server is unable to determine the time at its site, it should either refuse the connection or close it without sending anything.

    When used via UDP the time service works as follows:


    S: Listen on port 37 (45 octal).
    U: Send an empty datagram to port 37.
    S: Receive the empty datagram.
    S: Send a datagram containing the time as a 32 bit binary number.
    U: Receive the time datagram.


    The server listens for a datagram on port 37. When a datagram arrives, the server returns a datagram containing the 32-bit time value. If the server is unable to determine the time at its site, it should discard the arriving datagram and make no reply.

    The Time

    The time is the number of seconds since 00:00 (midnight) 1 January 1900 GMT, such that the time 1 is 12:00:01 am on 1 January 1900 GMT; this base will serve until the year 2036.

    For example:

    the time

    2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT,

    2,398,291,200 corresponds to 00:00 1 Jan 1976 GMT,

    2,524,521,600 corresponds to 00:00 1 Jan 1980 GMT,
     
    2,629,584,000 corresponds to 00:00 1 May 1983 GMT,

    and

    -1,297,728,000 corresponds to 00:00 17 Nov 1858 GMT.

    posted by 랜달프
    prev 1 next