⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个聊天服务器与客户端程序,用c#编写
💻 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.Threading ;

namespace sock_user
{
	/// <summary>
	/// 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.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.RichTextBox richTextBox2;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private IPAddress myIP=IPAddress.Parse("127.0.0.1");  //把字符串转换为IPAddress格式;
		private IPEndPoint MyServer;
		private Socket connectsock;
		private bool check=true;
		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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.richTextBox2 = new System.Windows.Forms.RichTextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(2, 12);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(60, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "服务器IP:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(2, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(60, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "请求端口:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(2, 86);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(60, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "接受信息:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(2, 158);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(60, 16);
			this.label4.TabIndex = 0;
			this.label4.Text = "发送信息:";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(66, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(94, 21);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(66, 44);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(94, 21);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.listBox1);
			this.groupBox1.Location = new System.Drawing.Point(162, 2);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(168, 66);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "客户状态";
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(4, 12);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(160, 52);
			this.listBox1.TabIndex = 0;
			// 
			// richTextBox1
			// 
			this.richTextBox1.Location = new System.Drawing.Point(66, 82);
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.Size = new System.Drawing.Size(264, 64);
			this.richTextBox1.TabIndex = 3;
			this.richTextBox1.Text = "";
			// 
			// richTextBox2
			// 
			this.richTextBox2.Location = new System.Drawing.Point(66, 156);
			this.richTextBox2.Name = "richTextBox2";
			this.richTextBox2.Size = new System.Drawing.Size(264, 64);
			this.richTextBox2.TabIndex = 3;
			this.richTextBox2.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(18, 240);
			this.button1.Name = "button1";
			this.button1.TabIndex = 4;
			this.button1.Text = "连接";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(128, 240);
			this.button2.Name = "button2";
			this.button2.TabIndex = 4;
			this.button2.Text = "发送信息";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(240, 240);
			this.button3.Name = "button3";
			this.button3.TabIndex = 4;
			this.button3.Text = "关闭连接";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(336, 281);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.richTextBox1);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.richTextBox2);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button3);
			this.Name = "Form1";
			this.Text = "网络聊天客户端";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				//把textBox1.Text输入的IP地址字符串转换为IPAddress格式;
				myIP=IPAddress.Parse (textBox1.Text);
			}
			catch
			{
				MessageBox.Show ("您输入的IP地址格式不正确!");
			}
			try
			{
				MyServer=new IPEndPoint(myIP,Int32.Parse (textBox2.Text ));
				connectsock=new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
				connectsock.Connect (MyServer);
				listBox1.Items.Add ("与主机"+textBox1.Text +"端口"+textBox2.Text+"连接成功!\r\n");
				Thread thread=new Thread (new ThreadStart (receive));
				thread.Start ();
			}
			catch(Exception ee)
			{
				MessageBox.Show (ee.Message );
			}

		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			try
			{
				Byte[] sendbyte=new Byte [64];
				string send=richTextBox2.Text +"\r\n";
				//生成NetworkStream实例,用于发送基础数据流。


				NetworkStream netstream=new NetworkStream (connectsock);
				sendbyte=System.Text .Encoding .BigEndianUnicode .GetBytes (send.ToCharArray ());
				netstream.Write (sendbyte,0,sendbyte.Length );//向socket服务器发送信息。
				netstream.Flush ();
			}
			catch
			{
				MessageBox.Show ("连接没有建立!无法发送!");
			}

		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			try
			{
				connectsock.Close ();//断开连接。
				listBox1.Items .Add ("与主机"+textBox1.Text +"端口"+textBox2.Text +"断开连接!\r\n");
			}
			catch
			{
				MessageBox.Show ("连接没有建立!断开无效!");
			}

		}

		private void receive()
		{
			while(true)
			{
				Byte[] Rec=new byte [64];
				NetworkStream netstream=new NetworkStream (connectsock);
				netstream.Read (Rec,0,Rec.Length );//读取客户发送来的信息。
				string RecMessage=System.Text .Encoding .BigEndianUnicode .GetString (Rec);
				richTextBox1.AppendText (RecMessage+"\r\n");//写入到接收信息栏中。
			}
		}


	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -