An example in C # network programming is parsed by asynchronously. It is mainly to apply the BeginResolve (), and endresolve () methods for the DNS class. This example has a problem that when it comes to the resolved domain name, the function is not called asyncCallback specified. I added a few code, set for 2 seconds timeout, timeout, display timeout. The problem now is that there is no parsing of the domain name after the timeout. The domain name that is more than 2 seconds will be displayed in ListBox. After the timeout wants to call DNS.Endresolve (), the abnormality will be thrown.
Need to study.
Using system.drawing; using system.collections; using system.bomponentmodel; using system.data; using system.net; using system.threading;
Namespace network {
public class DnsResolver: System.Windows.Forms.Form {private System.Windows.Forms.Button btnResolve; private System.Windows.Forms.Button btnClear; private System.Windows.Forms.Button btnAbout; private System.Windows.Forms.ListBox LBRESULTS; private system.windows.Forms.TextBox TXTHOST;
Private system.componentmodel.Container Components = NULL;
Public dnsresolver () {initializationComponent ();
Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose (Disposing);
#Region Windows Form Designer Generated Code ///
/// summary> private void InitializeComponent () {this.btnResolve = new System.Windows.Forms.Button (); this.txtHost = new System.Windows.Forms.TextBox (); this.lbResults = new System. Windows.Forms.ListBox (); this.btnclear = new system.windows.forms.button (); this.btnabout = new system.windows.forms.button (); this.suspendlayout (); // // btnResolve / / this.btnResolve.Location = new System.Drawing.Point (24, 64); this.btnResolve.Name = "btnResolve"; this.btnResolve.TabIndex = 0; this.btnResolve.Text = "& Resolve"; this.btnResolve .Click = new system.EventHandler (this.btnreesolve_click); /// TXthost // this.txthost.location = new system.drawing.point (8, 24); this.txthost.name = "txthost"; THIST .txthost.size = new system.drawing.size (160, 21); this.txthost.tabindex = 1; this.txthost.text = ""; /// LBRESULTS // this.lbresults.ItemHeight = 12; this .lbresults.location = new system.drawing.point (176, 8); this.lbresults.name = "LBR Esults "; this.lbresults.size = new system.drawing.size (296, 232); this.lbresults.tabindex = 2; // // btnce // this.btnclear.location = new system.drawing.point (24 112); this.btnclear.name = "btnce"; this.btnclear.tabindex = 3; this.btnclear.text = "& clear"; this.btnclear.click = new system.eventhandler (this.btnclear_click); / / // btnabout // this.btnabout.location = new system.drawing.point (24, 160); this.btnabout.name = "btnabout"; this.btnabout.tabindex = 4; this.btnabout.text = "&
About ... "; this.btnabout.click = new system.eventhandler (this.btnabout_click); // // DNSRESESESIZE = New System.drawing.Size (6, 14); this.clientsize = New system.drawing.size (504, 253); this.controls.addrange (new system.windows.forms.control [] {this.btnabout, this.btnclear, this.lbresults, this.txthost, this.btnresolve}) This.name = "DNSRESOLVER"; this.text = "DNS resolver"; this.ResumeLayout (false);} #ENDREGION
///
Void DNSRESOLVED (IASYNCRESULT AR) {iphostentry entry = dns.endresolve (ar); foreach (iPaddress IP in entry.addresslist) {lbresults.Items.Add ("Address:" ip.tostring ());
Foreach (string alias in entry.aliases) {lbresults.Items.add ("alias:" alias.tostring ());
LBRESULTS.ITEMS.ADD ("Resolve Done.");
Private void btnresolve_click (object sender, system.eventargs e) {if (txthost.text == ") Return;
LBRESULTS.ITEMS.ADD ("Resolve Beginning ..."); IASYNCRESULT AR = DNS.BeginResolve (TXTHOST.TEXT, New AsyncCallback (DNSRESOLVED), NULL); if (! ar.asyncwaithandle.waitone (2000, false) { LBRESULTS.ITEMS.Add ("Two Seconds Elapsed, Failed");}}
Private void btnclear_click (Object sender, system.eventargs e) {lbresults.Items.clear ();