목표: 서버에 연결가능하지를 검사하기 위한 방법으로,
Ping Test 기능을 구현해 봤다(USER Control 제작중 Ping Test 기능 구현)
처음에는 ActiveX를 이용하여 구현하려 했으나, C#으로 Main UI를 개발중이여서,
User Control을 사용하였다.
1. using을 선언하여 network에 필요한 namespace들을 지정한다.
//for use the network, you will define the list like under sentance
using System.Net;
using System.Net.NetworkInformation;
//////////////////////////////////////////////////
public partical class ServerLoginX : UserControl
{
///////////////////////////////////////////////////
2. 먼저 초기화 전역 선언부로 Ping Class를 선언한다.//
///////////////////////////////////////////////////
private string strCurIP = "";
private Ping ping = null;
private PingOptions pingOption = null;
///////////////////////////////////////////////////
3. Form이 생성되는 생성부에 초기값을 설정.
///////////////////////////////////////////////////
public ServerLoginX()
{
InitializeComponent();
// textbox ping result output window attribute set.
txtboxResult.Enabled = false;
txtboxResult.BackColor = Color.Black;
txtboxResult.ForeColor = Color.White;
// IP는 xxx.xxx.xxx.xxx 형태로 나타내기위해서 maskedTextBox 사용.
mskTxtBoxServerIP.Mask = "000.000.000.000";
}
///////////////////////////////////////////////////
4.실제 Ping Test Button이 수행되는 부분.
///////////////////////////////////////////////////
/// function : btnPingTest_Click
/// review : This "PING TEST" function is confirm whether The Linux Server connect
private void btnPingTest_Click(object sender, EventArgs e)
{
ping = new Ping();
pingOption = new PingOptions();
string sdata = "samplesentance";
strCurIP = mskTxtBoxServerIP.Text;
if(strCurIP.Substring(0,1) == " ")
{
strCurIP = "192.168.001.197"; // default set
mskTxtBoxServerIP.Text = strCurIP;
}
if (!mskTxtBoxServerIP.MaskCompleted)
{
MessageBox.Show("Not IP Address Style your input Address!! ex)192.168.001.1100");
mskTxtBoxServerIP.Focus();
return;
}
// txtbox clear...
txtboxResult.Clear();
byte[] byteSendData = Encoding.ASCII.GetBytes(sdata);
// combo box로부터 반복실행할 값을 얻어 int형으로 변환하였다.
int pingcnt = Convert.ToInt32(cboxPingCnt.Text.ToString());
for (int loopcnt = 0; loopcnt < pingcnt; loopcnt++)
{
PingReply pingReply = ping.Send(strCurIP, 120, byteSendData, pingOption);
string strTemp;
if (pingReply.Status == IPStatus.Success)
{
strTemp = string.Format("Replay[{0}] From {1} : {2} Bytes TTL = {3}\r\n",
loopcnt+1, pingReply.Address, pingReply.Buffer.Length, pingReply.Options.Ttl);
txtboxResult.AppendText(strTemp);
txtboxResult.ScrollToCaret();
}
else
{
strTemp = string.Format("Request Time Out!!\r\n");
txtboxResult.AppendText(strTemp);
txtboxResult.ScrollToCaret();
}
}
}
}
'프로그래밍 > .Net' 카테고리의 다른 글
Control Attribute 컨트롤 속성 변경하기(get/set) (0) | 2014.01.17 |
---|---|
외부응용프로그램 실행하기(Process.Start 메서드) (0) | 2014.01.17 |
class를 만들어 보자 (0) | 2014.01.17 |
To execut the another Form(form dialog를 띄워보자) (0) | 2014.01.17 |
file access, file을 생성하고 읽어보자 (0) | 2014.01.17 |