Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. namespace QuoteClient
  11. {
  12. /// <summary>
  13. /// Summary description for Form1.
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.Label label1;
  18. private System.Windows.Forms.Label label2;
  19. private System.Windows.Forms.TextBox textHostname;
  20. private System.Windows.Forms.StatusBar statusBar;
  21. private System.Windows.Forms.Button buttonQuote;
  22. private System.Windows.Forms.TextBox textQuote;
  23. private System.Windows.Forms.TextBox textPortNumber;
  24. /// <summary>
  25. /// Required designer variable.
  26. /// </summary>
  27. private System.ComponentModel.Container components = null;
  28. public Form1()
  29. {
  30. //
  31. // Required for Windows Form Designer support
  32. //
  33. InitializeComponent();
  34. //
  35. // TODO: Add any constructor code after InitializeComponent call
  36. //
  37. }
  38. /// <summary>
  39. /// Clean up any resources being used.
  40. /// </summary>
  41. protected override void Dispose( bool disposing )
  42. {
  43. if( disposing )
  44. {
  45. if (components != null) 
  46. {
  47. components.Dispose();
  48. }
  49. }
  50. base.Dispose( disposing );
  51. }
  52. #region Windows Form Designer generated code
  53. /// <summary>
  54. /// Required method for Designer support - do not modify
  55. /// the contents of this method with the code editor.
  56. /// </summary>
  57. private void InitializeComponent()
  58. {
  59. this.label1 = new System.Windows.Forms.Label();
  60. this.label2 = new System.Windows.Forms.Label();
  61. this.textHostname = new System.Windows.Forms.TextBox();
  62. this.textPortNumber = new System.Windows.Forms.TextBox();
  63. this.statusBar = new System.Windows.Forms.StatusBar();
  64. this.buttonQuote = new System.Windows.Forms.Button();
  65. this.textQuote = new System.Windows.Forms.TextBox();
  66. this.SuspendLayout();
  67. // 
  68. // label1
  69. // 
  70. this.label1.Location = new System.Drawing.Point(16, 16);
  71. this.label1.Name = "label1";
  72. this.label1.Size = new System.Drawing.Size(64, 23);
  73. this.label1.TabIndex = 0;
  74. this.label1.Text = "Hostname:";
  75. // 
  76. // label2
  77. // 
  78. this.label2.Location = new System.Drawing.Point(208, 16);
  79. this.label2.Name = "label2";
  80. this.label2.Size = new System.Drawing.Size(48, 23);
  81. this.label2.TabIndex = 2;
  82. this.label2.Text = "Port:";
  83. // 
  84. // textHostname
  85. // 
  86. this.textHostname.Location = new System.Drawing.Point(88, 16);
  87. this.textHostname.Name = "textHostname";
  88. this.textHostname.TabIndex = 1;
  89. this.textHostname.Text = "localhost";
  90. // 
  91. // textPortNumber
  92. // 
  93. this.textPortNumber.Location = new System.Drawing.Point(264, 16);
  94. this.textPortNumber.Name = "textPortNumber";
  95. this.textPortNumber.Size = new System.Drawing.Size(48, 20);
  96. this.textPortNumber.TabIndex = 3;
  97. this.textPortNumber.Text = "4567";
  98. // 
  99. // statusBar
  100. // 
  101. this.statusBar.Location = new System.Drawing.Point(0, 223);
  102. this.statusBar.Name = "statusBar";
  103. this.statusBar.Size = new System.Drawing.Size(336, 22);
  104. this.statusBar.TabIndex = 6;
  105. this.statusBar.Text = "statusBar";
  106. // 
  107. // buttonQuote
  108. // 
  109. this.buttonQuote.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  110. | System.Windows.Forms.AnchorStyles.Left) 
  111. | System.Windows.Forms.AnchorStyles.Right)));
  112. this.buttonQuote.Location = new System.Drawing.Point(8, 168);
  113. this.buttonQuote.Name = "buttonQuote";
  114. this.buttonQuote.Size = new System.Drawing.Size(320, 40);
  115. this.buttonQuote.TabIndex = 5;
  116. this.buttonQuote.Text = "Get Quote";
  117. this.buttonQuote.Click += new System.EventHandler(this.buttonQuote_Click);
  118. // 
  119. // textQuote
  120. // 
  121. this.textQuote.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  122. | System.Windows.Forms.AnchorStyles.Left) 
  123. | System.Windows.Forms.AnchorStyles.Right)));
  124. this.textQuote.Location = new System.Drawing.Point(8, 56);
  125. this.textQuote.Multiline = true;
  126. this.textQuote.Name = "textQuote";
  127. this.textQuote.Size = new System.Drawing.Size(312, 96);
  128. this.textQuote.TabIndex = 4;
  129. this.textQuote.Text = "";
  130. // 
  131. // Form1
  132. // 
  133. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  134. this.ClientSize = new System.Drawing.Size(336, 245);
  135. this.Controls.Add(this.textQuote);
  136. this.Controls.Add(this.buttonQuote);
  137. this.Controls.Add(this.statusBar);
  138. this.Controls.Add(this.textPortNumber);
  139. this.Controls.Add(this.textHostname);
  140. this.Controls.Add(this.label2);
  141. this.Controls.Add(this.label1);
  142. this.Name = "Form1";
  143. this.Text = "Quote of the Day";
  144. this.ResumeLayout(false);
  145. }
  146. #endregion
  147. /// <summary>
  148. /// The main entry point for the application.
  149. /// </summary>
  150. [STAThread]
  151. static void Main() 
  152. {
  153. Application.Run(new Form1());
  154. }
  155. private void richTextBox1_TextChanged(object sender, System.EventArgs e)
  156. {
  157. }
  158. private void buttonQuote_Click(object sender, System.EventArgs e)
  159. {
  160. statusBar.Text = "";
  161. string server = textHostname.Text;
  162. try
  163. {
  164. int port = Convert.ToInt32(textPortNumber.Text);
  165. }
  166. catch (FormatException ex)
  167. {
  168. statusBar.Text = ex.Message;
  169. return;
  170. }
  171. TcpClient client = new TcpClient();
  172. try
  173. {
  174. client.Connect(textHostname.Text,
  175. Convert.ToInt32(textPortNumber.Text));
  176. NetworkStream stream = client.GetStream();
  177. byte[] buffer = new Byte[1024];
  178. int received = stream.Read(buffer, 0, 1024);
  179. if (received <= 0)
  180. {
  181. statusBar.Text = "Read failed";
  182. return;
  183. }
  184. textQuote.Text = Encoding.Unicode.GetString(buffer);
  185. }      
  186. catch (SocketException ex)
  187. {
  188. statusBar.Text = ex.Message;
  189. }
  190. finally
  191. {
  192. client.Close();
  193. }
  194. }
  195. }
  196. }