📄 form1.cs
字号:
/**********************************************
* 聊天室客户端(教学示例程序)
* -------------------------------------------
* 作者:孙继磊
* 日期:2006-12-4
* -------------------------------------------
* 作者保留本软件所有版权。All rights reserved.
* 你可以免费复制、传播、修改本软件,但要保留本版权信息。
* -------------------------------------------
* 修改纪录
*
* ********************************************/
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.Threading;
namespace ChatClient
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Socket client;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox message;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox history;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel connectedPanel;
private System.Windows.Forms.StatusBarPanel connectedTimePanel;
private System.Windows.Forms.TextBox userName;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.message = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.history = new System.Windows.Forms.ListBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.connectedPanel = new System.Windows.Forms.StatusBarPanel();
this.connectedTimePanel = new System.Windows.Forms.StatusBarPanel();
this.userName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.connectedPanel)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.connectedTimePanel)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem6});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4,
this.menuItem5});
this.menuItem1.Text = "Server";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Connect";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "Disconnect";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "-";
//
// menuItem5
//
this.menuItem5.Index = 3;
this.menuItem5.Text = "Exit";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem7,
this.menuItem8});
this.menuItem6.Text = "Message";
this.menuItem6.Visible = false;
//
// menuItem7
//
this.menuItem7.Index = 0;
this.menuItem7.Text = "Send";
//
// menuItem8
//
this.menuItem8.Index = 1;
this.menuItem8.Text = "Receive";
//
// button1
//
this.button1.Location = new System.Drawing.Point(576, 504);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "Send";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// message
//
this.message.Location = new System.Drawing.Point(72, 504);
this.message.Name = "message";
this.message.Size = new System.Drawing.Size(496, 21);
this.message.TabIndex = 6;
this.message.Text = "textBox1";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 504);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Message";
//
// history
//
this.history.ItemHeight = 12;
this.history.Location = new System.Drawing.Point(8, 40);
this.history.Name = "history";
this.history.Size = new System.Drawing.Size(656, 460);
this.history.TabIndex = 4;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 539);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.connectedPanel,
this.connectedTimePanel});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(672, 22);
this.statusBar1.TabIndex = 8;
//
// connectedPanel
//
this.connectedPanel.Text = "Unconnected";
//
// connectedTimePanel
//
this.connectedTimePanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
this.connectedTimePanel.Width = 10;
//
// userName
//
this.userName.Location = new System.Drawing.Point(112, 8);
this.userName.MaxLength = 10;
this.userName.Name = "userName";
this.userName.Size = new System.Drawing.Size(224, 21);
this.userName.TabIndex = 11;
this.userName.Text = "无名小卒";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 8);
this.label2.Name = "label2";
this.label2.TabIndex = 10;
this.label2.Text = "User Name:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 561);
this.Controls.Add(this.userName);
this.Controls.Add(this.label2);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.message);
this.Controls.Add(this.label1);
this.Controls.Add(this.history);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Chat Client";
((System.ComponentModel.ISupportInitialize)(this.connectedPanel)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.connectedTimePanel)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
IPEndPoint remote;
if(client==null)
client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.IP);
if(!client.Connected)
{
remote=new IPEndPoint(IPAddress.Parse("10.3.3.10"),5678);
client.Connect(remote);
history.Items.Add("System : This client has connected to the server.");
statusBar1.Panels[0].Text="Connected";
statusBar1.Panels[1].Text="Connected at "+DateTime.Now.ToLocalTime();
ThreadStart ts=new ThreadStart(this.receiveData);
Thread t=new Thread(ts);
t.Start();
}
else
{
MessageBox.Show("The socket has alreay connected to a server!","message",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
if(message.Text.Trim()=="")
return;
client.Send(System.Text.Encoding.Unicode.GetBytes(userName.Text+"说:"+message.Text));//conert string to byte array and send it
message.Text="";
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
client.Shutdown(SocketShutdown.Both);
client.Close();
history.Items.Add("System : This client has disconnected from the server.");
statusBar1.Panels[0].Text="Unconnected";
statusBar1.Panels[1].Text="";
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void receiveData()
{
byte[] buffer=new byte[1024];
while(true)
{
int size=client.Receive(buffer);
if (size==0)
{
client.Shutdown(SocketShutdown.Both);
client.Close();
break;
}
string txt=System.Text.Encoding.Unicode.GetString(buffer,0,size);//convert byte[] to string
history.Items.Add(txt);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -