Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- namespace QuoteClient
- {
- /// <summary>
- /// Summary description for Form1.
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.TextBox textHostname;
- private System.Windows.Forms.StatusBar statusBar;
- private System.Windows.Forms.Button buttonQuote;
- private System.Windows.Forms.TextBox textQuote;
- private System.Windows.Forms.TextBox textPortNumber;
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Required for Windows Form Designer support
- //
- InitializeComponent();
- //
- // TODO: Add any constructor code after InitializeComponent call
- //
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.textHostname = new System.Windows.Forms.TextBox();
- this.textPortNumber = new System.Windows.Forms.TextBox();
- this.statusBar = new System.Windows.Forms.StatusBar();
- this.buttonQuote = new System.Windows.Forms.Button();
- this.textQuote = new System.Windows.Forms.TextBox();
- this.SuspendLayout();
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(16, 16);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(64, 23);
- this.label1.TabIndex = 0;
- this.label1.Text = "Hostname:";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(208, 16);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(48, 23);
- this.label2.TabIndex = 2;
- this.label2.Text = "Port:";
- //
- // textHostname
- //
- this.textHostname.Location = new System.Drawing.Point(88, 16);
- this.textHostname.Name = "textHostname";
- this.textHostname.TabIndex = 1;
- this.textHostname.Text = "localhost";
- //
- // textPortNumber
- //
- this.textPortNumber.Location = new System.Drawing.Point(264, 16);
- this.textPortNumber.Name = "textPortNumber";
- this.textPortNumber.Size = new System.Drawing.Size(48, 20);
- this.textPortNumber.TabIndex = 3;
- this.textPortNumber.Text = "4567";
- //
- // statusBar
- //
- this.statusBar.Location = new System.Drawing.Point(0, 223);
- this.statusBar.Name = "statusBar";
- this.statusBar.Size = new System.Drawing.Size(336, 22);
- this.statusBar.TabIndex = 6;
- this.statusBar.Text = "statusBar";
- //
- // buttonQuote
- //
- this.buttonQuote.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.buttonQuote.Location = new System.Drawing.Point(8, 168);
- this.buttonQuote.Name = "buttonQuote";
- this.buttonQuote.Size = new System.Drawing.Size(320, 40);
- this.buttonQuote.TabIndex = 5;
- this.buttonQuote.Text = "Get Quote";
- this.buttonQuote.Click += new System.EventHandler(this.buttonQuote_Click);
- //
- // textQuote
- //
- this.textQuote.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.textQuote.Location = new System.Drawing.Point(8, 56);
- this.textQuote.Multiline = true;
- this.textQuote.Name = "textQuote";
- this.textQuote.Size = new System.Drawing.Size(312, 96);
- this.textQuote.TabIndex = 4;
- this.textQuote.Text = "";
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(336, 245);
- this.Controls.Add(this.textQuote);
- this.Controls.Add(this.buttonQuote);
- this.Controls.Add(this.statusBar);
- this.Controls.Add(this.textPortNumber);
- this.Controls.Add(this.textHostname);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Name = "Form1";
- this.Text = "Quote of the Day";
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void richTextBox1_TextChanged(object sender, System.EventArgs e)
- {
-
- }
- private void buttonQuote_Click(object sender, System.EventArgs e)
- {
- statusBar.Text = "";
- string server = textHostname.Text;
- try
- {
- int port = Convert.ToInt32(textPortNumber.Text);
- }
- catch (FormatException ex)
- {
- statusBar.Text = ex.Message;
- return;
- }
- TcpClient client = new TcpClient();
- try
- {
- client.Connect(textHostname.Text,
- Convert.ToInt32(textPortNumber.Text));
- NetworkStream stream = client.GetStream();
- byte[] buffer = new Byte[1024];
- int received = stream.Read(buffer, 0, 1024);
- if (received <= 0)
- {
- statusBar.Text = "Read failed";
- return;
- }
- textQuote.Text = Encoding.Unicode.GetString(buffer);
- }
- catch (SocketException ex)
- {
- statusBar.Text = ex.Message;
- }
- finally
- {
- client.Close();
- }
- }
- }
- }