📄 frmclient.cs
字号:
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.IO;
namespace Example_3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmClient : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Label lblMessage;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
//User defined variables
private TcpClient objTcpClientSocketForServer;
private NetworkStream objNetworkStream;
private StreamReader objStreamReader;
private System.Windows.Forms.TextBox txtText;
private System.Windows.Forms.Label lbltext;
private System.Windows.Forms.Button btnExit;
private StreamWriter objStreamWriter;
public frmClient()
{
//
// 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.btnConnect = new System.Windows.Forms.Button();
this.lblMessage = new System.Windows.Forms.Label();
this.txtText = new System.Windows.Forms.TextBox();
this.lbltext = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(96, 86);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(90, 25);
this.btnConnect.TabIndex = 0;
this.btnConnect.Text = "连接(&C)";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// lblMessage
//
this.lblMessage.Location = new System.Drawing.Point(29, 52);
this.lblMessage.Name = "lblMessage";
this.lblMessage.Size = new System.Drawing.Size(259, 26);
this.lblMessage.TabIndex = 1;
//
// txtText
//
this.txtText.Location = new System.Drawing.Point(96, 9);
this.txtText.Name = "txtText";
this.txtText.Size = new System.Drawing.Size(192, 21);
this.txtText.TabIndex = 2;
this.txtText.Text = "";
//
// lbltext
//
this.lbltext.Location = new System.Drawing.Point(19, 9);
this.lbltext.Name = "lbltext";
this.lbltext.Size = new System.Drawing.Size(67, 24);
this.lbltext.TabIndex = 3;
this.lbltext.Text = "消息:";
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(202, 86);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(90, 25);
this.btnExit.TabIndex = 0;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// frmClient
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(297, 117);
this.Controls.Add(this.lbltext);
this.Controls.Add(this.txtText);
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.btnExit);
this.MaximizeBox = false;
this.Name = "frmClient";
this.Text = "客户端";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmClient());
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
try
{
objTcpClientSocketForServer = new TcpClient("localHost", 8221);
}
catch
{
lblMessage.Text = "未能在 8221 处连接至服务器";
return;
}
objNetworkStream = objTcpClientSocketForServer.GetStream();
objStreamReader = new StreamReader(objNetworkStream);
objStreamWriter =new StreamWriter(objNetworkStream);
try
{
string outputString;
// read the data from the host and display it
outputString = objStreamReader.ReadLine();
lblMessage.Text= outputString;
objStreamWriter.WriteLine(this.txtText.Text);
lblMessage.Text= this.txtText.Text;
objStreamWriter.Flush();
}
catch
{
Console.WriteLine("从服务器中读取时产生异常");
}
// closing the network stream
objNetworkStream.Close();
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -