타임 서버에서 데이터 받아오기
데브피아 서생일님께서 올리신 팁&강좌 게시판에서 가져온 글입니다.
타입서버에서 표준시간을 얻어오는 방법을 제시해주고 있습니다.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace NTP_CLIENT
{
class Program
{
static void Main(string[] args)
{
TcpClient tsc = new TcpClient("time-a.nist.gov", 13);
if (tsc.Connected)
{
NetworkStream ns = tsc.GetStream();
StreamReader sr = new StreamReader(ns);
string sResult = sr.ReadToEnd().Trim();
Console.WriteLine(sResult); //서버에서 받은 결과
//공백으로 결과값을 나눠서 배열에 넣음.
string[] saResult = sResult.Split(' ');
foreach (string s in saResult)
{
Console.WriteLine(s);
}
}
else
{
Console.WriteLine("-_-; 연결 안됨");
}
}
}
}