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

📄 tcpclientclient.cs

📁 聊天系统的经
💻 CS
字号:
using System;
using System.Drawing ; 
using System.Collections ; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data;

namespace WinSock
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class TCPClient : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox lstMsg;
		private System.Windows.Forms.TextBox txtSend;
		private System.Windows.Forms.PictureBox btnExit;
		private System.Windows.Forms.PictureBox btnConnect;
		private System.Windows.Forms.Panel panLogin;
		private System.Windows.Forms.PictureBox btnDisConeect;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.TextBox txtUserName;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.TextBox txtServerName;
		private System.Windows.Forms.PictureBox btnConnectToServer;
		private System.Windows.Forms.PictureBox btnSend;
		private P2PSvr.P2P MyP2P;

		public TCPClient()
		{
			//
			// 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.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TCPClient));
			this.lstMsg = new System.Windows.Forms.ListBox();
			this.txtSend = new System.Windows.Forms.TextBox();
			this.btnConnect = new System.Windows.Forms.PictureBox();
			this.btnExit = new System.Windows.Forms.PictureBox();
			this.panLogin = new System.Windows.Forms.Panel();
			this.btnDisConeect = new System.Windows.Forms.PictureBox();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.txtUserName = new System.Windows.Forms.TextBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.txtServerName = new System.Windows.Forms.TextBox();
			this.btnConnectToServer = new System.Windows.Forms.PictureBox();
			this.btnSend = new System.Windows.Forms.PictureBox();
			this.panLogin.SuspendLayout();
			this.SuspendLayout();
			// 
			// lstMsg
			// 
			this.lstMsg.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.lstMsg.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.lstMsg.ForeColor = System.Drawing.Color.Black;
			this.lstMsg.ItemHeight = 15;
			this.lstMsg.Location = new System.Drawing.Point(181, 82);
			this.lstMsg.Name = "lstMsg";
			this.lstMsg.Size = new System.Drawing.Size(466, 225);
			this.lstMsg.TabIndex = 1;
			// 
			// txtSend
			// 
			this.txtSend.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.txtSend.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.txtSend.ForeColor = System.Drawing.Color.Black;
			this.txtSend.Location = new System.Drawing.Point(182, 325);
			this.txtSend.Multiline = true;
			this.txtSend.Name = "txtSend";
			this.txtSend.Size = new System.Drawing.Size(385, 58);
			this.txtSend.TabIndex = 3;
			this.txtSend.Text = "";
			// 
			// btnConnect
			// 
			this.btnConnect.Cursor = System.Windows.Forms.Cursors.PanWest;
			this.btnConnect.Image = ((System.Drawing.Image)(resources.GetObject("btnConnect.Image")));
			this.btnConnect.Location = new System.Drawing.Point(19, 284);
			this.btnConnect.Name = "btnConnect";
			this.btnConnect.Size = new System.Drawing.Size(112, 27);
			this.btnConnect.TabIndex = 4;
			this.btnConnect.TabStop = false;
			this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
			// 
			// btnExit
			// 
			this.btnExit.Cursor = System.Windows.Forms.Cursors.PanEast;
			this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image")));
			this.btnExit.Location = new System.Drawing.Point(18, 358);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(112, 26);
			this.btnExit.TabIndex = 5;
			this.btnExit.TabStop = false;
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// panLogin
			// 
			this.panLogin.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.panLogin.Controls.Add(this.btnConnectToServer);
			this.panLogin.Controls.Add(this.txtServerName);
			this.panLogin.Controls.Add(this.pictureBox2);
			this.panLogin.Controls.Add(this.txtUserName);
			this.panLogin.Controls.Add(this.pictureBox1);
			this.panLogin.Location = new System.Drawing.Point(279, 156);
			this.panLogin.Name = "panLogin";
			this.panLogin.Size = new System.Drawing.Size(298, 76);
			this.panLogin.TabIndex = 10;
			// 
			// btnDisConeect
			// 
			this.btnDisConeect.Cursor = System.Windows.Forms.Cursors.PanWest;
			this.btnDisConeect.Image = ((System.Drawing.Image)(resources.GetObject("btnDisConeect.Image")));
			this.btnDisConeect.Location = new System.Drawing.Point(18, 321);
			this.btnDisConeect.Name = "btnDisConeect";
			this.btnDisConeect.Size = new System.Drawing.Size(112, 27);
			this.btnDisConeect.TabIndex = 11;
			this.btnDisConeect.TabStop = false;
			this.btnDisConeect.Click += new System.EventHandler(this.btnDisConeect_Click);
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(10, 6);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(197, 27);
			this.pictureBox1.TabIndex = 12;
			this.pictureBox1.TabStop = false;
			// 
			// txtUserName
			// 
			this.txtUserName.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.txtUserName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtUserName.Location = new System.Drawing.Point(64, 10);
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new System.Drawing.Size(135, 21);
			this.txtUserName.TabIndex = 13;
			this.txtUserName.Text = "南霸天";
			// 
			// pictureBox2
			// 
			this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
			this.pictureBox2.Location = new System.Drawing.Point(12, 41);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(197, 28);
			this.pictureBox2.TabIndex = 14;
			this.pictureBox2.TabStop = false;
			// 
			// txtServerName
			// 
			this.txtServerName.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.txtServerName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtServerName.Location = new System.Drawing.Point(65, 45);
			this.txtServerName.Name = "txtServerName";
			this.txtServerName.Size = new System.Drawing.Size(134, 21);
			this.txtServerName.TabIndex = 15;
			this.txtServerName.Text = "LocalHost";
			// 
			// btnConnectToServer
			// 
			this.btnConnectToServer.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnConnectToServer.BackgroundImage")));
			this.btnConnectToServer.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnConnectToServer.Image = ((System.Drawing.Image)(resources.GetObject("btnConnectToServer.Image")));
			this.btnConnectToServer.Location = new System.Drawing.Point(218, 9);
			this.btnConnectToServer.Name = "btnConnectToServer";
			this.btnConnectToServer.Size = new System.Drawing.Size(72, 58);
			this.btnConnectToServer.TabIndex = 16;
			this.btnConnectToServer.TabStop = false;
			this.btnConnectToServer.Click += new System.EventHandler(this.btnConnectToServer_Click);
			// 
			// btnSend
			// 
			this.btnSend.Cursor = System.Windows.Forms.Cursors.PanNorth;
			this.btnSend.Image = ((System.Drawing.Image)(resources.GetObject("btnSend.Image")));
			this.btnSend.Location = new System.Drawing.Point(570, 320);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(77, 64);
			this.btnSend.TabIndex = 12;
			this.btnSend.TabStop = false;
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// TCPClient
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(711, 414);
			this.ControlBox = false;
			this.Controls.Add(this.btnSend);
			this.Controls.Add(this.btnDisConeect);
			this.Controls.Add(this.panLogin);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnConnect);
			this.Controls.Add(this.txtSend);
			this.Controls.Add(this.lstMsg);
			this.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.MaximizeBox = false;
			this.Name = "TCPClient";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.TransparencyKey = System.Drawing.Color.Red;
			this.Load += new System.EventHandler(this.TCPClient_Load);
			this.panLogin.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close(); 
			this.MyP2P.DisposeMe();
			this.MyP2P = null;
			Application.Exit();
		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			if (txtSend.Text.Length ==0)
				return;
			if ((txtSend.Text).Length>40)
				return;

			string sMsg = txtUserName.Text +": "+txtSend.Text; 
			if (MyP2P.SendMsg(sMsg))
			{
				lstMsg.Items.Add(sMsg) ; 
				txtSend.Clear(); 
			}
			else
			{
				txtSend.Text = "无法发送消息到目标计算机!"; 
			} 		
		}

		private void TCPClient_Load(object sender, System.EventArgs e)
		{
			MyP2P = new P2PSvr.P2P();
			MyP2P.OnMsgListened+=new P2PSvr.P2P.DelgMsgListened(MyP2P_OnMsgListened); 

			MyP2P.ThreadToListen();
		}

		private void btnConnect_Click(object sender, System.EventArgs e)
		{
			panLogin.Visible = !panLogin.Visible; 
		}

		private void btnConnectToServer_Click(object sender, System.EventArgs e)
		{
			if (MyP2P.ConnectToRemote(txtServerName.Text.Trim()))
			{
				btnSend.Visible = true;
				panLogin.Visible = false;
				this.Refresh(); 
			} 
			else
			{
				txtSend.Text = "目标计算机拒绝连接请求......" ; 
			} 
		}

		private void btnDisConeect_Click(object sender, System.EventArgs e)
		{
			MyP2P.DisConnectToRemote(); 
			btnSend.Visible = false;
		}

		private void MyP2P_OnMsgListened()
		{
			lstMsg.Items.Add(MyP2P.MsgLisened); 
		}
	}
}

⌨️ 快捷键说明

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