C#pingコマンドの実装方法:Pingクラスの使用
36556 ワード
本稿では,C#pingコマンドの実装方法を紹介し,まず元のSocketソケットを用いてICMPプロトコルを実装し,pingコマンドを実行する.最後に、C#2.0に追加されたPingクラスを使用して実装することもできます.
C#pingコマンドの2つの実装方法について説明します.
C#pingコマンド実装:元のSocketソケットを使用してICMPプロトコルを実装します.
C#pingコマンド実行:pingコマンド実行
まず、Processクラスを使用して、独立したプロセスを作成し、Systemをインポートします.Diagnostics,
using System.Diagnostics;
インスタンスプロセスクラス、独立したプロセスの開始
Process p = new Process();
Processクラスには、いくつかの属性とメソッドを含むProcessStartInfoクラスのStartInfo属性があります.
次に、彼のいくつかのプロパティを使用します.
プログラム名の設定
p.StartInfo.FileName = "cmd.exe";
Shellの使用を閉じる
p.StartInfo.UseShellExecute = false;
リダイレクト標準入力
p.StartInfo.RedirectStandardInput = true;
リダイレクト標準出力
p.StartInfo.RedirectStandardOutput = true;
リダイレクトエラー出力
p.StartInfo.RedirectStandardError = true;
ウィンドウを表示しない設定
p.StartInfo.CreateNoWindow = true;
上記のプロパティの設定は、比較的重要なステップです.
設定されている以上、プロセスを開始しましょう.
p.Start();
実行するコマンドを入力します.ここでpingです.
p.StandardInput.WriteLine("ping -n 1 www.iwebtrados.com.cn ");
p.StandardInput.WriteLine("exit");
出力ストリームからコマンド実行結果を取得する、
string strRst = p.StandardOutput.ReadToEnd();
C#pingコマンド実装:c#2.0で追加されたPingクラスを利用
ここで私が書いたのはフォームプログラムです.まずtextbox、listbox、buttonコントロールを追加し、textboxはドメイン名またはIPを入力し、listboxは結果を表示する.
button 1_clickイベント入力
非同期の処理をしてbutton 1_を修正することもできます.Click、PingCompletedCallBackメソッドを追加
どうですか、1、2方式に比べて、3は簡単なほうが多いのではないでしょうか.
本文はネット小築のブログ:『c#の下でping操作を実現する』から来た.
C#pingコマンドの2つの実装方法について説明します.
C#pingコマンド実装:元のSocketソケットを使用してICMPプロトコルを実装します.
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- usingSystem.Net;
- usingSystem.Net.Sockets;
- namespacePingC
- {
- classping
- {
- constintSOCKET_ERROR=-1;
- constintICMP_ECHO=8;
- staticvoidMain(string[]args)
- {
- pingp=newping();
- Console.WriteLine(" Ping IP :");
- stringMyUrl=Console.ReadLine();
- Console.WriteLine(" Ping"+MyUrl+"……");
- Console.Write(p.PingHost(MyUrl));
- }
- publicstringPingHost(stringhost)
- {
- // IPHostEntry
- IPHostEntryServerHE,fromHE;
- intnBytes=0;
- intdwStart=0,dwStop=0;
- // ICMP Socket
- Socketsocket=
- newSocket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.Icmp);
- socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTimeout,1000);
- // ServerEndPoint
- try
- {
- ServerHE=Dns.GetHostByName(host);
- }
- catch(Exception)
- {
- return" ";
- }
- // ServerIP_EndPoint EndPoint
- IPEndPointipepServer=newIPEndPoint(ServerHE.AddressList[0],0);
- EndPointepServer=(ipepServer);
- // Endpoint
- fromHE=Dns.GetHostByName(Dns.GetHostName());
- IPEndPointipEndPointFrom=newIPEndPoint(fromHE.AddressList[0],0);
- EndPointEndPointFrom=(ipEndPointFrom);
- intPacketSize=0;
- IcmpPacketpacket=newIcmpPacket();
- //
- packet.Type=ICMP_ECHO;//8
- packet.SubCode=0;
- packet.CheckSum=0;
- packet.Identifier=45;
- packet.SequenceNumber=0;
- intPingData=24;//sizeof(IcmpPacket)-8;
- packet.Data=newByte[PingData];
- // Packet.Data
- for(inti=0;i<PingData;i++)
- {
- packet.Data[i]=(byte)'#';
- }
- //VariabletoholdthetotalPacketsize
- PacketSize=32;
- Byte[]icmp_pkt_buffer=newByte[PacketSize];
- Int32Index=0;
- //againcheckthepacketsize
- Index=Serialize(
- packet,
- icmp_pkt_buffer,
- PacketSize,
- PingData);
- //ifthereisaerrorreportit
- if(Index==-1)
- {
- return"ErrorCreatingPacket";
- }
- //convertintoaUInt16array
- //GettheHalfsizeofthePacket
- Doubledouble_length=Convert.ToDouble(Index);
- Doubledtemp=Math.Ceiling(double_length/2);
- intcksum_buffer_length=Index/2;
- //CreateaByteArray
- UInt16[]cksum_buffer=newUInt16[cksum_buffer_length];
- //CodetoinitializetheUint16array
- inticmp_header_buffer_index=0;
- for(inti=0;i<cksum_buffer_length;i++)
- {
- cksum_buffer[i]=
- BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index);
- icmp_header_buffer_index+=2;
- }
- //Callamethodwhichwillreturnachecksum
- UInt16u_cksum=checksum(cksum_buffer,cksum_buffer_length);
- //SavethechecksumtothePacket
- packet.CheckSum=u_cksum;
- //Nowthatwehavethechecksum,serializethepacketagain
- Byte[]sendbuf=newByte[PacketSize];
- //againcheckthepacketsize
- Index=Serialize(
- packet,
- sendbuf,
- PacketSize,
- PingData);
- //ifthereisaerrorreportit
- if(Index==-1)
- {
- return"ErrorCreatingPacket";
- }
- dwStart=System.Environment.TickCount;//Starttiming
- //sendthePacketoverthesocket
- if((nBytes=socket.SendTo(sendbuf,PacketSize,0,epServer))==SOCKET_ERROR)
- {
- return"SocketError:cannotsendPacket";
- }
- //Initializethebuffers.Thereceivebufferisthesizeofthe
- //ICMPheaderplustheIPheader(20bytes)
- Byte[]ReceiveBuffer=newByte[256];
- nBytes=0;
- //Receivethebytes
- boolrecd=false;
- inttimeout=0;
- //loopforcheckingthetimeoftheserverresponding
- while(!recd)
- {
- nBytes=socket.ReceiveFrom(ReceiveBuffer,256,0,refEndPointFrom);
- if(nBytes==SOCKET_ERROR)
- {
- return" ";
- }
- elseif(nBytes>0)
- {
- dwStop=System.Environment.TickCount-dwStart;//stoptiming
- return"Replyfrom"+epServer.ToString()+"in"
- +dwStop+"ms.Received:"+nBytes+"Bytes.";
- }
- timeout=System.Environment.TickCount-dwStart;
- if(timeout>1000)
- {
- return" ";
- }
- }
- //closethesocket
- socket.Close();
- return"";
- }
- ///<summary>
- ///ThismethodgetthePacketandcalculatesthetotalsize
- ///ofthePackbyconvertingittobytearray
- ///</summary>
- publicstaticInt32Serialize(IcmpPacketpacket,Byte[]Buffer,
- Int32PacketSize,Int32PingData)
- {
- Int32cbReturn=0;
- //serializethestructintothearray
- intIndex=0;
- Byte[]b_type=newByte[1];
- b_type[0]=(packet.Type);
- Byte[]b_code=newByte[1];
- b_code[0]=(packet.SubCode);
- Byte[]b_cksum=BitConverter.GetBytes(packet.CheckSum);
- Byte[]b_id=BitConverter.GetBytes(packet.Identifier);
- Byte[]b_seq=BitConverter.GetBytes(packet.SequenceNumber);
- Array.Copy(b_type,0,Buffer,Index,b_type.Length);
- Index+=b_type.Length;
- Array.Copy(b_code,0,Buffer,Index,b_code.Length);
- Index+=b_code.Length;
- Array.Copy(b_cksum,0,Buffer,Index,b_cksum.Length);
- Index+=b_cksum.Length;
- Array.Copy(b_id,0,Buffer,Index,b_id.Length);
- Index+=b_id.Length;
- Array.Copy(b_seq,0,Buffer,Index,b_seq.Length);
- Index+=b_seq.Length;
- //copythedata
- Array.Copy(packet.Data,0,Buffer,Index,PingData);
- Index+=PingData;
- if(Index!=PacketSize/*sizeof(IcmpPacket)*/)
- {
- cbReturn=-1;
- returncbReturn;
- }
- cbReturn=Index;
- returncbReturn;
- }
- ///<summary>
- ///ThisMethodhasthealgorithmtomakeachecksum
- ///</summary>
- publicstaticUInt16checksum(UInt16[]buffer,intsize)
- {
- Int32cksum=0;
- intcounter;
- counter=0;
- while(size>0)
- {
- UInt16val=buffer[counter];
- cksum+=buffer[counter];
- counter+=1;
- size-=1;
- }
- cksum=(cksum>>16)+(cksum&0xffff);
- cksum+=(cksum>>16);
- return(UInt16)(~cksum);
- }
- }
- ///
- ///<summary>
- ///ClassthatholdsthePackinformation
- ///</summary>
- publicclassIcmpPacket
- {
- publicByteType;//typeofmessage
- publicByteSubCode;//typeofsubcode
- publicUInt16CheckSum;//onescomplementchecksumofstruct
- publicUInt16Identifier;//identifier
- publicUInt16SequenceNumber;//sequencenumber
- publicByte[]Data;
- }//classIcmpPacket
- }
C#pingコマンド実行:pingコマンド実行
まず、Processクラスを使用して、独立したプロセスを作成し、Systemをインポートします.Diagnostics,
using System.Diagnostics;
インスタンスプロセスクラス、独立したプロセスの開始
Process p = new Process();
Processクラスには、いくつかの属性とメソッドを含むProcessStartInfoクラスのStartInfo属性があります.
次に、彼のいくつかのプロパティを使用します.
プログラム名の設定
p.StartInfo.FileName = "cmd.exe";
Shellの使用を閉じる
p.StartInfo.UseShellExecute = false;
リダイレクト標準入力
p.StartInfo.RedirectStandardInput = true;
リダイレクト標準出力
p.StartInfo.RedirectStandardOutput = true;
リダイレクトエラー出力
p.StartInfo.RedirectStandardError = true;
ウィンドウを表示しない設定
p.StartInfo.CreateNoWindow = true;
上記のプロパティの設定は、比較的重要なステップです.
設定されている以上、プロセスを開始しましょう.
p.Start();
実行するコマンドを入力します.ここでpingです.
p.StandardInput.WriteLine("ping -n 1 www.iwebtrados.com.cn ");
p.StandardInput.WriteLine("exit");
出力ストリームからコマンド実行結果を取得する、
string strRst = p.StandardOutput.ReadToEnd();
C#pingコマンド実装:c#2.0で追加されたPingクラスを利用
ここで私が書いたのはフォームプログラムです.まずtextbox、listbox、buttonコントロールを追加し、textboxはドメイン名またはIPを入力し、listboxは結果を表示する.
button 1_clickイベント入力
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- Pingp1=newPing();// ,
- PingReplyreply=p1.Send(this.textBox1.Text);//
- displayReply(reply);//
- }
- privatevoiddisplayReply(PingReplyreply)//
- {
- StringBuildersbuilder;
- if(reply.Status==IPStatus.Success)
- {
- sbuilder=newStringBuilder();
- sbuilder.Append(string.Format("Address:{0}",reply.Address.ToString()));
- sbuilder.Append(string.Format("RoundTriptime:{0}",reply.RoundtripTime));
- sbuilder.Append(string.Format("Timetolive:{0}",reply.Options.Ttl));
- sbuilder.Append(string.Format("Don'tfragment:{0}",reply.Options.DontFragment));
- sbuilder.Append(string.Format("Buffersize:{0}",reply.Buffer.Length));
- listBox1.Items.Add(sbuilder.ToString());
- }
- }
非同期の処理をしてbutton 1_を修正することもできます.Click、PingCompletedCallBackメソッドを追加
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- Pingp1=newPing();
- p1.PingCompleted+=newPingCompletedEventHandler(this.PingCompletedCallBack);// PingCompleted
- p1.SendAsync(this.textBox1.Text,null);
- }
- privatevoidPingCompletedCallBack(objectsender,PingCompletedEventArgse)
- {
- if(e.Cancelled)
- {
- listBox1.Items.Add("PingCanncel");
- return;
- }
- if(e.Error!=null)
- {
- listBox1.Items.Add(e.Error.Message);
- return;
- }
- StringBuildersbuilder;
- PingReplyreply=e.Reply;
- if(reply.Status==IPStatus.Success)
- {
- sbuilder=newStringBuilder();
- sbuilder.Append(string.Format("Address:{0}",reply.Address.ToString()));
- sbuilder.Append(string.Format("RoundTriptime:{0}",reply.RoundtripTime));
- sbuilder.Append(string.Format("Timetolive:{0}",reply.Options.Ttl));
- sbuilder.Append(string.Format("Don'tfragment:{0}",reply.Options.DontFragment));
- sbuilder.Append(string.Format("Buffersize:{0}",reply.Buffer.Length));
- listBox1.Items.Add(sbuilder.ToString());
- }
- }
どうですか、1、2方式に比べて、3は簡単なほうが多いのではないでしょうか.
本文はネット小築のブログ:『c#の下でping操作を実現する』から来た.